从鼠标点击位置转换

时间:2014-11-11 07:59:26

标签: unity3d unityscript mouseclick-event

尝试了这百万种方式,总是会出现转换错误。我只是在一个平面上点击,希望点击鼠标位置以进入变换变量,以便我的角色走向那一点。我使用var target: Transform;来定义角色移动的位置。如何从世界空间中的点击中提取该类型的变量,该点击将与平面(或任何东西)相交?

var WalkingSpeed = .05;

// The target marker.
var target: Transform;

// Speed in units per sec.
var speed: float;

static var mousePos : Vector3;

function Update() {
    // get distance between objects current position and the target position
    var distance = Vector3.Distance (target.transform.position, transform.position);

    if(distance > 0) {
        // we are still not at the target, do walking animation
        animation["walk"].speed = 1;    // set speed of animation
        animation.CrossFade("walk");    // actually play the animation
    } else {
        animation["idle"].speed = 1;    // set speed of animation
        animation.CrossFade("idle");    // actually play the animation
    }
    //Debug.Log(distance);

    // The step size is equal to speed times frame time.
    var step = speed * Time.deltaTime;

    // Move our position a step closer to the target.
    transform.position = Vector3.MoveTowards(transform.position, target.position, step);

    // Also rotate towards the point
    var targetDir = target.position - transform.position;
    var newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0);
    // Move our position a step closer to the target.
    transform.rotation = Quaternion.LookRotation(newDir);

    Debug.DrawRay(transform.position, newDir, Color.red);   // draw something showing direction (for debug)

    // on click, set new target position
    if (Input.GetMouseButton(0)) {
        var posVec = Input.mousePosition;
        posVec.z = transform.position.z - Camera.main.transform.position.z;
        target = Camera.main.ScreenToWorldPoint(posVec);

    }
}

这一个特别是抛出不能将vector3转换为变换

2 个答案:

答案 0 :(得分:6)

有两种方法可以做到这一点,这取决于您使用的相机类型。将任一脚本附加到要移动的对象上即可完成。确保将其放在Update()

正交

if(Input.GetMouseButton(0))
{
    var posVec = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    posVec.z = transform.position.z;

    transform.position = Vector3.MoveTowards(transform.position, posVec, speed * Time.deltaTime);
}

透视

 if (Input.GetMouseButton(0)) 
 {
     var posVec = Input.mousePosition;
     posVec.z = transform.position.z - Camera.main.transform.position.z;
     posVec = Camera.main.ScreenToWorldPoint(posVec);

     transform.position = Vector3.MoveTowards(transform.position, posVec, speed * Time.deltaTime);
 }

我还想指出transform.positionVector3

我可以告诉您的脚本已经附加到要移动的GameObject上。因此,您甚至不需要target

transform.position = Vector3.MoveTowards(transform.position, posVec, speed * Time.deltaTime);

将正常运作。


Video proof that code works

答案 1 :(得分:2)

给你的飞机一个对撞机并用光线投射获得命中的位置

  public Vector3 pos;
     void Update () {
             if (Input.GetMouseButtonDown (0)) {
                  Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 RaycastHit hit;
                  if (Physics.Raycast(ray, out hit))     
                      pos=hit.point;           

              } 
     }