Search Unity

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

Discussion in 'Cinemachine' started by Beguiled, Aug 29, 2020.

  1. Beguiled

    Beguiled

    Joined:
    Mar 15, 2014
    Posts:
    14
    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!
     
  2. ZeroThreeZero

    ZeroThreeZero

    Joined:
    Jul 12, 2020
    Posts:
    9
    Beguiled likes this.
  3. Beguiled

    Beguiled

    Joined:
    Mar 15, 2014
    Posts:
    14
    Thank you! That is something I was attempting to do. I’ll take a look at the thread you linked.
     
    ZeroThreeZero likes this.
  4. Beguiled

    Beguiled

    Joined:
    Mar 15, 2014
    Posts:
    14
    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:

    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3.  
    4. public class ThirdPersonCamera : MonoBehaviour
    5. {
    6.     private CinemachineFreeLook _cinemachineFreeLook;
    7.     private string _inputAxisNameX;
    8.     private string _inputAxisNameY;
    9.  
    10.     void Awake()
    11.     {
    12.         _cinemachineFreeLook = GetComponent<CinemachineFreeLook>();
    13.         _inputAxisNameX = _cinemachineFreeLook.m_XAxis.m_InputAxisName;
    14.         _inputAxisNameY = _cinemachineFreeLook.m_YAxis.m_InputAxisName;
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         _cinemachineFreeLook.m_XAxis.m_InputAxisName = Input.GetMouseButton(1) ? _inputAxisNameX : "";
    20.         _cinemachineFreeLook.m_YAxis.m_InputAxisName = Input.GetMouseButton(1) ? _inputAxisNameY : "";
    21.     }
    22. }
    23.  
    In essence this "ignores" the input unless the right mouse button is down.
     
    Last edited: Aug 31, 2020
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. public class InputOnMouseDown : MonoBehaviour, AxisState.IInputAxisProvider
    5. {
    6.     public string HorizontalInput = "Mouse X";
    7.     public string VerticalInput = "Mouse Y";
    8.  
    9.     public float GetAxisValue(int axis)
    10.     {
    11.         // No input unless right mouse is down
    12.         if (!Input.GetMouseButton(1))
    13.             return 0;
    14.  
    15.         switch (axis)
    16.         {
    17.             case 0: return Input.GetAxis(HorizontalInput);
    18.             case 1: return Input.GetAxis(VerticalInput);
    19.             default: return 0;
    20.         }
    21.     }
    22. }
    23.  
     
  6. Beguiled

    Beguiled

    Joined:
    Mar 15, 2014
    Posts:
    14
    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!
     
    Gregoryl likes this.
  7. Beguiled

    Beguiled

    Joined:
    Mar 15, 2014
    Posts:
    14
    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.
     
    Gregoryl likes this.