RTS movement not working properly

I am fairly new to unity although I have some prior experience coding in C#. I am trying to make a basic RTS game and have been working on getting units to move properly and was looking for some help.

On my unit I have the following script`using UnityEngine;
using System.Collections;

public class UnitSelect : MonoBehaviour {

// Use this for initialization
void Start () 
{

}

public int isHit;
public int isSelected;
// Update is called once per frame
void Update () 
{

	if (Input.GetMouseButtonDown(0)) 
	{
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit = new RaycastHit();

			if(Physics.Raycast(ray,out hit))
		{
			isHit = 0;
			isSelected = 1;
			//Destroy(GameObject.Find(hit.collider.gameObject.name));

		}
	}
	
}

}
`
and then on my camera I have this script
using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour 
{
	public GameObject unit;
	public GameObject unit1;
	public float unitSpeed = 0.00001f;

	private UnitSelect unitSelect;

	void Awake()
	{
		unitSelect = unit.GetComponent<UnitSelect> ();
		unit1 = GameObject.Find("SphereUnit");
	}
	// Use this for initialization
	void Start () 
	{
				
	}
	
	// Update is called once per frame
	void Update () 		
	{
		if (Input.GetMouseButtonDown (1)) 
		{
			Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit2 = new RaycastHit();
			if (Physics.Raycast(ray2, out hit2));
			{


				Debug.Log("Right click works");

				if (unitSelect.isSelected == 1)
				{
					Debug.Log("Working");
					unit1.renderer.material.color = Color.red;
					unit1.transform.Translate( hit2.point * unitSpeed * Time.deltaTime);
				}
			}
		}
	}
}

The problem I am having is that the unit will move only a little way to the destination of the mouse click and then if you click somewhere else it will move in a seemingly random direction. I am also unsure on how to get this to work for multiple units so any suggestions for that would be welcome. I have Googled this issue but all the answers I found where working in Jacascript.

Thanks :slight_smile:

So this is basically a case of Translate not actually doing what you think it will do.

Firstly, it just changes the transform position like doing transform.position += destination and since your multiplying by Time.deltaTime the magnitude of the vector is shrunk down a lot so it only moves a little bit.

Secondly, since you are not getting the vector of the difference between the current point and the target point it’s not a correct vector so that’s possibly what’s causing the second issue.

Another cause could be if the transform is rotating when moving. If rotated, doing the translate may cause the movement to be relative to the wrong direction and thus be just wrong.

As for solving all of these problems and using it with multiple units, check out the Unity tutorial on coroutines.
http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

It’s also worth noting the tutorial is kind of using Lerp wrong so you will probably want to change that or use MoveTowards instead. And add some logic for controlling the speed of the unit.

Hope that helps!