- Home /
Pausing an animation in a parent game object?
How can I pause an animation in a parent game object? I tried the following, but it only works on the game object that the script is attached to...
// Make all animations attached to this gameObject pause & then continue after 3 seconds
for (var state : AnimationState in Animation)
{
state.speed = 0;
yield WaitForSeconds(3);
state.speed = 1;
}
The problem I'm having is that I have multiple enemy characters being animated by a parent game object and whenever the Player collides with one of these enemies the parent animation keeps going so my Player character loses all his health and then loses a life right away ... so what I'm trying to do is when the player collides with an enemy then the enemies parent game object pauses it's animation for a few seconds to give the Player a chance to move out of the way before getting damaged again.
Answer by aldonaletto · Jan 04, 2012 at 02:37 AM
Why don't you stop the animation directly in the enemy hit? Kind of:
Player script (if it's a CharacterController):
function OnControllerColliderHit(hit: ControllerColliderHit){ if (hit.transform.tag == "Enemy"){ // call the function PauseAnimation(time) in the enemy hit: hit.transform.SendMessage("PauseAnimation", 3.0); } }
Enemy script:
function PauseAnimation(time: float){ for (var state : AnimationState in Animation){ state.speed = 0; yield WaitForSeconds(time); state.speed = 1; } }
Thanks for your quick response, but to answer your question.......
Because the animation I want to pause is not attached to the enemy game object, it is attached to the enemy game object's parent. I want to know how to pause an animation in a parent game object.
You could use Send$$anonymous$$essageUpwards ins$$anonymous$$d of Send$$anonymous$$essage - this would call the function in the script attached to the parent or above in the hierarchy. But try @flamy's answer: it goes direct to the parent object.
Something occurred to me: you should modify PauseAnimation this way:
function PauseAnimation(time: float){ for (var state : AnimationState in Animation){ state.speed = 0; // stop all animations... } yield WaitForSeconds(time); // wait for time delay... for (var state : AnimationState in Animation){ state.speed = 1; // resume all animations } }The other way would make each animation stop during 3 seconds consecutively.
Answer by flamy · Jan 04, 2012 at 04:09 AM
function OnControllerColliderHit(hit: ControllerColliderHit){
if (hit.transform.tag == "Enemy"){
// call the function PauseAnimation(time) in the enemy hit:
var _parent:GameObject=hit.transform.parent;
_parent.sendMessage(PauseAnimation",3.0);
}
}
Change the on control collider hit function like this, it would be enough..
if the parent u have got with above is not the top most you can change transform.parent to transform.root, it will return the first object in the parent heirarichy!!
hope it helps :)
I tried it and it's not working.... I'll post my code below as a new answer
Answer by replay11 · Jan 05, 2012 at 06:37 AM
Here is the code I'm using that is not working... I'm not getting any errors, but it just doesn't work :( My Player character is a submarine that shoots torpedoes (Missiles).
// pauses animations on the game object and it's parent when colliding with "Player" or "Missile"
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.tag == "Player" || collision.gameObject.tag == "missle")
{
// call the function PauseAnimation(time) in the enemy's parent:
var _parent : Transform;
_parent = transform.parent;
_parent.SendMessage ("PauseAnimation", 3.0);
// Make all animations attached to this gameObject pause & then continue after 3 seconds
for (var state : AnimationState in animation)
{
state.speed = 0;
yield WaitForSeconds(3);
state.speed = 1;
}
}
}
This is the script I'm attaching to the _parent game object...
function PauseAnimation(time: float){
for (var state : AnimationState in Animation){
state.speed = 0;
yield WaitForSeconds(time);
state.speed = 1;
}
}
did u try changing the tranform.parent to transform.root ?!?!
No, i didnt because the parent is the top most game object, but I will try that now and see if that fixes it. Thanks againfor your help! I really really appreciate it. :)
Ok, I just tried changing to transform.root and that didn't work either so I'm now going to try changing to Send$$anonymous$$essageUpwards ins$$anonymous$$d of Send$$anonymous$$essage as suggested above and see what happens.
I tried changing to Send$$anonymous$$essageUpwards and that did not work either so I'm a bit stumped at the moment. The only way so far that I was able to get it to work was to add another collider onto the parent game object (which then limits the parent to only having one child and reduces game performance due to another collider being used) and then attaching the following script to the parent game object...
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.tag == "Player" || collision.gameObject.tag == "missle")
{
// $$anonymous$$ake all animations attached to this gameObject pause & then continue after 3 seconds
for (var state : AnimationState in animation)
{
state.speed = 0;
yield WaitForSeconds(3);
state.speed = 1;
}
}
}
Does anyone know why Send$$anonymous$$essageUpwards doesn't work or why targeting transform.parent or transform.root doesn't work in my case? I think this would be very helpful to figure out for many people and seems like it should be something relatively straightforward to implement... but it hasn't been so far for me. It's times like this when I wish that Unity had something similar to Flash's target icon for ActionScript where you just click on the target icon in the code inspector and then a window pops up with everything in your project and you just select the item you want to target.
I finally got it working! In all the scripts above the line of code that says...
for (var state : AnimationState in Animation)
Well, apparently the "A" in Animation must be a lowercase "a" in order for it to work because when it is a capitol letter "A" the word "Animation" highlights in blue in $$anonymous$$onoDevelop so it must mean something else? I'm not certain. What I do know is that when I changed the letter "A" in the word "Animation" to a lowercase "a"... resulting in "animation" ... when I made this change in all places where that for loop occurs then magically everything began working exactly as I wanted it to!
Answer by replay11 · Jan 07, 2012 at 02:21 PM
I got it to work finally. The problems was with the capitol letter "A" used in the word "Animation" above. It has to be a lowercase "a" instead in order for the script to work.