//Select a unit or deselect all units public void SelectUnit() { RaycastHit hit; //Cast a ray if (Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, mask) && hit.point != target.position) { string hitMask = LayerMask.LayerToName(hit.transform.gameObject.layer); //if ground is hit, deselect all units if (hitMask == "Ground") { SelectAll(false); selectedAI.Clear(); //Otherwise select the unit that was clicked on } else if (hitMask == "Unit") { SelectAll(false); selectedAI.Clear(); AIWithPathfinding ai = hit.transform.GetComponent <AIWithPathfinding>(); //ai.target = target; ai.selected = true; selectedAI.Add(ai); SelectAll(true); print("Object selected"); } } }
/** * Regular update */ void Update() { //Start drawing selection box on left mouse down if (Input.GetMouseButtonDown(0)) { settingPatrol = false; //settingAttackMove = false; start_box = Input.mousePosition; drawBox = true; } //If user presses "s", top the current movement if (Input.GetKey(KeyCode.S)) { settingPatrol = false; //settingAttackMove = false; StopAll(); } if (Input.GetKey(KeyCode.H)) { settingPatrol = false; //settingAttackMove = false; HoldPositionAll(); } if (Input.GetKey(KeyCode.P)) { settingPatrol = true; //settingAttackMove = false; } /*if(Input.GetKey (KeyCode.A)) * { * settingAttackMove = true; * settingPatrol = false; * }*/ //Keep drawing selection box on left mouse down if (Input.GetMouseButton(0)) { end_box = Input.mousePosition; makeBox(); //printy(GUIUtility.ScreenToGUIPoint(Input.mousePosition)); //print(boundbox); } //On mouse button up, select all unts within the box if (Input.GetMouseButtonUp(0)) { //Disable box drawBox = false; //Get end box corner end_box = Input.mousePosition; //Create box makeBox(); //Iterate through all selectable objects and check which ones are in the box GameObject[] csel = GameObject.FindGameObjectsWithTag("Selectable"); for (int i = 0; i < csel.Length; i++) { //Convert object position to screen coordinated Vector3 objectlocation = Camera.main.WorldToScreenPoint(new Vector3(csel[i].transform.position.x, csel[i].transform.position.y, csel[i].transform.position.z)); //If the object falls inside the screen box set its state to selected so we can use it later if (boundbox != null && boundbox.Contains(objectlocation)) { //csel[i].SendMessage("setisSelected", true); AIWithPathfinding ai = csel[i].GetComponent <AIWithPathfinding>(); //ai.target = target; selectedAI.Add(ai); } SelectAll(true); } } }