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?
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.
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.