so I want to drag select objects RTS style. The simplest solution seems to be to use rays to figure out where the player has dragged his mouse and then make a trigger collider there and use ontriggerstay to collect all the objects in bounds.
Am I wrong? I understand that obviously ontriggerstay uses some kind of function/functions to find all objects inside the bounds of another object but I don’t want to rewrite a lot of code that already exists and is complicated just so i can have it without an empty collider object or to use it once per call.
I think possibly using co-routines is the answer but yield return still happens every frame and wait for seconds doesn’t really do what I want either.
I want something like yield wait for bool to be true or really just wrap the code in a function (but of course you can’t have it inside one) so that it only runs on frames I tell it to and isn’t placed in the physics update thread every frame.
I think the way I would do this is capture the x,y of the mouse position OnPointerDown and then again OnPointerUp. Then, have a list/array of objects with the tag you are looking for. If those objects’ positions are within those two points (assuming you’re making a rectangle/square) then select those targets.
EDIT: Made code so you can drag from top left corner to bottom right, or bottom right to top left.
using UnityEngine;
using System.Collections;
public class Selector : MonoBehaviour {
Ray ray;
RaycastHit hit;
public GameObject prefab;
public Vector3 point1;
public Vector3 point2;
public Vector3 point3;
public Vector3 point4;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnDrawGizmos(){
Gizmos.color = Color.blue;
Gizmos.DrawLine (point1,point2);
Gizmos.DrawLine(point1,point2);
Gizmos.DrawLine(point2,point3);
Gizmos.DrawLine(point3,point4);
Gizmos.DrawLine(point4,point1);
}
void Update () {
if (Input.GetKeyDown (KeyCode.Mouse0)) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit)) {
GameObject[] EditorObjects = GameObject.FindGameObjectsWithTag ("Editor");
foreach (GameObject editorObj in EditorObjects)
Destroy (editorObj);
GameObject obj = Instantiate (prefab, new Vector3 (hit.point.x, hit.point.y, hit.point.z), Quaternion.identity) as GameObject;
point1 = obj.transform.position;
obj.name = ("Point_1");
obj.GetComponent<Renderer> ().material.color = Color.red;
}
}
if (Input.GetKeyUp (KeyCode.Mouse0)) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit)) {
GameObject obj2 = Instantiate (prefab, new Vector3 (hit.point.x, hit.point.y, point1.z), Quaternion.identity) as GameObject;
obj2.name = ("Point_2");
obj2.GetComponent<Renderer> ().material.color = Color.blue;
point2 = obj2.transform.position;
GameObject obj3 = Instantiate (prefab, new Vector3 (hit.point.x, hit.point.y, hit.point.z), Quaternion.identity) as GameObject;
obj3.name = ("Point_3");
obj3.GetComponent<Renderer> ().material.color = Color.yellow;
point3 = obj3.transform.position;
GameObject obj4 = Instantiate (prefab, new Vector3 (point1.x, hit.point.y, hit.point.z), Quaternion.identity)as GameObject;
point4 = obj4.transform.position;
GameObject[] playerList = GameObject.FindGameObjectsWithTag ("Player");
foreach (GameObject player in playerList) {
Vector3 playerPos = player.transform.position;
if (playerPos.x > point1.x && playerPos.x < point3.x && playerPos.z < point1.z && playerPos.z > point3.z ||
playerPos.x < point1.x && playerPos.x > point3.x && playerPos.z > point1.z && playerPos.z < point3.z) //Left top corner to right bottom corner
Debug.Log (player.name + "IS within the bounds of the four points");
else {
Debug.Log (player.name + "IS NOT within the bounds of the four points");
}
}
}
}
}
}