- Home /
How can I make the player collide with the door while the door is opening?
Well, I'm having a hard day with a door script, I finished the script and I made the door open after the user press E but while the door animation is playing the door isn't colliding with the player and I couldn't fix that :( and ideas?
Answer by testure · Jul 17, 2011 at 08:06 PM
Either the door or your character needs a rigid body attached. If neither one has a rigid body, no collision will happen.
As a rule of thumb- if it moves and uses a collider/trigger, put a rigid body on it. If you don't need it to react to physics (which will usually be the case), set the rigid body to kinematic.
On a related note, triggers/colliders that move around require the physics engine to update the scene graph once per frame while the object is moving. This is a huge performance sink. If there's a rigid body attached, this does not happen.
okey i've done that but still can make him stop after he hits the door, i mean , i want the player to stop and cant go through the door thats what i want :(
I did not know that colliders that move without any rigidbody attached can be such a performance bummer. Thank you. I would have thought the opposite. Does it apply to Character Controller as well?
Answer by Zergling103 · Jul 17, 2011 at 07:43 PM
Try coding your character and door movement around the physics engine.
First, make sure the door has a collider.
Second, add a rigidbody to the character's collider. Constrain it's rotation along the x and z axis. Change the collider's physics material to have no friction. Change it's drag to 10. Then the physics engine should move the character out of the way.
When moving around rigidbodies, it is best to use AddForce() or MovePosition() rather than moving the object using it's transform. So if in your code you wrote (in Update()):
transform.position += moveVector;
Instead, write (in FixedUpdate()):
rigidbody.AddForce(moveVector / time.deltaTime, ForceMode.Acceleration);
You can do a similar thing if you want the door to react to the player's presence. Add a rigidbody script to the door, use similar settings for Drag and Friction, and use AddTorque(0,rotationSpeed,0) to make it rotate.
Your answer
![](https://koobas.hobune.stream/wayback/20220612145258im_/https://answers.unity.com/themes/thub/images/avi.jpg)