This question was
closed Apr 11, 2016 at 05:15 PM by
caylkadam for the following reason:
İt works
Deselecting all the other Units when selecting a Unit
Hi, i am trying to make a castle siege game. I have many instance of units to select in the game. I want to deselect all the units when clicked on one unit so only that unit is moving when clicked o movebutton. Here is how i approached to the problem. Right now either all move together or some move on their own. I couldn't figure out how does it act at the moment.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UnitSelectArcher : MonoBehaviour {
Ray ourRay;
RaycastHit hit;
public Camera MainCamera;
public bool unitSelected=false;
public bool unitSelected2=false;
public Button MoveButton;
public Button AttackButton;
public Canvas ActionMenu;
public string unitName="Archer";
float targetX;
float targetZ;
bool Moving=false;
public float speed=2f;
public GameObject kup;
public Transform spawnPoint;
// Use this for initialization
void Start () {
MainCamera = MainCamera.GetComponent<Camera> ();
MoveButton = MoveButton.GetComponent<Button> ();
AttackButton = AttackButton.GetComponent<Button> ();
ActionMenu.enabled = false;
targetX=transform.position.x;
targetZ = transform.position.z;
}
// Update is called once per frame
void Update () {
ourRay = MainCamera.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ourRay, out hit, Mathf.Infinity)
) {
Debug.Log ("{0}"+ hit.transform.position);
Debug.DrawRay(ourRay.origin, ourRay.direction*2000, Color.magenta, 10);
}
if (hit.collider.gameObject.name == "Archer"&& Input.GetMouseButtonDown (0)) {
unitSelected = true;
if (name == "Arhcer1"||name=="Archer2"||name=="Archer3"||name=="Archer0")
{ unitSelected = false;
Moving = false;
}
}
if (hit.collider.gameObject.name == "Archer0"&& Input.GetMouseButtonDown (0)) {
unitSelected = true;
if (name == "Arhcer1"||name=="Archer2"||name=="Archer3"||name=="Archer")
{ unitSelected = false;
Moving = false;
}
}
if (hit.collider.gameObject.name == "Archer1"&& Input.GetMouseButtonDown (0)) {
unitSelected = true;
if (name == "Arhcer"||name=="Archer2"||name=="Archer3"||name=="Archer0")
{
unitSelected = false;
Moving = false;
}
}
if (hit.collider.gameObject.name == "Archer2"&& Input.GetMouseButtonDown (0)) {
unitSelected = true;
if (name == "Arhcer"||name=="Archer1"||name=="Archer3"||name=="Archer0")
{
unitSelected = false;
Moving = false;
}
}
if (hit.collider.gameObject.name == "Archer3"&& Input.GetMouseButtonDown (0)) {
unitSelected = true;
if (name == "Arhcer" || name == "Archer1" || name == "Archer2" || name == "Archer0") {
unitSelected = false;
Moving = false;
}
}
if (Physics.Raycast(ourRay, out hit, 100.0f)
//&& Input.GetMouseButtonDown (0)
//&& hit.collider.gameObject.name == "Archer"
)
print("Found an object - distance: " + hit.distance +hit.transform.position);
if (unitSelected == true) {
ActionMenu.enabled = true;
MoveButton.onClick.AddListener (MoveButton_Clicked);
}
// The Object clicked on moves to the Target Location
if (Input.GetMouseButtonDown (0) && Moving == true) {
targetX = hit.transform.position.x;
targetZ = hit.transform.position.z;
}
/* Deneme aşamasında ama yemiyor
if (Input.GetMouseButtonDown (0)) {
if (hit.collider.transform.name == name)
unitSelected = true;
else {
unitSelected = false;
Moving = false;
}
}
*/
if (Moving == true) {
//Debug.Log (string.Format ("targetX:{0}, targetZ:{1}", targetX, targetZ));
if (targetX > transform.position.x)
{
Vector3 temp = new Vector3 (0.1f, 0, 0);
transform.position += temp;
}
else if (targetX < transform.position.x)
{
Vector3 temp = new Vector3 (0.1f, 0, 0);
transform.position -= temp;
}
if (targetZ > transform.position.z) {
Vector3 temp = new Vector3 (0, 0, 0.1f);
transform.position += temp;
}
else if (targetZ < transform.position.z)
{
Vector3 temp = new Vector3 (0, 0, 0.1f);
transform.position -= temp;
}
unitSelected = false;
}
}
public void MoveButton_Clicked()
{
Moving = true;
}
public void OnMouseDown()
{
unitSelected2= true;
print ("Seçildi"+unitSelected2);
}
}
Comment