- Home /
How can i select Units for an RTS layout?
This script right here shows that the mouse has clicked said point, but has done nothing with the units. using UnityEngine; using System.Collections;
public class UnitDetection: MonoBehaviour { public GameObject flag; public float distanceToMap = Mathf.Infinity; public GameObject selection; public Vector3 moveToPosition;
void Awake()
{
flag = GameObject.Find("_Flag");
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
CastRay();
}
}
public void CastRay()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, distanceToMap))
{
SelectableUnit su = hit.transform.GetComponent();
if (su != null && su.unitSide == SelectableUnit.UnitSide.friendly)
{
selection = hit.transform.gameObject;
}
if (su == null)
{
moveToPosition = hit.point;
moveToPosition.y = 0;
flag.transform.position = moveToPosition;
}
}
}
}
I have a seperate script that tells the units apart from friendly, nuetral, and enemy. So thats out of the way. Im just confused on what to do next. Help?
Answer by murkantilism · Mar 15, 2013 at 07:58 PM
I'm confused, what are you asking for help with? Selecting any sort of units, or just your own units?
My suggestion would be:
OnMouseEnter, create a plane (or cube if your RTS game has different heights) that is re-sized as the user drags it across the screen.
OnMouseExit use OnCollisionEnter to find out what the plane collided with. I assume all your units are tagged with "Friendly, Neutral, or Enemy" for the other script, so check for those. If the objects within the plane have any of those tags, add them to a list or dynamic array to keep track of them.
Instantiate a small green circle around the units, or make the units brighter, or do something to tell the player they've been selected.
Delete the plane
Now you've got a bunch of selected units you can give commands to!
Sweet, actually that may work let me try it out first and ill see what happens. Thanks
Your answer
Follow this Question
Related Questions
RTS like Frame on Minimap for user Camera Position with GL not rendering in Multiplayer (MLAPI) 0 Answers
Selecting a selectable using script doesn't seem to work anymore? 1 Answer
RPC Parameter Optimization 1 Answer
RTS Grid and Pathfinding 2 Answers
Best way to simulate Character Controller under Fixed Update? 1 Answer