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). Surely I must use deltaTime for movement in the Update()method?

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; 

My Camera (Stutters unless I use fixedUpdate):

void FixedUpdate () {
    targetPos = new Vector3 (followTarget.transform.position.x, followTarget.transform.position.y, transform.position.z);
    transform.position = Vector3.Lerp (transform.position, targetPos, moveSpeed);

The problem is that you're setting velocity. Since it goes without saying that distance = time x velocity, what you're doing now is distance = time x (velocity x time). If you were setting the position of your object, then multiplying by delta time would have made sense.

In short, you shouldn't be multiplying velocity by time. Again, if it was something like acceleration that you were using, since velocity = time x acceleration, the formula would've changed as follows:

myRigidBody.velocity = myRigidBody.velocity + (direction.normalized * acceleration * Time.deltaTime);
| improve this answer | |
  • \$\begingroup\$ @Green_qaue I've updated the answer to include acceleration and how you should be using the velocity if you want an "overtime change". Don't forget to set a maximum value for your velocity (an if condition and checking for maximum velocity should be fine). \$\endgroup\$ – John Hamilton May 28 '17 at 9:14
  • \$\begingroup\$ Thanks. So is setting velocity without deltaTime fine, or should I change so I set Position, instead of velocity, and use deltaTime? For normal movement \$\endgroup\$ – Green_qaue May 28 '17 at 9:14
  • 1
    \$\begingroup\$ Ah, I jsut read the docs, okay so I should not use velocity for this. Maybe I shouild stop folowing this tutorial \$\endgroup\$ – Green_qaue May 28 '17 at 9:15
  • \$\begingroup\$ @Green_qaue since you already have coding and other framework experience, I suggest just making a simple scene to learn the ropes and search for what you need online instead of sticking to a tutorial. \$\endgroup\$ – John Hamilton May 28 '17 at 9:16
  • \$\begingroup\$ One more question, should I be controlling movement inside Update or FixedUpdate? Simply setting myRigidBody.position += direction.normalized * moveSpeed * Time.deltaTime Didn't work. I see some people using addForce inside FixedUpdate, for movement \$\endgroup\$ – Green_qaue May 28 '17 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.