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 Sun-Pengfei · Nov 22, 2015 at 09:42 AM · animationunity 5animatoranimator controller

How to trigger the same animator state in unity5 with script?

Example:

In an animator there're two states: idle, attack.

In animation transition settings:

Default -> idle

attack(has exit time of 1) -> idle

Goal:

I want to play the attack animation whenever mouse is clicked.

Problem:

Using animator.Play("attack"); will trigger the attack state successfully, however when I clicked mouse immediately, if the animation in attack state has not finished, it won't be triggered again.

I searched for some time, only found this: http://answers.unity3d.com/questions/787605/mecanim-equvalent-of-animationstop.html

How to solve this? Thanks!

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
2
Best Answer

Answer by Sun-Pengfei · Nov 23, 2015 at 10:13 AM

One idea to solve this is to call Play("idle") before Play("attack"). The state would transit into idle then immediately transit into attack. However the following piece of code doesn't work.

 public void PlayAttackAnimation()
 {
     animator.Play("idle");
     animator.Play("attack");
 }

I've worked a way to let it work.

 bool playAttack = false;

 public void PlayAttackAnimation()
 {
     animator.Play("idle");
     attackAnimFlag = true;
 }

 void LateUpdate()
 {
     if (attackAnimFlag)
     {
         attackAnimaFlag = false;
         animator.Play("attack");
     }
 }

This works in Unity 5.2.2f1, but feels kind of hack. Notice: It only works using LateUpdate(), if you change the second part as Update(), it won't work. I don't know why, guessing Unity must have done some thing related with animator state changing between Update() and LateUpdate().

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
0

Answer by OctoMan · Nov 22, 2015 at 06:25 PM

You want to trigger an animation, even if it's not ended before? It wouldn't look good but i would try to trigger to idle, and get rid of exit time,so trigger to exit the running animation. Also use an animtion event in the last frame of your animation to trigger to idle too.

Comment
Add comment · Show 4 · 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 Sun-Pengfei · Nov 23, 2015 at 04:05 AM 0
Share

Thanks for your advice. Actually I've tried this, but it doesn't work if you call Play("attack") right after Play("idle"). I've worked out a way that'll work, but feels kind of "hack". I'll post my code later.

avatar image alvola Sun-Pengfei · Jan 12, 2016 at 12:35 AM 0
Share

Please share it :)

avatar image Sun-Pengfei alvola · Jan 12, 2016 at 01:39 AM 0
Share

The other one is my answer :)

Show more comments

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

Animation Different in Unity 5 1 Answer

Check Animator State Animation Completed 0 Answers

Hello! I have some issues with animations. 1 Answer

Cannot cast RuntimeAnimatorController to AnimatorOverrideController 0 Answers

Animator and Scripting problems. Help !! 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