2D: Why does my character stutter if I add deltaTime to moveSpeed?
So I'm following a tutorial on youtube, first time Unity user, but been making games in other frameweorks before. In the tutorial he does not use deltaTime for movement, which has always been the standard for me, so I added it my self, but it makes my character "stutter/lag" really weirdly (does not stutter without deltaTime). Should I not use deltaTime for movement in Unity?
Script for Player movement:
void Update () {
playerMoving = false;
if (!playerAttacking) {
Vector2 direction = new Vector2();
if (Mathf.Abs (Input.GetAxisRaw ("Horizontal")) > 0.5f) {
Debug.Log ("Moving on X");
playerMoving = true;
direction.x = Input.GetAxisRaw ("Horizontal");
lastMove.x = direction.x;
lastMove.y = 0;
}
if (Mathf.Abs (Input.GetAxisRaw ("Vertical")) > 0.5f) {
Debug.Log ("Moving on Y");
playerMoving = true;
direction.y = Input.GetAxisRaw ("Vertical");
lastMove.y = direction.y;
lastMove.x = 0;
}
if (playerMoving) {
myRigidBody.velocity = direction.normalized * moveSpeed * Time.deltaTime;
} else {
myRigidBody.velocity = Vector2.zero;
}
}
Answer by nathanlink169 · May 28, 2017 at 07:31 PM
Physics in games should generally happen in FixedUpdate.
Basically, create a new function titled FixedUpdate, cut your m_Rigidbody.velocity setting into FixedUpdate, swap Time.deltaTime out for Time.fixedDeltaTime, and keep the deciding of the velocity (i.e. the calculations to determine direction) in Update.
Your answer
![](https://koobas.hobune.stream/wayback/20220612122925im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GetKeyUp does not get called sometimes. 1 Answer
Raycast is hitting enemy even when not in contact with it? 2 Answers
moving an object on the y and x axis to mouse location 0 Answers
Unity2D How to dodge roll? 0 Answers
2D upward curve movement? 0 Answers