- Home /
Rts touch select unit
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;
}
}
}
}
}
Answer by misher · Jul 08, 2019 at 07:47 AM
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.
First, thank you for your interest, yes I have multiply gameobjects with PlayerTouch component on them
and I tried What may be understood by your explanation but I find many mistakes Can you explain more
I updated with more details. You can start crafting some code following the idea. Post your code then again here, so we can work on it if needed...
Answer by thecaesar · Jul 08, 2019 at 10:42 AM
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;
}
}
}
}
}
Replace if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
with: if (Input.Get$$anonymous$$ouseButtonDown(0)) {
also you should work on the component of a hit not on the manager GO:
var touched = hit.collider.GetComponentInParent ();
if (touched != null && selected != touched) {
selected.Deselect() or enabled = false; // according to your implementation
selected = touched;
selected.Select() or enabled = true;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Unity 2D Touch Controll via on screen button for Mobile 0 Answers
Distribute terrain in zones 3 Answers