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

How do I make a CinemachineFreeLook orbiting camera that only orbits when the mouse key is down?

Discussion in 'Cinemachine' started by WarpZone, Apr 19, 2018.

  1. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    I've tried toggling the CinemachineFreeLook object on and off, and even tried editing the script itself to check the mouse button in an if statement any time it messes with the Transform. Nothing seems to work.

    I just want the camera to orbit when the player right-clicks and drags, instead of anytime the player moves the mouse. At all other times, I want the mouse to just be a normal mouse cursor that doesn't affect the camera at all. I just want a simple on-mouse-down orbital camera. Not an always-on orbital camera.
     
    juher92 likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    By default the FreeLook queries the input manager for MouseX and Mouse Y and responds to that. You can intercept this call and return 0 unless the mouse button is down.

    See CinemachineCore.GetInputAxis. Override that with your custom delegate.
     
    Dieter_Dagger likes this.
  3. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Uhhhh... is there an example script anywhere I could look at? Or?

    You just reminded me, I also tried getting and setting the Input Axis Values directly from another script, but that didn't work either.
     
    Last edited: Apr 20, 2018
  4. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Squize and lukeiszed like this.
  5. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class CMFreelookOnlyWhenRightMouseDown : MonoBehaviour {
    7.     void Start(){
    8.         CinemachineCore.GetInputAxis = GetAxisCustom;
    9.     }
    10.     public float GetAxisCustom(string axisName){
    11.         if(axisName == "Mouse X"){
    12.             if (Input.GetMouseButton(1)){
    13.                 return UnityEngine.Input.GetAxis("Mouse X");
    14.             } else{
    15.                 return 0;
    16.             }
    17.         }
    18.         else if (axisName == "Mouse Y"){
    19.             if (Input.GetMouseButton(1)){
    20.                 return UnityEngine.Input.GetAxis("Mouse Y");
    21.             } else{
    22.                 return 0;
    23.             }
    24.         }
    25.         return UnityEngine.Input.GetAxis(axisName);
    26.     }
    27. }
     
    Last edited: Apr 30, 2018
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Excellent!
    May I suggest that instead of returning 0 on the last line, you do
    Code (CSharp):
    1. return UnityEngine.Input.GetAxis(axisName);
    That way you're not messing up all the other input channels.
     
  7. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Great idea!

    And is there a way to zoom in and out? With like the mousewheel?
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    The way to do that would be to make a custom script component alongside the FreeLook that looks at the mousewheel input and scales the radii in the FreeLook's orbits.
     
  9. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Oh, so just... access it directly through like myCinemachineFreeLook.m_Orbits[0].m_Radius = 3.5f and the like?
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
  11. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    I wanted to cache the orbits in my GameManager script so I could multiply their default settings by a zoom level, rather than explicitly defining them each by hand, but I ran into a snag:

    Code (CSharp):
    1. public Orbit[] m_Orbits;
    ...gives me...
    ...but I can't figure out what to Include. (The Orbit struct is defined locally in CinemachineFreeLook.cs.)
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Code (CSharp):
    1. using Cinemachine;
    2. [...]
    3. public CinemachineFreeLook.Orbit m_Orbits;
     
    janisrisa likes this.
  13. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Oh wow I didn't realize you could grab something like CinemachineFreeLook.Orbit like that.
     
  14. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Isn't anything ending in [] a builtin array?
     
  15. andrescuevas

    andrescuevas

    Joined:
    Apr 23, 2019
    Posts:
    24
    Hello Im trying to get this to work. i stick the script onto my CMFL cam but it does nothing. what am I doing wrong?
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Is it possible that you have another script somewhere that is also setting
    CinemachineCore.GetInputAxis
    ?
     
  17. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,105
    it works, script needs to go on the camera the one where the cinemachine brain script is located.

    if you have the "Simple Camera Controller Script" enabled you need to disable it. That script will interfere with pretty much anything that want to control the camera.
     
  18. Cyber-Squire

    Cyber-Squire

    Joined:
    Jun 4, 2021
    Posts:
    1
    I am Very confused, where do I make the component for this script? I tried the Main Camera but it didn't have any effect or error.
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Normally, putting that script on the main camera (or on any other active object) will work. Possibly you have some other scripts or settings that are interfering. Try it on a fresh project, or on one of the CM sample scenes.
     
  20. ZouiPl

    ZouiPl

    Joined:
    Aug 21, 2022
    Posts:
    1
    Uhhhhh, I might be a bit late? But I'll try qwq

    So the code is mostly clear for me, but what should I do with it?

    I tried putting it into the main camera (with Cinemachine brain) but that doesn't work
     
  21. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    What code are you talking about? Can you show it?