- Home /
How can I select a UI element with a raycast from another UI element?
I have buttons arranged in a circle and a dial in the middle that rotates and points outward. I want to raycast from the dial to select a button. I don't know what the best way to cast rays between UI elements is, though.
I know Graphics.Raycast is used for something similar to this, but I'm not sure if it's what I'm looking for here or not. Can anyone shed some light on raycasts between UI elements?
Answer by WideEyeNow · Jul 18, 2017 at 07:22 PM
I'm pretty sure the Raycast for UI is only from Camera to UI. If you only have buttons around a dial, you could set the rotation of the dial specific to said button. i.e. if dial rotation is >45 && <89 button 3 IsSelected.
I had thought about that. It just didn't seem as elegant as it could be. I have eight buttons that can be enabled or disabled, so it would be lots of branches. I thought Unity had to have a better way to handle the situation.
Answer by HattyCastles · Jul 18, 2017 at 10:39 PM
I ended up doing something similar to what WideEyeNow recommended for now (the code below). I would still love to know, though, if there's any solution for shooting rays inside of a canvas.
private Button GetPointedAt( ) {
float r = transform.eulerAngles.z;
float bracket = Mathf.Round(r / 45f);
if (bracket == 8) {
bracket = 0;
}
if (bracket < 7) {
return null;
} else {
return buttons [(int)bracket];
}
}
Your answer
Follow this Question
Related Questions
Clickable object behind clickable HUD element with Unity UI 2 Answers
Canvas: On hovering over Buttons: Wrong Buttons activated 2 Answers
Canvas Buttons not working after LevelLoad 5 Answers
Event trigger for instantiated objects 0 Answers
Google VR SDK - Distortion Correction makes World Space Canvas render on top of everything else. 0 Answers