x


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?

more ▼

asked Jun 16 '12 at 07:34 AM

sean1231 gravatar image

sean1231
0 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Jun 16 '12 at 07:40 AM

syclamoth gravatar image

syclamoth
15k 7 15 80

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.GetKey("e"))

AnimationSource.animation.Play(anim);

}

Just attatch this code to the trigger and attatch the animation to a gameObject.

Jun 17 '12 at 12:23 AM sean1231
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3926
x3418
x1765
x819
x230

asked: Jun 16 '12 at 07:34 AM

Seen: 565 times

Last Updated: Jun 17 '12 at 12:23 AM