unity-game-enginecinemachine

How to rotate player towards mouse position with a moving camera using cinemachine and input system? Unity 2D


I already know how to rotate the player based on the mouse position, there are tons of tutorials about that. However, every time I need the camera to move (normally using cinemachine) I get a supper jittery movement and the way the player faces the mouse changes. This is the code I used as for now, yet this is a problem I've had for a long time and can't seem to find anyone asking about it.

I'm using the new Input system, though if needed I can use the old one and using the 2D camera in cinemachine

        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        if(angle != 0)
        {
            savedAngle = angle;
        }
        transform.rotation = Quaternion.Euler(0, 0, savedAngle); 

Solution

  • You can try using Mathf.SmoothDamp() to make sure the angle doesn't change too much at once.

    https://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html

    something like this (assuming this code is running in Update()/FixedUpdate()/LateUpdate()) :

    // class member variables
    [SerializeField] private float _smoothTime = 0.3f;
    private float _velocity = 0.0f;
    ...
    ...
    float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    if(angle != 0)
    {
        savedAngle = Mathf.SmoothDamp(savedAngle, angle, ref _velocity, _smoothTime);
    }
    transform.rotation = Quaternion.Euler(0, 0, savedAngle);