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.
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;
}
}
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!!
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;
}
}
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.