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 ahstein · Feb 01, 2021 at 07:32 PM · animatordeltatimeanimation script

Getting framerate independent movement in OnStateMove or OnAnimatorMove

TL;DR Is there a best practice for calculating deltaTime in StateMachineBehavior.OnStateMove?


My specific issue:

I am trying to implement a knockback mechanic where an object will be knocked back in a different direction depending on where it is hit from, but implementing it as part of the knockback animation. Here's my script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class Knockback : StateMachineBehaviour
 {
     [SerializeField] float m_knockbackDistance = 1f;
 
     Vector2 m_velocity;
     float m_lastTime;
     Rigidbody2D m_rb;
 
     public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         base.OnStateEnter(animator, stateInfo, layerIndex);
         Vector2 direction = new Vector2(
             animator.GetFloat("HitDirectionX"),
             animator.GetFloat("HitDirectionY"));
         m_velocity = (m_knockbackDistance / stateInfo.length) * direction;
         m_rb = animator.GetComponent<Rigidbody2D>();
         m_lastTime = stateInfo.normalizedTime;
     }
 
     public override void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         base.OnStateMove(animator, stateInfo, layerIndex);
 
         // need to manually track time here because this gets called after both Update and FixedUpdate
         float deltaTime = stateInfo.normalizedTime - m_lastTime; // I'm assuming the animation doesn't loop here.
         m_lastTime = stateInfo.normalizedTime;
 
         m_rb.MovePosition((Vector2)animator.transform.position + m_velocity * deltaTime);
     }
 }

As you can see, there is some funkiness around using deltaTime in the script. My impression from reading the documentation on execution order is that OnStateMove is called both during FixedUpdate and Update, which means using Time.deltaTime or Time.fixedDeltaTime is incorrect and might end up doubling the movement depending on how the cycles line up. https://docs.unity3d.com/Manual/ExecutionOrder.html


However, I'm running into some bugs where objects seem to jump/teleport on some machines and not others. The bug is happening on a mac running the game at ~120 FPS (according to the stats tab in the editor), but not a PC running at ~600 FPS. I think this script is causing the problem. The most obvious answer is that my method for calculating deltaTime in the script is flawed somehow and the FPS difference is causing the different behaviors.

Suggestions? Thanks for your help!

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

137 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

Related Questions

Set the time of an animation keyframe per script? 0 Answers

How would I switch between 2 animation states connected to 1 state under input? 0 Answers

Do I have to manually add (animator controller) references to new states and parameters in an animation script every time I add new states and parameters to an animator controller? 1 Answer

Animator.GotoState: State could not be found. 2 Answers

How should I add a setparameter in an animation script after creating a parameter and a state in the animator controller? 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