Move an object towards a point using raycast from Main Camera

I’m trying to make an RTS, so I need to be able to right-click to where I want my unit to go. So far, I’ve gotten it so that I can select a unit with a script in the main camera, and then click to teleport the unit to a point. I don’t want the unit to teleport, but instead to move towards the point.

Here’s my code for the movement:

void Update(){
     if (Input.GetButtonDown ("Fire2") && target.tag == "Unit") {
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;
		if(Physics.Raycast(ray, out hit)){
			destination = hit.point;
			target.rigidbody.MovePosition(destination);
		}
	}
}

Nevermind, I fixed it. First I converted the .MovePosition to .MoveTowards, but it still jumped around. It turns out that the code was only executing during the frame in which you right-clicked, so I moved it out of the if statement it was in and adjusted the code accordingly.

Here’s the entire script, which is placed into the main camera. The way I set it up, you always need to have a target selected, so to prevent errors you need to put a gameObject with a rigidbody into “bTarg”.

public class ClickTarget : MonoBehaviour {

private GameObject target;
private Vector3 destination;
private float distance;
private Vector3 tTrans;

public GUIText targetDisplay;
public float speed;
public GameObject bTarg;

void Start () {
targetDisplay.text = “”;
distance = 0.0f;
target = bTarg;
}

void Update () {
if(Input.GetButtonDown(“Fire1”)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)){
if(hit.collider != null){
if(hit.collider.tag == “Unit”){
target = hit.collider.gameObject;
targetDisplay.text = "Unit: " + hit.collider.gameObject.name;
destination = target.transform.position;
target.rigidbody.freezeRotation = false;
}
if(hit.collider.tag == “Building”){
target = hit.collider.gameObject;
targetDisplay.text = "Building: " + hit.collider.gameObject.name;
}
}
}
}
}

void FixedUpdate(){
if (Input.GetButtonDown (“Fire2”) && target.tag == “Unit” && GUIUtility.hotControl == 0) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit)){
destination = hit.point;
}
}

tTrans = target.transform.position;
distance = Vector3.Distance (tTrans, destination);

if(target.tag == "Unit"){
    if (distance > .2f) {
        target.transform.LookAt (destination);
        target.transform.position = Vector3.MoveTowards (target.transform.position, destination, speed);
        target.rigidbody.freezeRotation = true;
    }
}

}
}

attach this script to a gameobject and it will follow mouse hit position

do the changes you need to make it work with your project

    var speed = 1;
    var targetRadius = 1;
    private var target : Vector3;
     
    function Start () {
    //    rigidbody.freezeRotation = true;
        target = transform.position;
    }
     
    function FixedUpdate () {
        target.y = transform.position.y;
        if (Input.GetButtonDown ("Fire1")) {
        	var hit : RaycastHit; 
            var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            if (Physics.Raycast (ray, hit)) {
                target = hit.point;         
            }
        }   
     
        if (Vector3.Distance(transform.position, target) > targetRadius) {
                transform.LookAt(target); 
                transform.Translate(0,0,speed);                 
        }
    }