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

Problems with toggling CinemachineCore.GetInputAxis

Discussion in 'Cinemachine' started by piginhat, Aug 19, 2019.

  1. piginhat

    piginhat

    Joined:
    Feb 17, 2016
    Posts:
    96
    On my mobile project I am including the option for the user to control the CMFreeLook via either joystick or finger and use this code to toggle:

    Code (CSharp):
    1.  
    2. public void SetCustomAxis(bool useCustomAxis) {
    3.  
    4.         if (useCustomAxis) {
    5.  
    6.             CinemachineCore.GetInputAxis += GetAxisCustom;
    7.         }
    8.         else {
    9.  
    10.             CinemachineCore.GetInputAxis -= GetAxisCustom;
    11.         }
    12.     }
    13.  
    Which works sometimes and then other not, so if I start out using finger control on start this method is called passing in false and the user can indeed use their finger to control the camera.

    When the use then changes the setting so they can use the joystick this method is called passing in true and the user can indeed now control the camera via the joystick input.

    But if now the user decides to revert to finger control and this method is called passing in false the finger gestures are ignored and the camera does not move?
     
  2. dave24BG

    dave24BG

    Joined:
    Jun 12, 2018
    Posts:
    9
    Perhaps it is not unsubscribing from that GetAxisCustom method?

    You could maybe try setting CinemachineCore.GetInputAxis += GetAxisCustom; once in OnEnable() or Awake(), then have this override always on, and rather change what values are returned from the GetAxisCustom method depending on the useCustomAxis bool.

    So if useCustom axis is true, say for "Horizontal" return what ever values are generated by touch, then if it is false, return the values generated by the joystick. (all inside of GetAxisCustom)

    Code (CSharp):
    1.  
    2. public float GetAxisCustom(string axisName) {
    3.         if (axisName == "Horizontal") {
    4.                 if(useCustomAxis)
    5.                 {
    6.                       return customAxis.x;
    7.                 }
    8.                 else
    9.                 {
    10.                       return normalAxis.x;
    11.                 }
    12.         }
    13.         if (axisName == "Vertical") {
    14.                 if(useCustomAxis)
    15.                 {
    16.                       return customAxis.y;
    17.                 }
    18.                 else
    19.                 {
    20.                       return normalAxis.y;
    21.                 }
    22.         }
    23.         return 0.0f;
    24. }
    25.                
    26.   .........................
    27.  
    28.  
     
    Last edited: Aug 26, 2019
    piginhat and Gregoryl like this.
  3. piginhat

    piginhat

    Joined:
    Feb 17, 2016
    Posts:
    96

    Thanks, I will give that a go and let you know the results :)
     
    dave24BG likes this.
  4. piginhat

    piginhat

    Joined:
    Feb 17, 2016
    Posts:
    96
    Alas no I don't see how it can work that way because I can obtain the custom x/y when in that mode but when using the default Unity input that is controlled by Cinemachine, so I can't do normalAxis.x etc

    :-(
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    CinemachineCore.GetInputAxis is initialized by default to Input.GetAxis. Use that to get the normal values.

    So, your code in OnEnable or Awake should do:
    Code (CSharp):
    1. CinemachineCore.GetInputAxis = GetAxisCustom;
    Then, in GetAxisCustom, you should do something like
    Code (CSharp):
    1. float GetAxisCustom(string axisName)
    2. {
    3.     if (useCustomAxis)
    4.     {
    5.         if (axisName == "Horizontal")
    6.             return customAxis.x;
    7.         if (axisName == "Vertical")
    8.             return customAxis.y;
    9.     }
    10.     return Input.GetAxis(axisName);
    11. }
     
    piginhat and dave24BG like this.
  6. dave24BG

    dave24BG

    Joined:
    Jun 12, 2018
    Posts:
    9
    epic
     
  7. piginhat

    piginhat

    Joined:
    Feb 17, 2016
    Posts:
    96
    Apologies for delay....holidays etc!

    This works PERFECT! Thanks for this, appreciated :)
     
    Gregoryl likes this.