Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Can the freelook camera's motion be limited by additional keys?

Discussion in 'Cinemachine' started by Sendatsu_Yoshimitsu, Dec 7, 2017.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    680
    I would ideally like the freelook camera to only move or zoom when I'm holding a mouse button/key, and to ignore mouse motion and remain where it is when that key isn't depressed. Since the freelook camera is currently extremely clunky with seemingly hardcoded axes, the best solution I've come up with is manually positioning a second, stationary camera directly on top of the freelook camera, and switching between the two when I press a key. This very technically works, but it's incredibly clunky; is there any chance the API supports more nuanced controls for freelook?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,228
    Axes are not hardcoded. You can put any axis names you like, and input manager will be queried for the values. There is also a hook in CinemachineCore that allows you to provide a custom function to get the values, replacing the input manager.
     
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    680
    Ooh I wasn't aware there was a hook now, that helps a lot!! Thanks :)
     
  4. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    709
    In case someone comes across this wants to save 15mins nutting it out themselves here is some code:

    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3.  
    4. [RequireComponent(typeof(CinemachineFreeLook))]
    5. public class FreeLookUserInput : MonoBehaviour
    6. {
    7.     private bool _freeLookActive;
    8.  
    9.     // Use this for initialization
    10.     private void Start ()
    11.     {
    12.         CinemachineCore.GetInputAxis = GetInputAxis;
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.         _freeLookActive = Input.GetMouseButton(1); // 0 = left mouse btn or 1 = right
    18.     }
    19.  
    20.     private float GetInputAxis(string axisName)
    21.     {
    22.         return !_freeLookActive ? 0 : Input.GetAxis(axisName == "Mouse Y" ? "Mouse Y" : "Mouse X");
    23.     }
    24. }
    Add this as an component to the Free Look camera and it should work fine with its default settings.

    Cheers.
     
    Last edited: Feb 12, 2018
    Jjoosshhb, Squize and kaaaaaskop like this.
  5. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    105
    I'm new to Unity so I don't really get what this means... When I just add your code it doesn't recognize "MouseButton" and "MouseButton.Right". What do I have to do with this so it gets recognized? :)
     
  6. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    709
    I've edited the code.
     
    Jjoosshhb and MSachs like this.
  7. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    105
    Thank you! It works but somehow only on one camera... when I put the script on multiple freelook cameras and blend between them it still only works on one. The other cameras are just static. What could be the reason for that? :)
     
  8. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    105
    I figuered it out. I put the script on the individual virtual cameras but putting it on the main camera solved it.
     
  9. DeidreRae

    DeidreRae

    Joined:
    Oct 28, 2019
    Posts:
    30
    Hey is this still working on newer blender and cinemachine?
     
unityunity