I have a problem when I select unit all units selected
public class RayTarget: MonoBehaviour {
Touch touch;
Vector3 touchPosition;
public bool selected = false;
public GameObject Player;
void Start ()
{
gameObject.GetComponent<PlayerTouch>().enabled = false;
}
private void Update(){
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction);
if (hit.collider.tag == "Plane") {
selected = !selected;
if (selected == true)
{
Player.GetComponent<PlayerTouch>().enabled = true;
}
else
{
Player.GetComponent<PlayerTouch>().enabled = false;
}
}
}
}
}
Do not use tags in this case. I imagine you have multiple gameobjects
with PlayerTouch
component on them. You need to store your current selected object in a variable. On raycast hit check if a collider has PlayerTouch
component (clicked over a unit) like this for example: var touched = hit.collider.GetComponentInParent(PlayerTouch);
if it is not null, deselect previous one (if there is one) and select the touched and store it as current.
Other Details:
There should be a component on each unit, call it UnitSelection
for example. It is responsible for changing user graphics so it appear selected or deselected, so it is gonna have 2 public methods to do it. Then, there is a selection manager. It is responsible for maintaining currently selected unit (if you want to select only one unit at the time) and for selecting/deselecting units. When user touch the screen (use also Input.GetMouseButtonDown(0)
in Editor to test it out), the manager perform a raycast and check if we hit an object with UnitSelection
component. If you have already a unit selected and stored in a variable in our manager, you will deselect it first (assuming you click on different unit or you clicked outside any unit. Then you select a new unit (using mentioned public methods of a UnitSelection
script) and store it as current.
This Code is working when I use it for test in Editor for mouse input in method private void OnMouseDown()
but if write this code in: void Update() all units Selecting So i think the problem is a method for android like OnMouseDown().
please if you have a solution reply
The code :
using UnityEngine;
using System.Collections;
public class Select: MonoBehaviour {
Touch touch;
public bool selected = false;
//public GameObject Player;
void Start ()
{
gameObject.GetComponent<PlayerTouch>().enabled = false;
}
private void Update(){
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction);
//Select Hool
if (hit.collider.GetComponentInParent<PlayerTouch> ()) {
selected = !selected;
if (selected == true)
{
gameObject.GetComponent<PlayerTouch>().enabled = true;
}
else
{
gameObject.GetComponent<PlayerTouch>().enabled = false;
}
}
}
}
}