- Home /
Question regarding Raycast hitting UI button object instead of gameobject
Hello everyone.
I am having trouble having my raycast detect a button object and sending a mouse click to it. My goal is to have the button call and play a specific animation on mouseclickup but only doing this when clicking on the button. The button object has an animator with all of the animations I need.
This is my current code for the first button:
void Update() { if (Input.GetMouseButtonUp(0)) { CheckRaycast();
}
}
void CheckRaycast() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000f))
{
if (hit.transform != null)
{
if (hit.transform)
{
Click.Play();
}
if (hit.transform.tag == "B1Tag" && B1 == 1)
{
Button1.GetComponent<Animator>().Play("G2R-Middle");
B1++;
B2++;
B4++;
clickcounter++;
}
The button1 is a public gameobject and also has the B1Tag on it.
The only thing I can think of, is the game camera is being used and it doesn't detect UI objects.
Is there a different way to make this work?
In order, I want button down to disable the animator for that object and show the sprite(button clicked/pressed) then after release(buttonup) it plays the requested animation, following a condition.
It works without a raycast, but I only want it to work for the clicked object.
Any advice is greatly appreciated.
Your answer
Follow this Question
Related Questions
How to implement tokenization of game characters? 1 Answer
Beginner Question: How to get normals from a physics raycast using visual scripting? 0 Answers
looking for a way to set an integer to another number based on a condition. 2 Answers
weighted inventory system 1 Answer
how to make a detect rotation script for negative rotation too 2 Answers