Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    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:
    116
    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. TrongHieu_Designer

    TrongHieu_Designer

    Joined:
    Oct 24, 2017
    Posts:
    5
    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:
    11
    Where do you put this code?
     
  5. abitofjohn

    abitofjohn

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

    MihajloNen

    Joined:
    Dec 7, 2015
    Posts:
    16
    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
     
  8. kitchen54

    kitchen54

    Joined:
    Nov 7, 2013
    Posts:
    5
    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:
    16
    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:
    1
    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:
    4,513
    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:
    140
    this doesnt work. any update on getting CM working with mouse and gamepad?
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,513
    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:
    140
    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:
    4,513
    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:
    140
    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:
    4,513
    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. emfpc

    emfpc

    Joined:
    Jul 25, 2013
    Posts:
    13
    @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?
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    4,513
    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. emfpc

    emfpc

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