Search Unity

  1. We want you to join us at GDC this year! Take a peek at all of the events we will be hosting during the week of GDC.
    Dismiss Notice
  2. Tell us about your experience here and you’ll get early access to the 2018 Game Studios report + more goodies.
    Dismiss Notice
  3. Unity 2017.3 has arrived! Read about it here.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  5. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    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:
    493
    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:
    787
    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:
    493
    Ooh I wasn't aware there was a hook now, that helps a lot!! Thanks :)
     
  4. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    694
    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
  5. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    45
    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:
    694
    I've edited the code.
     
    MSachs likes this.
  7. MSachs

    MSachs

    Joined:
    Nov 22, 2017
    Posts:
    45
    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:
    45
    I figuered it out. I put the script on the individual virtual cameras but putting it on the main camera solved it.