Disable a UI element when ray doesn't hit it anymore.
I have a hand icon, I wan't it to be active when I point my cursor at a object with ether of these two tags: Object and Interactable. But it doesn't work, it does disable the other one, but even when I look at the object the other one doesn't become enabled. Got this much done:
void Update()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Interactable" || hit.collider.tag == "Object")
{
crosshairDot.GetComponent<Image>().enabled = false;
crosshairHand.GetComponent<Image>().enabled = true;
}
if (hit.collider.tag != "Interactable" || hit.collider.tag != "Object")
{
crosshairDot.GetComponent<Image>().enabled = true;
crosshairHand.GetComponent<Image>().enabled = false;
}
}
}
}
Answer by Ilseroth · Mar 28, 2018 at 05:02 PM
The second if statement will always be true, since the object will always be either, not an interactable or not an object. I'm pretty sure you meant that to be an && statement, so that if it is both not an interactable AND not an object then it'll switch back to a dot.
(also the 2nd if might want to be an Else if statement)
Your answer
Follow this Question
Related Questions
The casted ray is persisting. 1 Answer
Raycast is offsetting object 1 Answer
Switching from object to object with Raycast Problem 1 Answer
Do touch on point where ray hits 0 Answers