Hi, im doing a 2D plataformer game, and i’m trying to make a box to be destroyed when my mouse cursor ,that is a small sphere, ( that is triger ) touch it. My code is working, but i have to move my cursor like crazy to the box be destroyed. At first i wanted that the box be destroyed when my mouse touch the box and i press a key.
My code is this:
Maybe someone with more experience in Unity can help me, why my trigger dont activate as soon as i touch the box? If the trigger activate allways my sphere (mouse cursor) was inside the box it will be easier, please some help ?
Not sure why this could be happening. Is the sphere and the box on the exact same axis? If the box is rotated the sphere may be only hitting a certain part according to the alignment, since it’s a 2d platformer. I would say a better way to go (if you don’t need the actual 3d sphere) is to use a Raycast with a long distance and use ScreenPointToRay(Input.mousePosition); from the camera instead.
I was having some trouble with colliders a while back: the issue was, ‘enter’ only runs for the frame when the entrance takes place. So basically, you’re having to touch the collider during the same frame you press down your key.
My first thought would be to add in a boolean: OnTriggerEnter sets it to true, OnTriggerExit sets it to false. Place your key detection in the update function - if the boolean is true and the key is pressed, destroy object.
That said…I’d probably play around with raycasting for this, I think Jep’s right about that being an easier way to go.