I am having an issue with jittery player movement. I have tried changing the interpolation, playing around with the Lerp speed and regular speed variables, and changing the movement physics between update and fixedUpdate.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 5f; // Movement speed
public float drag = 5f; // Drag to reduce sliding
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.drag = drag; // Set drag to the Rigidbody
}
void FixedUpdate()
{
// Get input
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
// Create movement vector
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical) * speed;
if (movement.magnitude > 0)
{
// Apply movement
Vector3 targetVelocity = new Vector3(movement.x, rb.velocity.y, movement.z);
rb.velocity = targetVelocity;
}
else
{
// Stop the player from sliding by zeroing out the velocity
rb.velocity = new Vector3(0, rb.velocity.y, 0);
}
}
}
I am using a rigidbody on a capsule with a capsule collider on a plane with a mesh collider and renderer.