# Unity RTS Multiple Selection Approacah

I've recently solved the single selection problem but now I want to do multiple selections. I know I need to find two points with the mouse position to draw a box but it just won't work for me.

    void Update()
{
if (Input.GetMouseButtonDown(0))
{
bool dragging = true;
Debug.Log("Multiselect active");
MultiSelect(dragging);
}
}
private void MultiSelect(bool dragging)
{

mouseDownPoint = Input.mousePosition;
if (Input.GetMouseButtonUp(0) && dragging)
{
mouseUpPoint = Input.mousePosition;
}
Debug.Log("First pos: " + mouseDownPoint + " Second pos: " + mouseUpPoint);
dragging = false;

}



What is a better way to achieve what I'm trying? If someone can tell me a way that can also detect any units inside the area that'd be nice too.

• What about the code you've written "just won't work"? What output/behaviour are you expecting, and how does it differ from what you have? Oct 5, 2019 at 20:53
• I expect to be able to drag my mouse and to get values returned for the point where I started and the point I stopped. However the values for where I stopped just don't make any sense.
– Kiyo
Oct 6, 2019 at 15:58

You get the start point and end point but you also want to calculate the height and width.

public class CameraSelections : MonoBehaviour
{
public static Rect selection = new Rect(0, 0, 0, 0);
private Vector3 startClick = -Vector3.one;
public Texture2D selectionHighLight = null;

private void LateUpdate()
{
StartDrag();
}
private void StartDrag()
{
if (Input.GetMouseButtonDown(0))
startClick = Input.mousePosition;
else if (Input.GetMouseButtonUp(0))
startClick = -Vector3.one;

HandleSelectionArea();
}
void HandleSelectionArea()
{
if (Input.GetMouseButton(0))
{
selection = new Rect(startClick.x, Screen.height - startClick.y, Input.mousePosition.x - startClick.x, (Screen.height - Input.mousePosition.y) - (Screen.height - startClick.y));
if (selection.width < 0)
{
selection.x += selection.width;
selection.width = -selection.width;
}
if (selection.height < 0)
{
selection.y += selection.height;
selection.height = -selection.height;
}
}
}
private void OnGUI()
{
if (startClick != -Vector3.one)
GUI.DrawTexture(selection, selectionHighLight);
}

}


then in your player or selectable you can set the Selected bool to true if the selectable is inside the Rect

public class Player : MonoBehaviour
{
public bool Selected=false;
void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position);
camPos.y = Screen.height -camPos.y;

//if inside the cameras drag selection area then mark player as selected
Selected = CameraSelections.selection.Contains(camPos);
}
}
}


• Thank you for the box making script. However, my unit controller operates from solely one script in the camera. I want to figure out how to check if a unit is in that box area.
– Kiyo
Oct 6, 2019 at 17:19
• the CameraSelections script goes on the camera and creates the rect and the player script will take that info and automatically toggle if it is selected within the area here Selected = CameraSelections.selection.Contains(camPos); you can if you wanted to solely use the camera script to do the toggle but should be on the player and if there is multiple they will all toggle themselves selected if they are within the selection area Oct 6, 2019 at 19:52
• I see but even if I put the code in the unit it still doesn't seem to work...? I'm starting to lose patience with this method due to the fact I have deadlines to meet. I initially thought about putting a list in the main unit controller where it'd iterate through every unit in the scene and add it in if the positions were in the area of a ray cast. But I don't even know how to access every object in the scene...
– Kiyo
Oct 9, 2019 at 20:01
• Could it be due to the fact that the unit controller only selects objects of tag "Unit"?
– Kiyo
Oct 9, 2019 at 20:12
• DISREGARD THAT I WAS AN IDIOT AND FORGOT TO BIND THE SCRIPT TO THE UNIT! SORRY!
– Kiyo
Oct 9, 2019 at 20:24