As the title says, when I’m moving around the object I’m looking at, it jitters hard. However when I’m moving forward or strafe left, right, it looks normal.
Edited: I edited the code so that all input logic is in Update and physics logic in FixedUpdate. Problem still remains.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] Rigidbody playerRigidbody;
[SerializeField] Transform playerTransform;
[SerializeField] Transform cameraTransform;
// Camera control variables
[SerializeField] float mouseSensitivity = 100f;
float xRotation = 0f; // xRotation is the rotation around x Axis, which is camera looking up down
float mouseX;
float mouseY;
// Movement control variables
[SerializeField] float movementSpeed = 12f;
Vector3 moveDirection;
float groundDragValue = 7f;
float movementSpeedMultiplier = 10f;
float forwardMovement;
float sideMovement;
// Jump control variable
[SerializeField] float jumpSpeed = 12f;
[SerializeField] Transform groundCheckSphere;
[SerializeField] LayerMask groundMask;
bool jumpInput;
bool isGrounded;
float airDragValue = 2f;
float airMovementSpeed = 5f;
// Crouch control variables
bool crouchInput;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
playerRigidbody.freezeRotation = true;
}
// Update is called once per frame
void Update()
{
MyInput();
}
// FixedUpdate has the frequency of Physic system and moving with ridigbody is based on Physic. So call MovementControl in
// FixedUpdate makes movement smoother.
private void FixedUpdate()
{
isGrounded = Physics.CheckSphere(groundCheckSphere.position, 0.4f, groundMask);
Debug.Log(isGrounded);
MovementControl();
JumpControl();
CameraControl();
DragControl();
CrouchControl();
}
void MyInput() {
// Get mouse input and apply mouse sensitivity
mouseX = Input.GetAxisRaw("Mouse X") * mouseSensitivity * Time.smoothDeltaTime;
mouseY = Input.GetAxisRaw("Mouse Y") * mouseSensitivity * Time.smoothDeltaTime;
// Get movement
forwardMovement = Input.GetAxisRaw("Vertical");
sideMovement = Input.GetAxisRaw("Horizontal");
// Get jump input
jumpInput = Input.GetKey(KeyCode.Space);
// Get crouch input
crouchInput = Input.GetKey(KeyCode.LeftControl);
}
void CameraControl()
{
// Look horizontal
playerTransform.Rotate(0f, mouseX, 0f, Space.Self);
// Look vertical
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
cameraTransform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
}
void MovementControl()
{
moveDirection = transform.forward * forwardMovement + transform.right * sideMovement;
// Use normalized to make all the magnitudes to [-1, 1]
if (isGrounded)
{
playerRigidbody.AddForce(moveDirection.normalized * movementSpeed * movementSpeedMultiplier, ForceMode.Acceleration);
} else
{
playerRigidbody.AddForce(moveDirection.normalized * airMovementSpeed * movementSpeedMultiplier, ForceMode.Acceleration);
}
}
void DragControl()
{
if (isGrounded)
{
playerRigidbody.drag = groundDragValue;
} else
{
playerRigidbody.drag = airDragValue;
}
}
void CrouchControl()
{
if (crouchInput)
{
playerTransform.localScale = new Vector3(playerTransform.localScale.x, 1.3f, playerTransform.localScale.z);
} else
{
playerTransform.localScale = new Vector3(playerTransform.localScale.x, 2f, playerTransform.localScale.z);
}
}
void JumpControl()
{
if (jumpInput && isGrounded)
{
playerRigidbody.AddForce(transform.up * jumpSpeed, ForceMode.Impulse);
}
if (playerRigidbody.velocity.y < 0)
{
playerRigidbody.AddForce(Physics.gravity * 4, ForceMode.Acceleration);
}
}
}
Here’s the video to show