- Home /
How Do I get Units to Move to Different Locations?
I keep coming back to this because this is still an issue to me. I switched my focus to other Unity and C# studies but now I'm back. I've seen some videos. Still stuck.
Situation:
I have a small Terrain and two spheres. I want to give orders to each sphere separately to move to different locations...at the same time though. I want to do it in a "classic" RTS fashion (Ideally, anyway) by left mouse click on unit and then right mouse click on destination.
I'm thinking I need a private bool isSelected switch for the left mouse click and then some sort of raycast to mark the destination. But I can't quite figure out exactly how to write this in code.
I know how to use raycasting...I can make ALL the units go to a single right-click location, that's easy...I just can't make two units go to separate locations at the same time. I've done all this with physics, without physics, with NavMesh and without NavMesh...all of that.
Bummer. I've been spending a lot of time on this but I feel like I'm missing something really basic that's keeping me from accomplishing my task.
Because of the very slow time frames involved, there is no need for me to give movement orders to more than one unit at a time...so I won't need to drag/drop/move a group of objects like you see in most RTS games.
I just need to click on a single unit and move it to a specific location...and each object must move independently of the others.
In your mouse control script, have a variable that holds a reference to the selected unit. That measn that when you select a unit by clicking on it, that variable is filled iwth the unit reference (GameObject, transform, or any script on the unit, for example).
Each unit obviously has some kind of movement script so when you click to move a selected unit, you access it with the variable reference and access it'S movement script, and set the target position., and possible tell the movement script that it's ok to start moving now, search a path etc.
Thanks Cherno, I'm looking at your suggestion right now. I'm going to try to finally figure this out and I appreciate your help.
I don't know how to "accept" your answer since it's a response.
Answer by Topthink · Dec 19, 2016 at 08:55 PM
Cherno. I finally got this to do exactly what I want...I finally understand it.
I'm not sure why it took me multiple questions over a long period for this to finally sink in...but you finally made it understandable for me.
How do I accept your answer since it's not a separate comment?
I'm just as happy as can be. This has been an enormous barrier to me and I finally got it.
If you could see me now, you'd see a happy programmer jumping up and down for joy.
:)
Your answer
Follow this Question
Related Questions
How to make object to avoid other objects? 2 Answers
NavMeshAgent not fully reaching destination, thus it won't delete itself when it gets there 1 Answer
How to generate a NavMesh only with colliders? 5 Answers
Get NavMeshAgents (Enemies) to not bunch up together 3 Answers
how to navigate through buttons? 1 Answer