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 Berbelucha · May 04, 2015 at 05:30 AM · animationcoroutines

Move animation in coroutine problem

Hi,

I'm trying to take my first steps in Unity and I've stumbled upon this very weird issue. I've a pathfinding script, after finding one I'm trying to move player and change his animation to idle/walking with current direction. But when I'm using code below the animation isn't changing at all or is changing with random delays. The result is that player is idle-sliding or sliding with good animation but wrong direction. I've tried changing animation parameters in coroutines but with no success. Big part of the code is from Roguelike tutorial.

     public float moveTime = 0.1f;
     protected int direction = 2;
     protected bool isWalking = false;

     void Update() {
         anim.SetBool ("walking", isWalking);
         anim.SetInteger ("direction", direction);
     }

     //Protected, virtual functions can be overridden by inheriting classes.
 protected virtual void Start ()
 {
     //Get a component reference to this object's BoxCollider2D
     boxCollider = GetComponent <BoxCollider2D> ();
     
     //Get a component reference to this object's Rigidbody2D
     rb2D = GetComponent <Rigidbody2D> ();

     anim = GetComponent<Animator> ();

     //By storing the reciprocal of the move time we can use it by multiplying instead of dividing, this is more efficient.
     inverseMoveTime = 1f / moveTime;
 }

     protected IEnumerator moveAlongPath(List<IntVector2> path) {
         isWalking = true;
         Debug.Log ("Anim-walking: " + anim.GetBool ("walking"));
         foreach (IntVector2 p in path) {
             Vector3 to = new Vector3(p.X, p.Y, 0);
             changeDirection(to);
             Debug.Log ("Anim-direction: " + anim.GetInteger ("direction"));
             yield return StartCoroutine(SmoothMovement(to));
         }
         isWalking = false;
         Debug.Log ("Anim-walking: " + anim.GetBool ("walking"));
     }
     
     
     //Co-routine for moving units from one space to next, takes a parameter end to specify where to move to.
     protected IEnumerator SmoothMovement (Vector3 end)
     {
         //Calculate the remaining distance to move based on the square magnitude of the difference between current position and end parameter. 
         //Square magnitude is used instead of magnitude because it's computationally cheaper.
         float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
         
         //While that distance is greater than a very small amount (Epsilon, almost zero):
         while(sqrRemainingDistance > float.Epsilon)
         {
             //Find a new position proportionally closer to the end, based on the moveTime
             Vector3 newPostion = Vector3.MoveTowards(rb2D.position, end, inverseMoveTime * Time.deltaTime);
             
             //Call MovePosition on attached Rigidbody2D and move it to the calculated position.
             rb2D.MovePosition (newPostion);
             
             //Recalculate the remaining distance after moving.
             sqrRemainingDistance = (transform.position - end).sqrMagnitude;
             
             //Return and loop until sqrRemainingDistance is close enough to zero to end the function
             yield return null;
         }
     }

alt text

Exit times always 0, the same with transition duration

PS. How can I change speed of movement, because moveTime variable isn't giving any result :(

anim-contr.jpg (91.0 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

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

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

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