Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 track01 · Aug 25, 2011 at 03:08 AM · animationstopisplaying

Stop one animation while another plays

Hey everyone ive been getting a problem where I have a walk animation and an attack animation on the same object (sword) For some reason when I walk and use the attack button the swing animation I use bugs out with the walking animation.

Is there a way to stop one animation while another plays? With my game its pretty important that the swing happens at the moment the button is pressed. I've tried a few things but the code I have up to now doesn't work.

 if(Input.GetMouseButtonDown(0)){
 sword.animation.Stop("walk");
 sword.animation.Play("attack1");

 }
 else if(Input.GetAxis("Vertical")) 
 {
     sword.animation.CrossFade("walk");
 }
 
 else if(Input.GetAxis("Horizontal")) 
 {
     sword.animation.CrossFade("walk");
 }
 else 
 {
     sword.animation.Stop("walk");
 }
 

Would anyone be able to tell me what is wrong and how I can fix this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Waz · Aug 25, 2011 at 03:19 AM

With the above code, executing every frame in an Update() function, if you walk and while walking press the mouse, this happens:

  • walk animation plays

  • walk stops, attack1 plays

  • walk animation crossfades in again the very next frame

I'm guessing you haven't read the Animation Scripting documentation, since it covers this exact case in the "Animation Layers" section.

Reading is good.

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
avatar image
2

Answer by track01 · Aug 25, 2011 at 03:39 AM

Thank you, I've gotten it working now :)

If anyone needs the new code:

 var sword : GameObject;
 
 function Start() {
     // Set all animations to loop
    sword.animation.wrapMode = WrapMode.Loop;
    // except shooting
    sword.animation["attack1"].wrapMode = WrapMode.Once;
 
    // Put idle and walk into lower layers (The default layer is always 0)
    // This will do two things
    // - Since shoot and idle/walk are in different layers they will not affect
    //   each other's playback when calling CrossFade.
    // - Since shoot is in a higher layer, the animation will replace idle/walk
    //   animations when faded in.
    sword.animation["attack1"].layer = 1;
 
    // Stop animations that are already playing
    //(In case user forgot to disable play automatically)
    sword.animation.Stop();
 
 }
 
 function Update() {
    // Based on the key that is pressed,
    // play the walk animation or the idle animation
    if (Input.GetAxis("Vertical"))
       sword.animation.CrossFade("walk");
    else if(Input.GetAxis("Horizontal")) 
     {
         sword.animation.Play("walk");
     }
    // Shoot
    if(Input.GetMouseButtonDown(0))
       sword.animation.CrossFade("attack1");
 }
Comment
Add comment · Show 1 · 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 Velketor · Jun 21, 2012 at 07:02 PM 0
Share

this just fixed a big problem I was having in my script. I had the same exact issue and layering them properly in my code is what fixed it. All I had to do was put my animation 1 layer above the other and voila, it works. thanks for the info!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

ANimation doesn't rewind on animation.Stop 3 Answers

Animation Finished 1 Answer

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers

Detect if is playing an animation 1 Answer


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