- Home /
Play annimation on collision not working.
I am very new to scripting here in unity. I have a script that is suppost to play when you collide into a tagged game object. The only problem is my script does not work. The player walks on the object and animation does not play. Player is suppost to walk on it making the platform go down slowly. (Going up causes the platform to go throught the character) Here is my script.
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag == "Elevator")
{
animation.Play("Down22");
}
}
I am probably doing something very wrong. I cannot figure it out. Like I said I am new to unity scripting.
Answer by MC HALO · Apr 14, 2011 at 08:15 PM
Ok this normally works for me. as you can see that at the moment your tag is the elevator, what this means is that once the player hits the elevator the animation will play. Now this does not work because when you collied with the object it will say the player does not have the animation attached to it. so what you need to do is attach this script to the Elevator object and add a tag to the main player called "Player". once you have done that in the script change the following:
var CloseCollision : boolean = false;
function OnCollisionEnter(collision : Collision)
{
if(CloseCollision == flase){ if (collision.gameObject.tag == "Player"){
animation.Play("Down22");
CloseCollision = true; } else{ CloseCollision = false; // read the description below to understand what is going on } } }
well if you notice on the top of the script i created a boolean called CloseCollision. Now what this does is it allows you to turn you collision on and off. So for example: now what will happen is if the player collieds with the elevator object it will play the animation then set the Collision boolean to true. once it is set to true the collision will stop this is because on the top of the following line "if (collision.gameObject.tag == "Player"){" i have add the following line "If(CloseCollision == false){" this line makes everything work. what it is saying to the system when you run the game check and see if the boolean is set to false so the box in the inspector mode should be unchecked if it is and if the player collides with the object it will play the animation. Now after the animation is over i said to the system "hey look" if this guy has hit the wall set the boolean to true. Once it is true the collision will no longer do anything that's because it will only run if the boolean check box is set to false. Now the "else" plays an important part if we have no collided with the object it will make the boolean remain false, that's all that does. This lines of code should now only make the animation play once :). make sure the script is attached to the elevator object not the player. i hope this helps. Oh yes makes sure you have the elevator animation attached to the elevator object in unity.
It works $$anonymous$$y script was bad as expected. Now after i walk on to it it does the animation, however I walk when on the object and the animation restarts... I am sure I can find a way to stop that from happening. So thanks.
Cool, I am sure this will work:) I did it such a wimpy way but it works. I am sure this is more eficient though. I will try it as soon as I can.
Your answer
Follow this Question
Related Questions
Collision detection problem 1 Answer
Collision with animations 0 Answers
How to stop an animation on collision 0 Answers
Tags returning true before collision 2 Answers
Trigger animtaion when hit 1 Answer