• 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 fgbg · Nov 12, 2019 at 04:45 AM · animationmovetowardsflip

How to execute code between animation transitions?

Can anyone determine why my walk animation plays for one frame before my flip code executes?

I have a 2D enemy platformer animation behavior where it idles by default. After it idles once, it starts the movement animation until I trigger it to idle again (when it touches a waypoint object). Movement code is ignored when animiation idling == true. This part works.

alt text alt text

However, there is a small hiccup that I cannot figure out. I flip the sprite depending on the way it should be facing after the idle animation finishes. This works, but my walk animation plays for one frame before the flip code executes. It makes a noticeable jigger when turning.

This is the movement code:

 // Update is called once per frame
 public override void Update()
 {
     //Debug.Log("MossGiant Updating...");
     Move();
 }
 private void Move()
     {
         //Check if idling
         if (_animator.GetCurrentAnimatorStateInfo(0).IsName("Idle"))
         {
             idling = true;
         }
         else
         {
             idling = false;
         }
 
         //Figure out how far we move for the next step using delta time to smooth it
         float step = speed * Time.deltaTime;
         Debug.Log("Counter is " + counter);
 
         ////Move
         //Don't move if idling
         if (idling == false)
         {
             //Check if we've reach our waypoint or not
             if (transform.position == moveTo.position)
             {
                 //We reached our waypoint so set next waypoint and do an idle animation
                 counter++;
                 _mossGiantAnimation.Idle();
 
                 //If we are at the end of the waypoints, head back to the first one and repeat
                 if (counter >= wayPoints.Length)
                 {
                     counter = 0;
                 }
                 moveTo = wayPoints[counter];
             }
             //We didn't reach our waypoint yet
             else
             {
 
                 //Flip sprite depending positive or negative x direction to waypoint
                 direction = transform.position.x - moveTo.position.x;
                 Debug.Log("Direction is " + direction);
                 FlipSprites(_sprites, direction);
 
                 //Move a step towards the waypoint at the speed of step
                 Debug.Log("Moving to waypoint " + moveTo.name);
                 transform.position = Vector3.MoveTowards(transform.position, moveTo.position, step);
             }
         }
     }
 

    void FlipSprites(SpriteRenderer[] sprites, float direction)
     {
         foreach (var sr in sprites)
             //Positive value is moving left, negative is moving right
             if (direction > 0)
                 sr.flipX = true;
             else if (direction < 0)
                 sr.flipX = false;
     }

Can anyone determine why my walk animation plays for one frame before my flip code executes?

enemy-animations.png (18.2 kB)
enemy-idle-to-wlak-transition.png (22.4 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

295 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 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 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 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 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 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 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 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 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 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 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

Can I make animations snap to a frame? 1 Answer

Fitting in a turn around animation (Alucard-like) in a transition from facing right to the left (2D) 1 Answer

i had a problem with in flip when add animation 0 Answers

Animation State disallowing movement 1 Answer

Vector3.moveTowards with animation 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