Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    691
    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?
     
    ugotstoopt likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    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:
    691
    Ooh I wasn't aware there was a hook now, that helps a lot!! Thanks :)
     
  4. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    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:
    122
    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:
    716
    I've edited the code.
     
    Jjoosshhb and MSachs like this.
  7. MSachs

    MSachs

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

    DeidreReay

    Joined:
    Oct 28, 2019
    Posts:
    49
    Hey is this still working on newer blender and cinemachine?
     
  10. dkbozkurt

    dkbozkurt

    Joined:
    Feb 14, 2020
    Posts:
    1
    It still works fine in Unity's newer versions. Thank you mate.