Stupid rotation problem

I feel like I’m taking crazy pills here, but I’m attempting to add mouse smoothing to my game. Found a script someone posted to share. Tried to clamp the rotation on the x rotation. And it is just flat out not working at all and I’m kinda going nuts.

        if (_cameraT.eulerAngles.x > 90) _cameraT.eulerAngles = new Vector3(90, _cameraT.rotation.y, _cameraT.rotation.z);
        if (_cameraT.eulerAngles.x < -90)
        {
            Debug.Log("Ting");
            _cameraT.eulerAngles = new Vector3(-90, _cameraT.rotation.y, _cameraT.rotation.z);
        }
    }

I am baffled why the log isn’t even firing.

BTW, I do know clamp is a thing but it will not work for this particular script.

Anytime you read from eulerAngles you are going to have a very bad time.

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

1 Like

Well, hell.

I’m not sure what to do then…

Here’s the code I’m trying to tweak to give it a clamp on the camera:

public class PlayerLook : MonoBehaviour

{

    [Header("Info")]
    private List<float> _rotArrayX = new List<float>();
    private List<float> _rotArrayY = new List<float>();
    private float rotAverageX;
    private float rotAverageY;
    private float mouseDeltaX;
    private float mouseDeltaY;

    [Header("Settings")]
    public float _sensitivityX = 1.5f;
    public float _sensitivityY = 1.5f;
    [Tooltip("The more steps, the smoother it will be.")]
    public int _averageFromThisManySteps = 3;

    [Header("References")]
    [Tooltip("Object to be rotated when mouse moves up/down.")]
    public Transform _cameraT;

    //============================================
    // FUNCTIONS (UNITY)
    //============================================

    void Update()
    {
        MouseLookAveraged();
    }

    //============================================
    // FUNCTIONS (CUSTOM)
    //============================================

    void MouseLookAveraged()
    {
        rotAverageX = 0f;
        rotAverageY = 0f;
        mouseDeltaX = 0f;
        mouseDeltaY = 0f;

        mouseDeltaX += Input.GetAxis("Mouse X") * _sensitivityX;
        mouseDeltaY += Input.GetAxis("Mouse Y") * _sensitivityY;

        // Add current rot to list, at end
        _rotArrayX.Add(mouseDeltaX);
        _rotArrayY.Add(mouseDeltaY);

        // Reached max number of steps? Remove oldest from list
        if (_rotArrayX.Count >= _averageFromThisManySteps)
            _rotArrayX.RemoveAt(0);

        if (_rotArrayY.Count >= _averageFromThisManySteps)
            _rotArrayY.RemoveAt(0);

        // Add all of these rotations together
        for (int i_counterX = 0; i_counterX < _rotArrayX.Count; i_counterX++)
            rotAverageX += _rotArrayX[i_counterX];

        for (int i_counterY = 0; i_counterY < _rotArrayY.Count; i_counterY++)
            rotAverageY += _rotArrayY[i_counterY];

        // Get average
        rotAverageX /= _rotArrayX.Count;
        rotAverageY /= _rotArrayY.Count;

        // Apply
        transform.Rotate(0f, rotAverageX, 0f, Space.World);
        _cameraT.Rotate(-rotAverageY, 0f, 0f, Space.Self);
    }

}

I’m really stuck on what to do…

Not sure how large you’re letting that list get to, but it will have a “dead” or “laggy” feel to it because you are linearly adding the historical terms up.

At a minimum you should be increasing the importance / weight of the most-recent terms.

This controller:

handles the mouse smoothing at around line 206/207 with a simple Lerp on each axis. The “acc” variables are the accumulating term that the raw inputs are fed into.

Yes, that’s not the “intended” purpose of Lerp, but Lerp has a secondary role as an easy-cheesy low-pass filter, giving high importance to recent inputs and less and less to past accumulated inputs.

Full original post here:

1 Like

Thanks for your help once again Kurt, I managed to get it up and running.

1 Like