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

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.

1 Like

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.

3 Likes

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.

Found an example of using delegates with Cinemachine.Core.GetInputAxis. Overriding Freecamera Cinemachine inputs or Freecamera axis inversion (Linked for the benefit of people living in The Future whom google randomly dumps here.)

Sorry for not taking more initiative, but I’ve had some terrible experiences with delegates before in Unity.

2 Likes
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class CMFreelookOnlyWhenRightMouseDown : MonoBehaviour {
    void Start(){
        CinemachineCore.GetInputAxis = GetAxisCustom;
    }
    public float GetAxisCustom(string axisName){
        if(axisName == "Mouse X"){
            if (Input.GetMouseButton(1)){
                return UnityEngine.Input.GetAxis("Mouse X");
            } else{
                return 0;
            }
        }
        else if (axisName == "Mouse Y"){
            if (Input.GetMouseButton(1)){
                return UnityEngine.Input.GetAxis("Mouse Y");
            } else{
                return 0;
            }
        }
        return UnityEngine.Input.GetAxis(axisName);
    }
}
14 Likes

Excellent!
May I suggest that instead of returning 0 on the last line, you do return UnityEngine.Input.GetAxis(axisName);
That way you’re not messing up all the other input channels.

5 Likes

Great idea!

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

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.

Oh, so just… access it directly through like myCinemachineFreeLook.m_Orbits[0].m_Radius = 3.5f and the like?

yes

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:

public Orbit[] m_Orbits;

…gives me…

…but I can’t figure out what to Include. (The Orbit struct is defined locally in CinemachineFreeLook.cs.)

using Cinemachine;
[...]
public CinemachineFreeLook.Orbit m_Orbits;
1 Like

Oh wow I didn’t realize you could grab something like CinemachineFreeLook.Orbit like that.

Isn’t anything ending in [ ] a builtin array?

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?

Is it possible that you have another script somewhere that is also setting CinemachineCore.GetInputAxis?

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.

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.

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.

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