Waypoint problem

I’m trying to turn my Mouse-Cursor into a waypoint. I’ve done this by making a game object hold a piece of code (which i will show below) which changes the mouse-cursor image and holds a waypoint script.

But when it comes to getting my object to go to the waypoint, it goes toward the gameobject holding the code rather than the actual cursor.

Is there a way to make my object go to the cursor rather than the gameobject which holds the code?

This is the ‘cursor-customising’ code i’m using (Thanks to ‘Stelimar’ from a differnt U.A. question):

var cursorImage : Texture;

function Start() {
    Screen.showCursor = false;
}

function OnGUI() {
    var mousePos : Vector3 = Input.mousePosition;
    var pos : Rect = Rect(mousePos.x,Screen.height - mousePos.y,cursorImage.width,cursorImage.height);
    GUI.Label(pos,cursorImage);
}

This is the waypoint code i’m using:

var waypoint : Transform;
var speed : float = 20;

function Update () {
var target : Vector3 = waypoint.position;
var moveDirection : Vector3 = target - transform.position;

var velocity = rigidbody.velocity;

if(moveDirection.magnitude < 1){
velocity = Vector3.zero;
}
else{
velocity = moveDirection.normalized * speed;
}

rigidbody.velocity = velocity;

}

Help?

You need to update the position of the “waypoint” to a new 3D position that corresponds to the current 2D position of the cursor.

In the first script you want to add something like:

Update() {
     transform.position = Camera.main.ScreenToWorldPoint(Vector3.zero + Input.mousePosition);
}

This will move the object in 3-space corresponding to the cursor’s movement. Unfortunately without knowing more about your specific project it is difficult to give you a more exact answer.

You can find the 3D point the mouse pointer is over with ScreenPointToRay and Raycast. The logic of your script must change a little, because there’s no waypoint object - just a target point. If there’s only one object using this script, you add to it the new code:

var target : Vector3; // use the target point instead of a Transform
var speed : float = 20;

function Start(){

    target = transform.position; // initialize target point
}

function Update(){

    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var hit: RaycastHit;
    if (Physics.Raycast(ray, hit)){
        target = hit.point; // only update target if the ray hit something
    }
    var moveDirection : Vector3 = target - transform.position;
    moveDirection.y = 0; //# ignore vertical distance #
    var velocity = rigidbody.velocity;
    if(moveDirection.magnitude < 1){
        velocity = Vector3.zero;
    }
    else{
        velocity = moveDirection.normalized * speed;
        velocity.y = rigidbody.velocity.y; //# keep rigidbody vertical velocity #
    }
    rigidbody.velocity = velocity;
}

To avoid problems with height differences between the moving object and the point clicked, you can ignore the difference in Y when measuring the distance, and set only the X and Z velocity components in order to preserve gravity. The instructions that implement this are marked with # in the comments. If you don’t want this features, just comment out or delete these lines.