Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

How to lock free look mouse rotation and set camera behind player?

Discussion in 'Cinemachine' started by IEdge, Dec 15, 2018.

  1. IEdge

    IEdge

    Joined:
    Mar 25, 2017
    Posts:
    51
    Hi guys!

    In my game sometimes I need to block the free look camera mouse rotation and set it just behind the player. Here a video demostration:



    How can I achieve this?
     
  2. IEdge

    IEdge

    Joined:
    Mar 25, 2017
    Posts:
    51
    I have modified a bit the script of this answer, however when I stop pressing the activator key (in this case Q), the camera returns to its previous orbit rotation. I'm using Binding Mode as World Space:




    The script:
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3. using Cinemachine.Utility;
    4. using DG.Tweening;
    5.  
    6. public class ZTargeting : MonoBehaviour {
    7.     public string input;
    8.     public float recenterTime = .5f;
    9.     public DOTweenAnimation targetBorderTop;
    10.     public DOTweenAnimation targetBorderDown;
    11.     [HideInInspector] public bool targetActive;
    12.  
    13.     private bool _recenter;
    14.     private bool _targetBordersActive;
    15.     private CinemachineFreeLook _cm_freeLook;
    16.     private CinemachineOrbitalTransposer[] _orbital = new CinemachineOrbitalTransposer[3];
    17.     private CinemachineVirtualCamera[] _rigs = new CinemachineVirtualCamera[3];
    18.  
    19.     void Start() {
    20.         _cm_freeLook = GetComponent<CinemachineFreeLook>();
    21.         for (int i = 0; _cm_freeLook != null && i < 3; ++i) {
    22.             _rigs[i] = _cm_freeLook.GetRig(i);
    23.             _orbital[i] = _rigs[i].GetCinemachineComponent<CinemachineOrbitalTransposer>();
    24.         }
    25.     }
    26.     void Update() {
    27.         targetActive = Input.GetKey(KeyCode.Q);
    28.  
    29.         if (targetActive && !_targetBordersActive)
    30.             ShowBorders(true);
    31.         else if (!targetActive && _targetBordersActive)
    32.             ShowBorders(false);
    33.  
    34.         var target = _cm_freeLook != null ? _cm_freeLook.Follow : null;
    35.  
    36.         if (target == null)
    37.             return;
    38.         // Disable the transposers while recentering
    39.         for (int i = 0; i < 3; ++i)
    40.             _orbital[i].enabled = !_recenter;
    41.         if (_recenter) {
    42.             // How far away from centered are we?
    43.             var up = _cm_freeLook.State.ReferenceUp;
    44.             var back = _cm_freeLook.transform.position - target.position;
    45.             var angle = UnityVectorExtensions.SignedAngle(back.ProjectOntoPlane(up), -target.forward.ProjectOntoPlane(up), up);
    46.            
    47.             if (Mathf.Abs(angle) < UnityVectorExtensions.Epsilon)
    48.                 _recenter = false; // done!
    49.             // Do the recentering on all 3 _rigs
    50.             angle = Damper.Damp(angle, recenterTime, Time.deltaTime);
    51.  
    52.             for (int i = 0; _recenter && i < 3; ++i)
    53.                 _rigs[i].transform.position = Quaternion.AngleAxis(angle, up) *
    54.                                               (_rigs[i].transform.position - target.position) + target.position;
    55.         }
    56.     }
    57.  
    58.     void ShowBorders(bool flag) {
    59.         _targetBordersActive = flag;
    60.  
    61.         if (_targetBordersActive) {
    62.             _recenter = true;
    63.             targetBorderTop.DORestart();
    64.             targetBorderDown.DORestart();
    65.         } else {
    66.             targetBorderTop.DOPlayBackwards();
    67.             targetBorderDown.DOPlayBackwards();
    68.         }
    69.     }
    70. }
     

    Attached Files:

    • 2.png
      2.png
      File size:
      145.2 KB
      Views:
      7,625
    • a111.png
      a111.png
      File size:
      154.8 KB
      Views:
      7,562
  3. IEdge

    IEdge

    Joined:
    Mar 25, 2017
    Posts:
    51
    I've solved it partially, basically the answer was always there, it was just mistake on my part to not see it :), sorry guys:



    However, this does not recenter the yAxis. I can simply set the value manually (vcam.m_YAxis.Value = .5f) but there would not be a smoothed rotation, so my next question is: How can I recenter the yAxis with a smoothed rotation?

    EDIT: Finally I solve the issue, here the code, maybe it could be useful to someone:
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3. using DG.Tweening;
    4.  
    5. public class ZTargeting : MonoBehaviour {
    6.  
    7.     [HideInInspector] public bool active;
    8.     public string inputButton;
    9.     public float yAxisRecenterValue = .5f;
    10.     public float yAxisRecenterSpeed = .5f;
    11.     public DOTweenAnimation targetBorderTop;
    12.     public DOTweenAnimation targetBorderDown;
    13.  
    14.     private bool _locked;
    15.     private bool _updateYAxis;
    16.     private CinemachineFreeLook _cm_freeLook;
    17.  
    18.     void Start() {
    19.         _cm_freeLook = GetComponent<CinemachineFreeLook>();
    20.     }
    21.  
    22.     void SetActive(bool flag) {
    23.         _locked = flag;
    24.         _cm_freeLook.m_RecenterToTargetHeading.m_enabled = flag;
    25.     }
    26.  
    27.     void StopRecenterYAxis() {
    28.         _updateYAxis = false;
    29.         _cm_freeLook.m_YAxis.Value = yAxisRecenterValue;
    30.     }
    31.     void Update() {
    32.         if (!Link.Enabled)
    33.             return;
    34.  
    35.         active = Input.GetButton(inputButton);
    36.  
    37.         if (active && !_locked) {
    38.             SetActive(true);
    39.  
    40.             _updateYAxis = _cm_freeLook.m_YAxis.Value != yAxisRecenterValue;
    41.             _cm_freeLook.m_XAxis.m_InputAxisValue = 0;
    42.             _cm_freeLook.m_YAxis.m_InputAxisValue = 0;
    43.             _cm_freeLook.m_XAxis.m_InputAxisName = string.Empty;
    44.             _cm_freeLook.m_YAxis.m_InputAxisName = string.Empty;
    45.  
    46.             targetBorderTop.DORestart();
    47.             targetBorderDown.DORestart();
    48.  
    49.         } else if (!active && _locked) {
    50.             SetActive(false);
    51.  
    52.             _cm_freeLook.m_XAxis.m_InputAxisName = "Mouse X";
    53.             _cm_freeLook.m_YAxis.m_InputAxisName = "Mouse Y";
    54.  
    55.             targetBorderTop.DOPlayBackwards();
    56.             targetBorderDown.DOPlayBackwards();
    57.         }
    58.  
    59.         if (_updateYAxis)
    60.             if (_cm_freeLook.m_YAxis.Value > yAxisRecenterValue) {
    61.                 _cm_freeLook.m_YAxis.Value -= yAxisRecenterSpeed *
    62.                                               (Time.deltaTime / _cm_freeLook.m_RecenterToTargetHeading.m_RecenteringTime);
    63.                 if (_cm_freeLook.m_YAxis.Value <= yAxisRecenterValue)
    64.                     StopRecenterYAxis();
    65.             } else if (_cm_freeLook.m_YAxis.Value < yAxisRecenterValue) {
    66.                 _cm_freeLook.m_YAxis.Value += yAxisRecenterSpeed *
    67.                                               (Time.deltaTime / _cm_freeLook.m_RecenterToTargetHeading.m_RecenteringTime);
    68.                 if (_cm_freeLook.m_YAxis.Value >= yAxisRecenterValue)
    69.                     StopRecenterYAxis();
    70.             }
    71.     }
    72. }
     

    Attached Files:

    Last edited: Dec 17, 2018
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,744
    Why not just use the builtin Y Axis recentering function?

    upload_2018-12-17_12-54-51.png
     
    realrobet and IEdge like this.
  5. IEdge

    IEdge

    Joined:
    Mar 25, 2017
    Posts:
    51
    I guess it's because I'm using CM from the asset store. I'll have to update, thanks :)