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 Addyarb · Apr 02, 2014 at 03:25 PM · animationcoroutines

How to call a coroutine before an animation? (JS or C#)

Hi there,

I've been having issues with calling coroutines since I started coding. I'm sort of picking scripting up as I go, but this is something that I simply can't make sense of, and there's no clear examples on how to use it in my situation.

What I'm trying to do:

 function Update (); {
 
 
 if (Input.GetKey (KeyCode.Space))
         {
             //Wait for .5 seconds so that the model has time to wind up before he                
             //actually jumps
         WaitForSeconds(.5);
         animation.CrossFade("jump");
         }
 }

From the documentation I'm reading, I have to start a coroutine so that I can call it, but I can't use it inside the update function, so I have to create a whole new function just for my one animation? That makes no sense, but if that's the easiest way to wait half a second, can someone show me the proper formatting to do so?

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
1

Answer by fafase · Apr 02, 2014 at 07:35 PM

So you want to call the coroutine to make the animation starts later. You cannot run a coroutine from Update because it cannot be yielded.

 var waitForJump : boolean = false;
 function Update (){
        if (Input.GetKey (KeyCode.Space) && !waitforJump)
        {
             waitForJump = true;
             Jump();
        }
 }
 
 function Jump(){
      var timer:float = 0f;  
      while(timer < 0.5f){
          timer += Time.deltaTime;
          yield;
      }
      animation.CrossFade("jump");
      waitForJump = false;
 }

the boolean makes sure you cannot call the coroutine many time in a row.

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 Addyarb · Apr 03, 2014 at 07:11 AM 0
Share

This code returns the error: (13,11): UCE0001: ';' expected. Insert a semicolon at the end.

Do I need to be defining the timer up in variables?

Thanks for the help! I feel I'm getting closer to understanding it finally.

avatar image fafase · Apr 03, 2014 at 08:08 AM 0
Share

Oh I think I am mixing C# and Js. The timer declaration is wrong.

 float timer; -> var timer :float;

I edit the answer.

avatar image
0

Answer by Ben-Stoneman · Apr 02, 2014 at 03:59 PM

Try this:

C#:

 void Update()
 {
 
        if (Input.GetKey (KeyCode.Space))
        {
         StartCoroutine(Wait());
         animation.CrossFade("jump");
        }
 }
 
 IEnumerator Wait()
 {
   yield return new WaitForSeconds(5);
 }

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html

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 Addyarb · Apr 02, 2014 at 07:31 PM 0
Share

This didn't work unfortunately :/ Put it in a script all by itself and attached to player to make sure.

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

21 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

Related Questions

Move animation in coroutine problem 0 Answers

Passing reference of transform properties to coroutine animation? 1 Answer

Lerp in Nested Coroutines doesn't work if I add some functions inside the Coroutines 1 Answer

My Coroutine does not get called 4 Answers

Animation Curve or Coroutine? Performance question. 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