- Home /
Targeting system, can select but won't deselect...
Hi guys, I'm relatively new to Unity and c#. My question is regarding an MMO style targetting system similar to that of wow. I am able to select the target currently but cannot for the life of me workout how to deselect that target when another is selected. The results i have found are either old and the video responses are no longer available or unasnwered.
Here is my code so far.
using UnityEngine; using System.Collections;
public class Targeting : MonoBehaviour {
public bool isTarget = false;
RaycastHit hit;
Ray ray;
public Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1")) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, 100)) {
if(hit.transform.tag == "Enemy"){
target = this.hit.transform;
ApplyTarget (target);
target.GetComponent<Targeting>().isTarget = true;
} else {
target.renderer.material.color = Color.red;
target.GetComponent<Targeting>().isTarget = false;
}
}
}
}
void ApplyTarget(Transform theTarget){
theTarget.renderer.material.color = Color.green;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
setting a bool on another object's animator by player's raycast 2 Answers
How can I make that the interactable raycast shoot will not pass through some objects like doors ? 1 Answer
How can i use raycast hit to detect object by the object tag name ? 1 Answer