Reputation: 118
So far learning unity, camera rotation with mouse is the thing that has given me the most problems. Now the problem I am facing is, when making a first person controller based on rigidbody, that the rotation of the character and camera with the mouse is inconsistent and jittery.
This youtube video, shows the problem. In it, i am smoothly moving the mouse but the rotation seems to sometimes skip some intermediate rotations and jump to new values.
This is the hierarchy I am currently using for the character as well as the attached components:
And this is the complete code for the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody), typeof(Collider))]
public class CompleteRBController : MonoBehaviour
{
[Header("Referenced components:")]
[SerializeField]
private Rigidbody _rb;
[SerializeField]
private Transform _cameraTransform;
[Header("Mouse Settings")]
public float _mouseSensitivity = 100f;
[SerializeField]
private float pitch = 0f;
[SerializeField]
private bool _mouseInverted = false;
[Header("Parameters:")]
[SerializeField]
private float _speed = 10f;
[SerializeField]
private float _sprintSpeed = 15f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
//Get input mouse
float mouseX = Input.GetAxisRaw("Mouse X") * _mouseSensitivity * Time.fixedDeltaTime;
float mouseY = Input.GetAxisRaw("Mouse Y") * _mouseSensitivity * Time.fixedDeltaTime;
//Get input keyboard
float vertical = Input.GetAxisRaw("Vertical");
float horizontal = Input.GetAxisRaw("Horizontal");
// Debug.Log(string.Format("Vertical: {0}, Horizontal: {1}", vertical, horizontal));
bool isSprinting = Input.GetKey(KeyCode.LeftShift);
//Rotate
pitch += (_mouseInverted) ? -mouseY : mouseY;
pitch = Mathf.Clamp(pitch, -89, 89);
_cameraTransform.localRotation = Quaternion.Euler(pitch, 0, 0);
transform.Rotate(transform.up * mouseX);
//Move
Vector3 move = (transform.forward * vertical + transform.right * horizontal).normalized * ((isSprinting)? _sprintSpeed : _speed);
_rb.velocity = new Vector3(move.x, _rb.velocity.y, move.z);
}
}
I will also add a list of things I tried to change to make it smooth:
Sometimes on the first seconds of the simulation i does work smoothly but after a little while it becomes wonky again.
I dont really know how to fix it / what is causing the problem. I would really appreciate it if you could help me with this or put me in the right track to understanding the problem.
Thanks in advance for helping me.
Upvotes: 2
Views: 2824
Reputation: 38
The skipping occurs whenever the FixedUpdate does not match the updating of a frame / Update(). Thats why you should put all Input related stuff in Update and all Physics related stuff in FixedUpdate. I would recommend you to make a Inputmanager Method and a CalculateMovement method. Then put the Inputmanager Method in Update and the CalculateMovement method in FixedUpdate. Also Time.fixedDeltaTime is not the same as Time.DeltaTime. If you put the Input in Update instead of FixedUpdate, you have to change it to Time.DeltaTime instead. Hope i could help you! :)
Upvotes: 2