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

Overriding Freecamera Cinemachine inputs or Freecamera axis inversion

Discussion in 'Cinemachine' started by sebsmax, Jul 24, 2017.

  1. sebsmax

    sebsmax

    Joined:
    Sep 8, 2015
    Posts:
    118
    It took me a while to figure it out, and it's fairly simple. So I'm posting that here to save newcomers a bit of time

    Hooking the controls to any external input system:
    Code (CSharp):
    1.      
    2.  
    3.         public void Start()
    4.         {
    5.             CinemachineCore.GetInputAxis = GetAxisCustom;
    6.         }
    7.  
    8.         public float GetAxisCustom(string axisName)
    9.         {
    10.             if(axisName == "Mouse X")
    11.             {
    12.                 return [YOUR INPUT VALUE HERE].x;
    13.             }
    14.             else if (axisName == "Mouse Y")
    15.             {
    16.                 return [YOUR INPUT VALUE HERE].y;
    17.             }
    18.  
    19.             return 0;
    20.         }
    Horizontal Axis inversion:

    Code (CSharp):
    1.      
    2.  
    3.         public void Start()
    4.         {
    5.             CinemachineCore.GetInputAxis = GetAxisCustom;
    6.         }
    7.  
    8.         public float GetAxisCustom(string axisName)
    9.         {
    10.             if(axisName == "Mouse X")
    11.             {
    12.                 return -UnityEngine.Input.GetAxis("Mouse X");
    13.             }
    14.             else
    15.                 return UnityEngine.Input.GetAxis(axisName);          
    16.         }
     
    Last edited: Jul 24, 2017
  2. piginhat

    piginhat

    Joined:
    Feb 17, 2016
    Posts:
    96
    Awesome! Thanks :)
     
  3. Rand_D

    Rand_D

    Joined:
    Oct 24, 2017
    Posts:
    44
    Thanks a ton. Just in time when i was searching insanely for it.
    A suggestion: change the title to Freelook Cinemachine so that it can pop up more on search engine.
     
  4. youngmusa

    youngmusa

    Joined:
    Apr 15, 2018
    Posts:
    13
    Where do you put this code?
     
  5. abitofjohn

    abitofjohn

    Joined:
    Nov 6, 2012
    Posts:
    27
    Thanks! this was driving me crazy!
     
  6. mnenad

    mnenad

    Joined:
    Dec 7, 2015
    Posts:
    18
    Great. Thx a lot for the help!
     
  7. Hellzbellz123

    Hellzbellz123

    Joined:
    May 15, 2019
    Posts:
    12
    sorry to bump but this also works for Cinemachine and unitys new input system.
    take the delta from the mouse and then split the vector2 into .x and .y and then just return it
     
    DARamos97 likes this.
  8. kitchen54

    kitchen54

    Joined:
    Nov 7, 2013
    Posts:
    6
    You write this script and then you put it onto the object that holds the Freelook camera script for cinemachine.
     
  9. malditonuke

    malditonuke

    Joined:
    Apr 15, 2017
    Posts:
    18
    Thank you! This works with controller input systems (like InControl). Works with Free Camera and Virtual Camera too. I'm using it for POV controller input.
     
  10. rivanavur

    rivanavur

    Joined:
    Jun 21, 2020
    Posts:
    4
    hey what should i do with this: [YOUR INPUT VALUE HERE]
    i have no idea i try Mouse X/Mouse Y but it doesnt work
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    This thread is now quite old, and Cinemachine has built-in features to do many of the things that you would need this script for. What are you trying to accomplish?
     
  12. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197
    this doesnt work. any update on getting CM working with mouse and gamepad?
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Can you be more specific about what exactly you're trying to do and why it doesn't work for you?
     
  14. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197
    what is the best solution to code both mouse and game pad input axis with bolt? i can only use bolt. i tried C# for 6 months. I use bolt only now.

    i need to control input axis string name with bolt into cinemachine. or i want to use new input manager but it is too hard and i broke my whole game last time i tried it. can it work with bolt???
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    I'm still having trouble understanding what you're trying to do. Why can't you just set up your input channels in the inspector?
     
  16. zeropointblack

    zeropointblack

    Joined:
    Jun 8, 2020
    Posts:
    197
    Thanks, Greg. As far as I can tell, I can only input one axis string name per free look axis. I would like TWO on each simultaneously. mouse and right analog stick.

    As I understand, I am to leave the axis field BLANK which allows me to code as many as I want. What I'm having trouble finding is what code to use to do such an operation, (and then of course translate that to bolt, if indeed i can.)
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    The easiest way is to make a custom script that inherits AxisState.IInputAxisProvider (see https://docs.unity3d.com/Packages/c...Cinemachine.AxisState.IInputAxisProvider.html). Add that custom script to your vcam. The vcam will find it and ask the script whenever it needs an input value. Your script's implementation of GetAxisValue() will query whatever input source you care about, and return the result.

    Unfortunately I can't help you with Bolt, I have never used it.
     
  18. rehtse_studio

    rehtse_studio

    Joined:
    Jul 25, 2013
    Posts:
    24
    @Gregoryl
    My apologies if I am bothering. Using InputAxisProvider to use cinemachine with the new input works wonderfully but, here’s is where I am stuck: when you add a touchscreen bind inside a Looks action (default input map when creating player input) this will aloud to control cinemachine on mobile when you touch the screen, but if you have a image / joystick with the on-stick script for the new input system also on mobile both input will cross; while moving the player with the joystick the camera also moves.

    Do you have any tips in how to use both input without crossing each other?
     
    Diggu likes this.
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Maybe you can ask on the input forum: https://forum.unity.com/forums/input-system.103/
    My guess is that you have to set up separate actions for player and camera control.
     
  20. rehtse_studio

    rehtse_studio

    Joined:
    Jul 25, 2013
    Posts:
    24
    Both actions are separated. Will try on the input forum ;) thanks.
     
  21. SamuelKeller

    SamuelKeller

    Joined:
    Nov 9, 2020
    Posts:
    9
    Note for anyone confused by this: using the cinemachine namespace is required to use this code correctly.
     
  22. RedMoon50

    RedMoon50

    Joined:
    Feb 22, 2022
    Posts:
    1
    Hi, I'm trying to control cinemachine freeLook with new input system but it doesn't work (can't orbit the camera with mouse),
    I need some example with
    (AxisState.IInputAxisProvider)
     
  23. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    What version of Cinemachine are you using?

    If you have a new enough version of CM and have the Input package installed, then CM will help you set up the input. Create a new FreeLook and look at the inspector. You should see this:

    upload_2022-6-21_8-17-4.png

    Press that button. It will get you started in the right direction.
     
  24. jacobsand

    jacobsand

    Joined:
    Mar 7, 2022
    Posts:
    1
    here is another easy, practical way to do it:
    Make a new script, I called it CamOverride, and put it on the object where you put CinemachineFreeLook.
    axis : 0=X 1=Y 2=Z


    Code (CSharp):
    1. using UnityEngine;
    2. using static Cinemachine.AxisState;
    3.  
    4.  
    5. public class CamOverride : MonoBehaviour, IInputAxisProvider
    6. {
    7.  
    8.      public float GetAxisValue(int axis)
    9.     {
    10.         if (axis == 0) {
    11.             return 0;
    12.         }
    13.         else if (axis == 1)
    14.         {
    15.             return mouseYOrSomething;
    16.         }
    17.         else
    18.         {
    19.             return 0;
    20.         }
    21.     }
    22.  
    23. }
    24.