admin管理员组

文章数量:1334337

I made a first-person camera script and it’s currently attached to the main camera which is a child of my player.

I think I made a mistake with how I made my code which I can’t solve. My mouse sensitivity is set very low, to 2f which is already a decent speed, something like 10f would be uncontrollable.

If I add * Time.deltaTime to: float rotationSpeed = inputVector.x * mouseSensitivity; then my vertical rotation becomes much faster than my horizontal rotation which becomes much slower. If I add * Time.deltaTime to my mouseX and mouseY floats inside Update(), vertical mouse input/rotation no longer works and only horizontal mouse input works. But with that second approach, if I increase mouse sensitivity, horizontal input would work still, but vertical input would go from not working to being extremely jittery. Am I missing something?

using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
    // Camera rotation variables
    public float mouseSensitivity = 2f;
    public float topAngleLimit = 80.0f;
    public float bottomAngleLimit = -45.0f;
    private Vector2 inputVector;
    public float cameraPitch = 0.0f;

    public Transform mainCamera;

    //public float horizontalRecoilOffset = 0f;
    public Transform playerBody; // i put the main parent object into this inspector slot

    [SerializeField] private WeaponManager weaponManager; 
    public Aiming aim;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;      
    }

    void Update()
    {

        float mouseX = Input.GetAxis("Mouse X") * nouseSensitivity;
        float mouseY = Input.GetAxis("Mouse Y") * nouseSensitivity;

        inputVector = new Vector2(mouseX, mouseY);

        CameraRotation(); 
    }

    private void CameraRotation()
    {
        // Adjust vertical/pitch
            cameraPitch -= inputVector.y;
            cameraPitch = ClampAngle(cameraPitch, bottomAngleLimit, topAngleLimit);

            // apply vertical rotation to the camera
            mainCamera.localRotation = Quaternion.Euler(cameraPitch, horizontalRecoilOffset, 0.0f);

            // Rotate the player body for horizontal move/yaw
            float rotationSpeed = inputVector.x;
            playerBody.Rotate(Vector3.up * rotationSpeed);
    }

    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360f) angle += 360f;
        if (angle > 360f) angle -= 360f;
        return Mathf.Clamp(angle, min, max);
    }
}

I made a first-person camera script and it’s currently attached to the main camera which is a child of my player.

I think I made a mistake with how I made my code which I can’t solve. My mouse sensitivity is set very low, to 2f which is already a decent speed, something like 10f would be uncontrollable.

If I add * Time.deltaTime to: float rotationSpeed = inputVector.x * mouseSensitivity; then my vertical rotation becomes much faster than my horizontal rotation which becomes much slower. If I add * Time.deltaTime to my mouseX and mouseY floats inside Update(), vertical mouse input/rotation no longer works and only horizontal mouse input works. But with that second approach, if I increase mouse sensitivity, horizontal input would work still, but vertical input would go from not working to being extremely jittery. Am I missing something?

using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
    // Camera rotation variables
    public float mouseSensitivity = 2f;
    public float topAngleLimit = 80.0f;
    public float bottomAngleLimit = -45.0f;
    private Vector2 inputVector;
    public float cameraPitch = 0.0f;

    public Transform mainCamera;

    //public float horizontalRecoilOffset = 0f;
    public Transform playerBody; // i put the main parent object into this inspector slot

    [SerializeField] private WeaponManager weaponManager; 
    public Aiming aim;

    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;      
    }

    void Update()
    {

        float mouseX = Input.GetAxis("Mouse X") * nouseSensitivity;
        float mouseY = Input.GetAxis("Mouse Y") * nouseSensitivity;

        inputVector = new Vector2(mouseX, mouseY);

        CameraRotation(); 
    }

    private void CameraRotation()
    {
        // Adjust vertical/pitch
            cameraPitch -= inputVector.y;
            cameraPitch = ClampAngle(cameraPitch, bottomAngleLimit, topAngleLimit);

            // apply vertical rotation to the camera
            mainCamera.localRotation = Quaternion.Euler(cameraPitch, horizontalRecoilOffset, 0.0f);

            // Rotate the player body for horizontal move/yaw
            float rotationSpeed = inputVector.x;
            playerBody.Rotate(Vector3.up * rotationSpeed);
    }

    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360f) angle += 360f;
        if (angle > 360f) angle -= 360f;
        return Mathf.Clamp(angle, min, max);
    }
}
Share Improve this question edited Nov 20, 2024 at 10:51 RelevantUni98 asked Nov 20, 2024 at 9:06 RelevantUni98RelevantUni98 715 bronze badges 9
  • what about using a Vector2 for the mouse sensitivity so you can adjust X and Y individually? – derHugo Commented Nov 20, 2024 at 9:15
  • 1 one reason for jitter might also be the rotationThreshold .. why not take any rotation into account even if very small / slow? – derHugo Commented Nov 20, 2024 at 9:16
  • also in CameraRotation you do float rotationSpeed = inputVector.x * mouseSensitivity; .. you already applied the sensitivity to the inputVector itself so this is redundant – derHugo Commented Nov 20, 2024 at 9:17
  • Yeah you're right, I took out the other 'mouseSensitivity' since it wasn't needed, thanks for pointing that out. I'm going to give your first comment a try so adjust both individually with a Vector2, I will see how that goes. But I think the issue might also be with how I use currentMouseSensitivity? I know not much can be helped with that since the other scripts are not included in this post. But I change sensitivity dynamically for each gun when scoping/aiming/zooming in with them. Will still try the other ideas you gave – RelevantUni98 Commented Nov 20, 2024 at 9:27
  • I completely removed: if (inputVector.sqrMagnitude >= rotationThreshold) (updated my post) so now the issue seems to be gone, but I still need to test more since I've had no luck so far, not sure why I overlooked that line though – RelevantUni98 Commented Nov 20, 2024 at 9:33
 |  Show 4 more comments

2 Answers 2

Reset to default 0

Let's break down what your code does

Input.GetAxis("Mouse Y") returns relatively small values (in pixels moved since the last frame)

you are multiplying this number by the sensitivity:

float mouseY = Input.GetAxis("Mouse Y") * currentMouseSensitivity

then storing it into the inputVector
inputVector = new Vector2(mouseX, mouseY);

then you are multiplying the Y value again when you're calculating the cameraPitch

cameraPitch -= inputVector.y * mouseSensitivity;

Check if the problem lies in the logic above

You can smooth the input over time for slightly less precision but less jitter. Using Cinemachine will do that automatically for you. Just have an empty gameobject that takes the actual mouse movements and a virtual camera with smooth follow.

本文标签: cHow to remove jittery mouse input Unity3DStack Overflow