Get transform from mouse click position

prismspecs picture prismspecs · Nov 11, 2014 · Viewed 10.6k times · Source

Tried this a million ways, always get conversion errors. I am simply clicking around, against a plane, would like mouse position at click to get pumped into a transform variable so that my character walks towards that point. I'm using var target: Transform; to define where the character moves. How can I extract that type of variable from a click in world space, which will intersect with a plane (or anything)?

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);

    }
}

This one in particular throws cannot convert vector3 to transform

Answer

apxcode picture apxcode · Nov 11, 2014

There are two ways to do it, which depend on the kind of camera you are using. Attach either script to the object you want to move and you are done. Make sure you put it in the Update().

The Orthographic

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);
}

The Perspective

 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);
 }

I would also like to point out that transform.position is a Vector3.

I can tell your script is already attached to the GameObject that you want to move. Therefore, you don't even need target.

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

will work correctly.


Video proof that code works.