- Home /
GameObject.collider.Raycast to ignore collision between other objects?
Hi,
I have a few questions about using collider.raycast. I have a code snippet below that I'm using for a simple menu for touch devices. In the scene, I have one visible main menu button, and 3 hidden buttons for easy, normal and hard. The way it works, i press the main menu button, it disappears and the 3 other button appears.
The problem I have right now is that in the scene, if I position the main menu button on top of any of the other invisible buttons, pressing the menu button will also activate the collider of the ones below it. I've tried using the layer collision matrix but i think it only applies if I'm using physics.raycast. The only I fixed it is by positioning it so it doesn't sit on top any of the other buttons(even if it's hidden).
So my first question is there a way to ignore other game colliders when pressing a button using collider.raycast?
If using Physics.Raycast is the option to choose, it means I have to use tags to assign to each of the menu buttons right? I kept being told to avoid using tags since it will affect performance. Is that true?
Hope someone can help.
Thanks.
Hakimo
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetMouseButtonDown(0)) { if (startGame.collider.Raycast(ray,hit,100)) { // Main Menu Play button ButtonPressed(); yield WaitForSeconds(0.1); gameEasy.active = true; gameMedium.active = true; gameHard.active = true; startGame.active = false; } if(gameEasy.collider.Raycast(ray,hit,100)) { Application.LoadLevel(1); } if(gameMedium.collider.Raycast(ray,hit,100)) { Application.LoadLevel(2); } if(gameHard.collider.Raycast(ray,hit,100)) { Application.LoadLevel(3); } }
Answer by Hakimo · Mar 29, 2012 at 06:38 AM
Hi hijinxbassist, I just sorted it out thanks. I didn't have to change any conditions but more to arrangements of the code. In my original code, I placed the if(Input.GetMouseButtonDown(0)) outside my button if condition statements. What i did to fix it was arrange it like:
if (startGame.collider.Raycast(ray,hit,100)) { // Main Menu Play button if(Input.GetMouseButtonDown(0)) { // Put the input condition inside instead ButtonPressed(); yield WaitForSeconds(0.1); gameEasy.active = true; gameMedium.active = true; gameHard.active = true; startGame.active = false; } }
I had to put the input.getMousebutton down in all of my button conditions. This fixes the collision issue. Thanks again.
-Hakimo
Your answer
Follow this Question
Related Questions
collider.Raycast problem 2 Answers
when laser pointer from controller hits a game object a popup text should appear? 0 Answers
Collider.Raycast is complaining that my ray does not have a normalized direction 1 Answer
Drag mouse Up or Down to play or reverse animation 0 Answers
How to stop RaycastHit2D from using the last object in the hierachy? 1 Answer