Cinemachine Freelook third-person - disable/enable following mouse movement

Hello all! I recently came across using the Cinemachine Freelook camera as a third-person camera option and I really like it. Right now I’m in the very early stages of development and am playing around with options. One struggle I’m having is the ability to define how I want the camera to follow the mouse.

By default the camera locks to all mouse movement, and then re-centers automatically (after the defined delay) when I have “Recenter to Target Heading” enabled. This works very well for me, however, I would prefer it if the camera only followed my mouse if I was holding down the right mouse button, and then, when released, would begin its Wait Time timer to re-center the camera. I’ve seen this effect many times in games (WoW comes to mind most readily) and am looking to reproduce something like that.

Unfortunately I’ve come up with no good guides on how to pull this off. I was hoping it was a setting within the Brain or camera’s CinemachineFreeLook component but if it is I’ve been unable to find it.

Can anyone point me to a guide or provide some input on how I could pull this off? I would greatly appreciate any guidance, and thank you in advance!

You could disable recentering from a script as soon as the player is aiming (i.e. holding the right mouse button) and re-enable on release. Take a look at this thread: Make Player Turn Before Camera Does

Hope this helps.

Thank you! That is something I was attempting to do. I’ll take a look at the thread you linked.

After some more thought I went with something really simple. It works for me for now, but will need some further
refinement in the future. Either way, this is what I came up with:

using Cinemachine;
using UnityEngine;

public class ThirdPersonCamera : MonoBehaviour
{
    private CinemachineFreeLook _cinemachineFreeLook;
    private string _inputAxisNameX;
    private string _inputAxisNameY;

    void Awake()
    {
        _cinemachineFreeLook = GetComponent<CinemachineFreeLook>();
        _inputAxisNameX = _cinemachineFreeLook.m_XAxis.m_InputAxisName;
        _inputAxisNameY = _cinemachineFreeLook.m_YAxis.m_InputAxisName;
    }

    void Update()
    {
        _cinemachineFreeLook.m_XAxis.m_InputAxisName = Input.GetMouseButton(1) ? _inputAxisNameX : "";
        _cinemachineFreeLook.m_YAxis.m_InputAxisName = Input.GetMouseButton(1) ? _inputAxisNameY : "";
    }
}

In essence this “ignores” the input unless the right mouse button is down.

That will work but manipulating strings every frame generates garbage for GC and is not recommended. If you have CM 2.6.0 or higher, try adding this script to your FreeLook instead:

using UnityEngine;
using Cinemachine;

public class InputOnMouseDown : MonoBehaviour, AxisState.IInputAxisProvider
{
    public string HorizontalInput = "Mouse X";
    public string VerticalInput = "Mouse Y";

    public float GetAxisValue(int axis)
    {
        // No input unless right mouse is down
        if (!Input.GetMouseButton(1))
            return 0;

        switch (axis)
        {
            case 0: return Input.GetAxis(HorizontalInput);
            case 1: return Input.GetAxis(VerticalInput);
            default: return 0;
        }
    }
}

Oh very nice. I appreciate you taking the time to correct me on this. I had no idea. Thank you!
I’ll give this a try and let you know my results!

Thanks again. This code introduces me to other concepts as well that I need to learn. It also works very well for my intended purpose.

Thank you Very much for the Idea this is so helpful to understand the mechanics of the Axis and GC

sadly doesn’t work with new input system/Unity 6.2