I am trying to play an animation when the player is in a colider and he/she presses a button e.g. “e”
I tried to get this script to work:
But this does not work, Any ideas?
By ‘in a collider’, what do you mean? There are several things which could be going wrong here.
For starters, the physics engine will only detect collisions (and send the messages you need for this kind of behaviour) if there is at least one non-kinematic rigidbody involved in the collision. Of course, a non-kinematic rigidbody will immediately try to extract itself from inside any collider it finds itself within- the ‘OnCollisionStay’ message is referring to objects remaining in contact, not necessarily intersecting.
In this case, you should probably be using a trigger volume. Set the collider you are trying to test to ‘isTrigger’, and then use the ‘OnTriggerStay’ message, instead.
If your character is not controlled by a rigidbody, you will have to attach a rigidbody component to it and set it to ‘isKinematic’. This will allow it to receive trigger messages (although it will still not receive collision messages from other kinematic or static colliders).