1
\$\begingroup\$

As said, rigidbody moves fine, even smoothly in the x axis.

Problem comes in on the y axis. Whenever the character goes up or down, it noticeably shakes.

Additional Info:

  • Unity 5.6
  • Using linear interpolation
  • Mass is 5
  • Linear Drag is 20
  • Gravity is 0
  • Angular Drag is 0
  • Locked rotation on the z-axis
  • Dynamic Rigidbody
  • No parent Object, not even a child

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerMovement : MonoBehaviour {
    
    Rigidbody2D rbody;
    //Animator anim;
    public float speed = 0;
    /*SpriteRenderer sr;
    Vector2 movementVector;
    void Start () {
        rbody = GetComponent<Rigidbody2D> ();
        anim = GetComponent<Animator> ();
        sr = GetComponent<SpriteRenderer>();
    }*/
    
    void Update(){
        //sr.sortingOrder = ((int)Camera.main.WorldToScreenPoint (sr.bounds.min).y) * -1;
        movementVector = new Vector2 ((Input.GetAxisRaw("Horizontal"))*3, Input.GetAxisRaw("Vertical"));
        /*if (movementVector != Vector2.zero) {
            anim.SetBool ("isWalking", true);
            anim.SetFloat ("inputX", movementVector.x);
            anim.SetFloat ("inputY", movementVector.y);
        } else 
            anim.SetBool ("isWalking", false);
    }*/
    
    void FixedUpdate(){     
        rbody.AddForce (movementVector * speed);
    }
    

    }

I also noticed that changing the mass and linear drag affected the movement speed on the y axis but not on the x axis, and movement on the y axis is noticeably faster than on the x axis, which is why I multiplied the horizontal input by 3.

\$\endgroup\$
  • \$\begingroup\$ Will you comment out the animation code and see if the problem persists? That will help us narrow it down. \$\endgroup\$ – Evorlor May 24 '17 at 14:00
  • \$\begingroup\$ @Evorlor There you go + this script is the only thing that could affect the character's movement. \$\endgroup\$ – kdyz May 24 '17 at 15:10
  • \$\begingroup\$ Typically when I've experience jittery or skewed behavior it has to do with the parent object. You could explore that a bit but it's a stab in the dark really. \$\endgroup\$ – Greg Petersen May 24 '17 at 19:06
  • \$\begingroup\$ @GregPetersen, It's not a child and it also has no other game objects attached to it. \$\endgroup\$ – kdyz May 25 '17 at 4:59
  • \$\begingroup\$ I tried to recreate this Behaviour with the Provided code but i failed at it both axis are the same speed when i leave out the 3* part I also tried with different speeds but no jittering smooth on both axis \$\endgroup\$ – Gorock May 25 '17 at 6:52
0
\$\begingroup\$

Check to see if the rigidbody on your character has a gravity scale being applied to it;

Unity Rigidbody2D component

That's my own guess for what's wrong. It's a common enough pitfall that I fall into every now and then myself, but it's a bit hard to tell without knowing what it looks like or what type of game you're doing.

On the code itself, I wouldn't recommend directly manipulating the Input, rather adjusting the input after getting it provides better insight as to whether the input itself is working.

...
// Let's divvy up the x and y speeds
public float xSpeed = 5;
public float ySpeed = 5;
Vector2 movementVector;

void Update(){
    ...
    movementVector = new Vector2 ((Input.GetAxisRaw("Horizontal")), Input.GetAxisRaw("Vertical"));
    ...
}

void FixedUpdate(){
    // Now we have more control for the x and y speeds, neato
    movementVector = new Vector2(movementVector.x * xSpeed, movementVector.y * ySpeed);
    rbody.AddForce (movementVector);
}
\$\endgroup\$
  • \$\begingroup\$ Angular and Gravity is both at zero, I also locked the z-rotation. \$\endgroup\$ – kdyz May 21 '17 at 4:13
-1
\$\begingroup\$

Fixed it!

For the varying speed at the axes:

  1. Head over to Project Settings -> Input
  2. Make sure gravity is 0 for both axes

For the camera, I tried to lock it and watch my character move around, and it moved without a jitter, my solution was:

  1. Make sure that Camera's movement is under the same type of execution function as the target's movement is. So if target's at Update() then so should the camera.

Originally, my camera was at late update which must be why it jittered, because they were not moving at the same time.

\$\endgroup\$

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.