I’m making this retro fps and am using Brackeys fps movement, but I found out that the move look is jittery.
How can I smoothen it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseCam : MonoBehaviour {
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
I found this other code that does what I want, but it doesn’t clamp rotation.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseCam : MonoBehaviour {
[SerializeField]
public float sensitivity = 5.0f;
[SerializeField]
public float smoothing = 2.0f;
// the chacter is the capsule
public GameObject character;
// get the incremental value of mouse moving
private Vector2 mouseLook;
// smooth the mouse moving
private Vector2 smoothV;
// Use this for initialization
void Start () {
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update () {
// md is mosue delta
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
// the interpolated float result between the two float values
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
// incrementally add to the camera look
mouseLook += smoothV;
// vector3.right means the x-axis
transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
}
}
Can someone help me fix any one of these scripts?