0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$
    – DMGregory
    Oct 5 '19 at 20:53
  • \$\begingroup\$ 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. \$\endgroup\$
    – Kiyo
    Oct 6 '19 at 15:58
1
\$\begingroup\$

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);        
    }
}
}

unit selection

\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$
    – Kiyo
    Oct 6 '19 at 17:19
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ Oct 6 '19 at 19:52
  • \$\begingroup\$ 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... \$\endgroup\$
    – Kiyo
    Oct 9 '19 at 20:01
  • \$\begingroup\$ Could it be due to the fact that the unit controller only selects objects of tag "Unit"? \$\endgroup\$
    – Kiyo
    Oct 9 '19 at 20:12
  • \$\begingroup\$ DISREGARD THAT I WAS AN IDIOT AND FORGOT TO BIND THE SCRIPT TO THE UNIT! SORRY! \$\endgroup\$
    – Kiyo
    Oct 9 '19 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.