hey, i’m following a new tut for a RTS game. But there’s only explained how to select each single object. So i fighted me through several tuts, and found a script attached to a unit prefab which detects if it’s selected by drawing a rectangle for multi selection.
It basically works but i haven’t found a way to read in those units in a List in a script which is attached to the maincam with witch the rectangle is drawn and there i declared the List.
This script is used with the main cam :
public Texture aTexture;
public static Rect selection = new Rect(0,0,0,0);
private Vector2 _box_start_pos = Vector2.zero;
private Vector2 _box_end_pos = Vector2.zero;
public string Pl_tag = "Player";
public List<GameObject> selectedObj = new List<GameObject>();
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Debug.DrawRay(ray.origin, ray.direction * 100.0f, Color.yellow);
// Called while the user is holding the mouse down.
if(Input.GetKey(KeyCode.Mouse0))
{
if (Physics.Raycast(ray, out hit ))
{
//Debug.Log(hit.collider.tag );
if(hit.collider.tag == Pl_tag)
{
var obj = hit.collider.gameObject;
if(selectedObj.Contains(obj) == false)
{
selectedObj.Add(obj);
}
Debug.Log(Pl_tag +" hit");
}
}
else
{
selectedObj.Clear();
Debug.Log("");
}
// Called on the first update where the user has pressed the mouse button.
if (Input.GetKeyDown(KeyCode.Mouse0))
_box_start_pos = Input.mousePosition;
else // Else we must be in "drag" mode.
_box_end_pos = Input.mousePosition;
}
else
{
// Handle the case where the player had been drawing a box but has now released.
if(_box_end_pos != Vector2.zero && _box_start_pos != Vector2.zero)
//HandleUnitSelection();
// Reset box positions.
_box_end_pos = _box_start_pos = Vector2.zero;
}
/// <summary>
/// Draws the selection rectangle if the user is holding the mouse down.
/// </summary>
void OnGUI()
{
// If we are in the middle of a selection draw the texture.
if(_box_start_pos != Vector2.zero && _box_end_pos != Vector2.zero)
{
// Create a rectangle object out of the start and end position while transforming it
// to the screen's cordinates.
selection = new Rect(_box_start_pos.x, Screen.height - _box_start_pos.y,
_box_end_pos.x - _box_start_pos.x,
-1 * (_box_end_pos.y - _box_start_pos.y));
// Draw the texture.
GUI.DrawTexture(selection, aTexture);
}
}
public static float InvertMouseY(float y)
{
return Screen.height - y;
}
This script is attached to the GameObject prefab called Unit:
public GameObject thistransform;
public bool selected = false;
private Renderer rend ;
void Start()
{
Renderer rend = thistransform.GetComponent<Renderer>();
}
void Update ()
{
Renderer rend = thistransform.GetComponent<Renderer>();
if(rend.isVisible && Input.GetMouseButtonUp(0))
{
Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position);
camPos.y = NewBehaviourScript.InvertMouseY(camPos.y);
selected = NewBehaviourScript.selection.Contains(camPos, true);
}
if(selected)
{
rend.material.color = Color.red;
}
else
rend.material.color = Color.green;
}
So i would like to achiefe to read this game objects in the List selectedObj. If i understand it right selection.Contains(camPos, true) does return only Vector3 of each object ?
I’ve tried very much but i’m out of any idea now.
I hope that all isn’t to confusing the way i explained it here (i’m german)
so many thanks previously !!!