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.

1 Reply

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.

possibly, it's mostly just personal preference; if you are running it with yield return null; it works out to be the same as running a normal method/function every Update(). but you can run it with a waitforseconds instead or just run a method every FixedUpdate() since that's when all the physics stuff is calculated anyways so it should be fine, unless a unit is really fast.

For multiple units you could probably use an Array/List/Dictionary of all the GameObjects and their destination points and just iterate through it instead of having a ton of methods/coroutines running(`Dictionary<GameObject, Vector3>` probably best).

I haven't really had to use something like this though; so whatever works.

It's basically covered in that video that I linked, there are also copies of the scripts he used under the video, the Mouse scripts and the second coroutine script are basically bang on.

raycasting seems fine, if the ray is hitting multiple things, you could iterate through 'till you get ground or some tag you want.
But yea If(mouseclick && unitSelected){ set target position for unit; }
if you put the coroutine on every unit you could have a public targetVector3 in all the unit scripts and you could just set it with a master click script. gameObjectSelected.targetVector3 = clickPosition; again basically what they did in the tutorial.
Main difference will be the public in the mouse script will be a big array of units. And use Vector3.MoveTowards instead of Lerp.