- Home /
Play animation when in collider and button pressed
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:
function OnCollisionStay()
{
if(Input.GetKey("e"))
animation.Play("Test_an");
}
But this does not work, Any ideas?
Answer by syclamoth · Jun 16, 2012 at 07:40 AM
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).
Thankyou for the help!
If anyone else has run into the same problem, Here is my code:
var anim : String; var AnimationSource = gameObject;
function OnTriggerStay() { if(Input.Get$$anonymous$$ey("e"))
AnimationSource.animation.Play(anim);
}
Just attatch this code to the trigger and attatch the animation to a gameObject.
Your answer
Follow this Question
Related Questions
Activating One Part of An Animation 2 Answers
What am I doing wrong with this attack script? 2 Answers
what is the jump axis for if (Input.GetAxis(" ? ") 2 Answers
How to activate someting if a key is held down? 2 Answers
Animation Script not working. 1 Answer