- Home /
Detect collision and prevent intersection
I m trying to detect the collision of two objects (Player and Rock) I want to be able to handle the collision event (OnTriggerEnter) but also prevent the objects from intersecting.
Example:
the player runs into the rock,
the rock knows its the player (OnTriggerEnter or similar)
The player cannot move through the rock, stops right at it's edge as if trying to walk through a wall
The rock does not move
I have tried all variations of Is Trigger, Is Kinematic and both objects have rigid bodies attached but the closest I get is one of the objects spinning off into the abyss. I have even tried using OnTriggerEnter and OnTriggerStay to set the position of the player to the position it was at when the collision first occurred which works but looks like ass.
How can I detect the collision in my script but keep both objects from driving over/through each other?
Thank you very much for any help!
Answer by save · Oct 01, 2011 at 10:33 PM
You mean like OnCollisionEnter? Just don't use trigger and make sure to use a collider on both objects. The player needs to have a rigidbody and be completely controlled by physics (stay away from transform.position and translate).
I don't understand why I post a response here and it doesn't show up with no indication of error at all. This is my second time responding to your answer, lets hope it gets posted....
The player can't be completely controlled by physics as it is controlled by the player. When I implement your answer and the player collides with the rock it spins off into the abyss and I lose control. The player is also controlled using translate as it is the keyboard that is used to control the player.
The effect I am trying to achieve is I move my player forward and he hits a rock. The rock knows what hit it (a player, a bullet, a butterfly) but since I am controlling the player I can just keep walking him right into that rock. He doesn't all of a sudden spin off in some other direction, the rock all of a sudden doesn't spin off in some direction it is just like trying to walk through a wall.. does this make sense or am I not understanding what you are saying?
Thanks!
Final note in regards to the above. I have tried using Freeze position and freeze rotation as well. If all are frozen then I can just walk right through/over the rock.
It doesn't get posted because members with higher karma needs to approve your post before it becomes public.
You can't use translate and physics, the physics engine won't calculate anything else than physic events. You can control the player just via physics, make the inputs fire off physic events ins$$anonymous$$d of translation/positioning.
Have a look at the rigidbody class.
Thank you for both explanations :) looks like using rigidbody is the way to go then.
Your answer
Follow this Question
Related Questions
Collision Handling (Pushout and Sliding) for Customized Collision Object? 0 Answers
Probably another "bullet through paper problem". Jerks, teleports, and other problems 0 Answers
How to check if a collision impact is perpendicular with the velocity of the collider? 0 Answers
Collision not working 1 Answer