Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by replay11 · Jan 04, 2012 at 02:22 AM · animationgameobjectparentpause

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
  }
}
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image replay11 · Jan 04, 2012 at 04:02 AM 0
Share

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.

avatar image aldonaletto · Jan 04, 2012 at 10:11 PM 0
Share

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.

avatar image aldonaletto · Jan 05, 2012 at 08:32 PM 1
Share

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.
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image replay11 · Jan 04, 2012 at 04:21 AM 0
Share

awesome! Thanks !! I'm going to try this now!

avatar image replay11 · Jan 05, 2012 at 06:33 AM 0
Share

I tried it and it's not working.... I'll post my code below as a new answer

avatar image
0

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;
   }
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flamy · Jan 05, 2012 at 08:55 AM 0
Share

did u try changing the tranform.parent to transform.root ?!?!

avatar image replay11 · Jan 05, 2012 at 04:04 PM 0
Share

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

avatar image replay11 · Jan 05, 2012 at 08:10 PM 0
Share

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.

avatar image replay11 · Jan 06, 2012 at 06:31 PM 0
Share

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.

avatar image replay11 · Jan 07, 2012 at 06:13 PM 0
Share

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!

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

make parent of animated game object follow the game object's animation 0 Answers

Best way to animate a guy putting on a hat 1 Answer

Play Animation Clips Not Attached to Parent? 1 Answer

Target child of child without knowing name? 1 Answer

remove parent of particles while emiting? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges