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 ElMenduko · Feb 12, 2015 at 01:41 PM · animatorparameter

(C#) Switching back and forth between an "idle" animation and a "punch" animation

I always wanted to experiment with custom characters and animations, but making 3D models is kinda complicated, specially animated and rigged ones so I experimented with a poorly drawn 2D character.

I made myself a nice spritesheet (Here it is) with 4 animations of 4 frames each. I experimented all morning with it and managed to make an animator with all of the animations, and a parameter: "animState", an integer that decides which anim is playing.

My animator

For now I'll just focus on the "spuder_idle" and "spuder_punch" states. When the "animState" parameter equals 1, it will transition from "spuder_idle" to "spuder_punch" animation.

Now look at this piece of code:

 void Update () {
 
         if (Input.GetKeyDown (KeyCode.Mouse0)) {
         //punch state
             anim.SetInteger("animState", 1);
 
             //wait for the end of the animation and then anim.SetInteger("animState", 0);
 
         }

What happens with this script is that at the start of the scene, my character is playing the idle animation. When I press the left mouse button it starts playing the punch animation forever.

I know why this happens: It happens because the "punch" state ends after the animation has played (ExitTime 1.0). But when it goes back to the "idle" state the animState parameter is still 1, so it goes back to the punch state instantly and so on.

To fix this I tried something like this:

 if (Input.GetKeyDown (KeyCode.Mouse0)) {
         //punch state
             anim.SetInteger("animState", 1);
             anim.SetInteger("animState", 0);
 
 
         }

But it switches animState from 1 to 0 in the same frame. So nothing happens at all.

THE REAL QUESTION IS:

  • How do I wait for the animation to end and then, in that moment set animstate to 0? I found some "WaitForSomething" stuff in the documentation but I got no idea how to use them.

  • Or how do I setup my Animator in such a way that it's easier to switch states back and forth?

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

1 Reply

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

Answer by Baste · Feb 12, 2015 at 02:15 PM

You don't use SetInteger and an integer for this. Not only is it hard to read (you have to remember what int means what state), but you also run into the issue you're facing.

Use triggers instead. Create a trigger called "punch" in the animator, and have the animator transition from idle to punch on that trigger. Then you call:

 anim.SetTrigger("Punch");

This will have your animator transition to the punch state, and then return to idle until the button is pressed again.

Triggers are what you want to use for single-fire events in the animator. Bools, ints and float are something you'll want to use for persistent states - such as having your character be crouched or not, or selecting what weapon (if any) your character model should be holding.

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 ElMenduko · Feb 12, 2015 at 03:07 PM 0
Share

Thanks a lot. I didn't know what "trigger" type did. I'm mostly familiar with normal variable, not unity ones.

I thought it would be like a boolean but now I tried it and it resets automatically. Thanks.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why Unity thinks that animator parameter is being used by the Blending Tree? 1 Answer

2D Animation does not start 1 Answer

play different clips via GetKeyDown in unity 0 Answers

Parameter does not exist 3 Answers

Player freezes at start of scene due to animator 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