I’m trying to figure out how to create a basic Unit Selection/Mobilization structure in Unity, I’m not too experienced with C# and it’s my first week with Unity, I’ve been following the BurgZerg arcade tutorials for a RPG game, and I’ve learned a few things such as how to use the Transformations to create a basic AI, etc.
I’m trying to figure out how to get a selectable unit although, perhaps I could get some help or be pointed in a good direction. I want this script to allow me to select a unit, and move it via right-clicking. Similar to an RTS type game. I’ve checked you-tube but no luck.
Ok, so lets run through it.
You want to select a unit:
Using raycasts you can check if a mouse click has hit a unit. When it does, cache a reference to that unit; in your script you can have a variable like “SelectedUnit”.
You want to click to move it:
When your “SelectedUnit” variable has a reference to a unit, then the next click you make will be your movement target. Use another raycast for this, to get the position on the terrain.
You want the unit to move to the clicked position:
Using vectors and methods of the Vector3 class, you can calculate the direction the unit has to move in order to reach the target. MoveDirection = UnitPosition - TargetPosition.
In time you can make more complex systems such as multiple selected units using arrays and squad behavior when moving.
Take your time to look at each section individually and build up one bit at a time. Read the documentation too!
Good luck.
Thank you for this information, looks like it’s time to study on ray-casts.