- Home /
My top-down movement is really jittery, any idea how to fix?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float moveSpeed = 5f;
public Rigidbody2D rb;
public Vector2 movement;
public Animator anim;
public void Start()
{
anim.SetBool("IsMoving", false);
}
// Update is called once per frame
public void Update()
{
//Input
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if (movement.x == 0 && movement.y == 0)
{
anim.SetBool("IsMoving", false);
}
if(movement.x != 0 || movement.y != 0 || movement.x != 0 && movement.y !=0)
{
anim.SetBool("IsMoving", true);
}
}
void FixedUpdate()
{
//Movement
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
I expect the actual answer is something about rigidbodies that I don’t know BUT just in case, check your animator has it’s animate root motion checkbox turned off :) if that’s on with a rigidbody, then they can mess with each other.
Also just a random point;
if(movement.x != 0 || movement.y != 0 || movement.x != 0 && movement.y !=0)
The last clause will always be true if either of the first two are so is redundant.
You can just do if (movement.magnitude > 0)
Answer by harperrhett · Nov 14, 2020 at 12:39 AM
You could try changing the velocity of the rigid body, instead of using MovePosition().
rb.velocity = new Vector2(movement.x * moveSpeed, movement.y * moveSpeed);