- Home /
OnMouseDown with colliders behind the object being clicked
I have on screen buttons which are not currently working because there is collider behind them which is flooring for my platformer. Do you know of a possible work around for this issue.
They're just colliders that are called by on mousedown.
Answer by Hexer · Jul 27, 2015 at 03:10 PM
I have stated before that you have to change the transform.position.z value of one of your sprites so that 1 of the sprites don't overlap eachother in 3d space. This time I have added some pictures with instructions to explain how it works.
You can clearly see in the first picture that in the game scene, the green button is infront of the black sprite. But this is not the case in 3d space, in 3d space the black sprite is actually infront of the green button. In the game scene we only changed the order layer to let it seem like it was infront of the black sprite, while this was not the case.
Now when we change that transform.position.z value to something else. In my case the black sprite Z position was -2 while the GreenButton Z position was 0. So I changed the blacksprite Z position to 1 or higher. Now the Black sprite is actually standing behind the greenbutton in 3d space.
You could also change the button Z position to let it come infront of the black sprite, the same principles are used as above.
Now when I changed the Z position value and click on the button, I would see a value (which I set with GuiText ToString(). This indicates that it worked.
$$anonymous$$y objects are already at different z's and it's still not working I have the buttons set 5 and the floors set to 0.
Thank you, know this z position behaviour helped me alot!
Thank you, thank you , thank you! This explained the mysterious behavior I was seeing involving overlapping 2D colliders with different Sort Orders but the same Z. You're a sanity saver!
Answer by AlexGan001 · Jul 28, 2015 at 09:54 AM
I would use a Canvas and the UI Buttons, because they come with their own built in function OnClick() in the editor. If you want the button to stay in one place in the scene, you would select 'World Space' in the Render Mode box.
Answer by Khudere · May 05, 2020 at 04:11 PM
Thanks this helped me! I was trying to play with the layers but forgot to look at the z location