So - the camera has the slightest case of the jitters, and the character tends to freeze up when walking into a wall (something to do with the physics and velocity).
The camera as I mentioned - jitters but it’s also oddly ‘laggy’? The game runs smoothly but you can see the FPS don’t match.
The character as I mentioned - gets stuck on walls, while is quite annoying. I believe it has to do with velocity of the rigidbody.
Here’s the code for my stuff:
Camera
using UnityEngine;
using UnityEngine.InputSystem;
public class FollowPlayer : MonoBehaviour
{
[Header("INITIALIZE")]
[SerializeField] private Transform Player;
[SerializeField] private Vector3 Offset;
[SerializeField] private InputActionReference lookAction;
[Header("Properties")]
[SerializeField] private float MouseSensitivity = 75f;
[SerializeField] private float Smoothing = 0.05f;
[Header("Limits")]
[SerializeField] private float minY = -90f;
[SerializeField] private float maxY = 90f;
private Vector2 lookInput;
private float xRotation = 0f;
private void OnEnable()
{
lookAction.action.Enable();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void OnDisable()
{
lookAction.action.Disable();
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
private void FixedUpdate()
{
float mouseX = lookInput.x * MouseSensitivity * Time.fixedDeltaTime;
Player.Rotate(Vector3.up * mouseX);
}
private void LateUpdate()
{
transform.position = Player.position + Offset;
lookInput = lookAction.action.ReadValue<Vector2>();
float mouseY = lookInput.y * MouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, minY, maxY);
transform.rotation = Quaternion.Euler(xRotation, Player.eulerAngles.y, 0f);
}
}
Rigidbody / Movement
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
[Header("Initialization")]
private Rigidbody PlayerRigidbody;
[SerializeField] private InputActionReference MoveAction;
[Header("Ground Check")]
[SerializeField] private Transform GroundCheck;
[SerializeField] private float GroundCheckRadius = 0.5f;
[SerializeField] private LayerMask GroundMask;
[Header("Movement")]
[SerializeField] private float MovementSpeed = 5f;
[SerializeField] private float Acceleration = 10f;
[SerializeField] private float Deceleration = 10f;
[Header("Status")]
private bool _isGrounded;
private bool _previousIsGrounded;
public bool IsGrounded => _isGrounded;
private void Start()
{
PlayerRigidbody = GetComponent<Rigidbody>();
}
private void OnEnable()
{
MoveAction.action.Enable();
}
private void OnDisable()
{
MoveAction.action.Disable();
}
private Vector3 moveInput;
private Vector3 currentVelocity;
private void Update()
{
Vector2 Input = MoveAction.action.ReadValue<Vector2>();
moveInput = new Vector3(Input.x, 0, Input.y);
}
void FixedUpdate()
{
_isGrounded = CheckGrounded();
if (_isGrounded != _previousIsGrounded)
{
_previousIsGrounded = _isGrounded;
}
MovePlayer();
}
// FUNCTIONS
private void MovePlayer()
{
Vector3 targetVelocity = transform.forward * moveInput.z * MovementSpeed + transform.right * moveInput.x * MovementSpeed;
currentVelocity = Vector3.Lerp(currentVelocity, targetVelocity, (targetVelocity.magnitude > 0 ? Acceleration : Deceleration) * Time.fixedDeltaTime);
PlayerRigidbody.velocity = new Vector3(currentVelocity.x, PlayerRigidbody.velocity.y, currentVelocity.z);
}
public bool CheckGrounded()
{
return Physics.CheckSphere(GroundCheck.position, GroundCheckRadius, GroundMask);
}
// Gizmos
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(GroundCheck.position, GroundCheckRadius);
}
}