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
0
Question by FlashX · Dec 01, 2014 at 02:21 AM · c#animationloop

loop animation through controller for period of time? C#

Hi everyone!

I'm only learning unity and C# so go easy on me :)

Ive managed to set off an animation trigger using the lines of code below:

 int runHash = Animator.StringToHash("Run");
 
 void Start(){
 anim = GetComponent<Animator>();
 anim.SetTrigger (runHash);
 }

But how would i have the animation loop for a certain number of times or length of time? Is there a way to do that in C# using some sort of variable or do i have to make that animation a default through code?

Any insight or code would i would be appreciative of.

Thanks guys! :)

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

Answer by Baste · Dec 01, 2014 at 10:19 AM

So the usual way to use the Animator is to send a trigger that takes it to another state when that change needs to happen. If that's after a certain amount of time, you simply use a coroutine.

Say that you've got the code above, and want to switch to the walk anim after five seconds, you do:

 private Animator anim;
 
 void Start() {
     anim = GetComponent<Animator>();
     anim.SetTrigger ("Run");
     StartCoroutine(ChangeAnim());
 }

 IEnumerator ChangeAnim() {
     yield return new WaitForSeconds(5f);
     anim.SetTrigger("Walk");
 }

Note that you don't have to store the hash, just sending in the name of the trigger you're using works.

Now, normally you'd change between a running and walking animation depending on how fast your character is moving;

 anim.SetFloat("Speed", rigidbody.velocity.mangitude);

And simply set your animator to switch between walking and running when the Speed variable crosses a certain treshhold.

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 FlashX · Dec 02, 2014 at 02:23 AM 0
Share

Thank you so much Baste for your detailed response, it's so good to see people who are happy to share their knowledge without giving cryptic answers!

Your suggestion is great and has really helped me but I'm still a bit stuck.

the character is a non-playable character so the first code you gave me suites well but how can i make the Run animation keep running and not just do one cycle?

You're awesome thank you again!

Cheers

avatar image Baste · Dec 02, 2014 at 10:23 AM 0
Share

You need to set the animation to loop in the animation itself.

If it's a standalone animation clip, you just select it and check the "loop time" right on top of the inspector.

If the animation's a part of an imported .fbx or blender file or something similar, you open the animation tab on the file, find the animation, and check the box that says "loop time".

You need to do this for every animation you're going to use that needs to loop.

avatar image FlashX · Dec 02, 2014 at 11:07 AM 0
Share

O$$anonymous$$G! Baste you are amazing! Thank you so much for all your help! That worked a treat! For all those other people reading who have had this trouble, I used what Baste used above but set a bool variable and changed that to true to make it work!

Ahhhh!! All the excites!! :P

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

27 People are following this question.

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

Related Questions

Transition Animations at end of current loop 0 Answers

Animation method/coroutine playable once or loop 0 Answers

Animation keeps playing 2 Answers

Distribute terrain in zones 3 Answers

Animation keeps looping even on WrapMode.Once; 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