Search Unity

Mobile Joystick and Cinemachine Touch Input

Discussion in 'Cinemachine' started by turkinnovations, Dec 10, 2019.

  1. turkinnovations

    turkinnovations

    Joined:
    May 18, 2019
    Posts:
    2
    Hey, I'm trying to make use of the Cinemachine free look for mobile, but the touch input (Mouse X/Y) interferes with the standard assets joystick H/V axes, controlling both at the same time. I've looked into Event.Use() with no luck, how do I fix this?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Can you set up new input channels to drive your CM camera? Don't use the standard ones.
     
  3. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    Are you getting anywhere with this? let me know.

    Bye,

    Jean
     
  4. turkinnovations

    turkinnovations

    Joined:
    May 18, 2019
    Posts:
    2
    Hey @Jean-Fabre, apologies for not replying sooner. I'm still having trouble fixing the problem. I've tried isolating the CM freelook input to Unity's touch pad Standard asset prefab, but the camera was unresponsive.

    @Gregoryl I duplicated the Mouse Y & Mouse X (input manager) and renamed them to Touch Y & Touch X. From there, i changed the Freelook axis input's to Touch X/Y, that was also not moving the camera.

    @Jean-Fabre i've seen mobile games like Honkai Impact 3rd isolate Camera controls to the right half of the screen. Is there a way i can get CinemachineCore to override via touch pad/Ui Sprite? Unity' touch pad prefab looks like this:


    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. namespace UnityStandardAssets.CrossPlatformInput
    7. {
    8.     [RequireComponent(typeof(Image))]
    9.     public class TouchPad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    10.     {
    11.         // Options for which axes to use
    12.         public enum AxisOption
    13.         {
    14.             Both, // Use both
    15.             OnlyHorizontal, // Only horizontal
    16.             OnlyVertical // Only vertical
    17.         }
    18.  
    19.  
    20.         public enum ControlStyle
    21.         {
    22.             Absolute, // operates from the center of the image
    23.             Relative, // operates from the center of the initial touch
    24.             Swipe, // swipe to touch touch no maintained center
    25.         }
    26.  
    27.  
    28.         public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
    29.         public ControlStyle controlStyle = ControlStyle.Absolute; // control style to use
    30.         public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
    31.         public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
    32.         public float Xsensitivity = 1f;
    33.         public float Ysensitivity = 1f;
    34.  
    35.         Vector3 m_StartPos;
    36.         Vector2 m_PreviousDelta;
    37.         Vector3 m_JoytickOutput;
    38.         bool m_UseX; // Toggle for using the x axis
    39.         bool m_UseY; // Toggle for using the Y axis
    40.         CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
    41.         CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
    42.         bool m_Dragging;
    43.         int m_Id = -1;
    44.         Vector2 m_PreviousTouchPos; // swipe style control touch
    45.  
    46.  
    47. #if !UNITY_EDITOR
    48.     private Vector3 m_Center;
    49.     private Image m_Image;
    50. #else
    51.         Vector3 m_PreviousMouse;
    52. #endif
    53.  
    54.         void OnEnable()
    55.         {
    56.             CreateVirtualAxes();
    57.         }
    58.  
    59.         void Start()
    60.         {
    61.  
    62. #if !UNITY_EDITOR
    63.             m_Image = GetComponent<Image>();
    64.             m_Center = m_Image.transform.position;
    65. #endif
    66.         }
    67.  
    68.         void CreateVirtualAxes()
    69.         {
    70.             // set axes to use
    71.             m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
    72.             m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
    73.  
    74.             // create new axes based on axes to use
    75.             if (m_UseX)
    76.             {
    77.                 m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
    78.                 CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
    79.             }
    80.             if (m_UseY)
    81.             {
    82.                 m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
    83.                 CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
    84.             }
    85.         }
    86.  
    87.         void UpdateVirtualAxes(Vector3 value)
    88.         {
    89.             value = value.normalized;
    90.             if (m_UseX)
    91.             {
    92.                 m_HorizontalVirtualAxis.Update(value.x);
    93.             }
    94.  
    95.             if (m_UseY)
    96.             {
    97.                 m_VerticalVirtualAxis.Update(value.y);
    98.             }
    99.         }
    100.  
    101.  
    102.         public void OnPointerDown(PointerEventData data)
    103.         {
    104.             m_Dragging = true;
    105.             m_Id = data.pointerId;
    106. #if !UNITY_EDITOR
    107.         if (controlStyle != ControlStyle.Absolute )
    108.             m_Center = data.position;
    109. #endif
    110.         }
    111.  
    112.         void Update()
    113.         {
    114.             if (!m_Dragging)
    115.             {
    116.                 return;
    117.             }
    118.             if (Input.touchCount >= m_Id + 1 && m_Id != -1)
    119.             {
    120. #if !UNITY_EDITOR
    121.  
    122.             if (controlStyle == ControlStyle.Swipe)
    123.             {
    124.                 m_Center = m_PreviousTouchPos;
    125.                 m_PreviousTouchPos = Input.touches[m_Id].position;
    126.             }
    127.             Vector2 pointerDelta = new Vector2(Input.touches[m_Id].position.x - m_Center.x , Input.touches[m_Id].position.y - m_Center.y).normalized;
    128.             pointerDelta.x *= Xsensitivity;
    129.             pointerDelta.y *= Ysensitivity;
    130. #else
    131.                 Vector2 pointerDelta;
    132.                 pointerDelta.x = Input.mousePosition.x - m_PreviousMouse.x;
    133.                 pointerDelta.y = Input.mousePosition.y - m_PreviousMouse.y;
    134.                 m_PreviousMouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);
    135. #endif
    136.                 UpdateVirtualAxes(new Vector3(pointerDelta.x, pointerDelta.y, 0));
    137.             }
    138.         }
    139.  
    140.  
    141.         public void OnPointerUp(PointerEventData data)
    142.         {
    143.             m_Dragging = false;
    144.             m_Id = -1;
    145.             UpdateVirtualAxes(Vector3.zero);
    146.         }
    147.  
    148.         void OnDisable()
    149.         {
    150.             if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
    151.                 CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
    152.  
    153.             if (CrossPlatformInputManager.AxisExists(verticalAxisName))
    154.                 CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
    155.         }
    156.     }
    157. }
     
  5. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    yes, you can do that, take over the cinemachine input and feed this with the on screen joystick system

    have you checked
    CinemachineCoreGetInputTouchAxis.cs component?

    simply replace the touch input feeds with your on screen joystick values, also, you will likely want to change the way the input works, as mouse/touch input are absolute movement, but you may want your on screen joystick to move the camera in relative mode ( that is, if you keep the joystick left, the camera will keep rotating). But that's already in the second phase of the implementation, first, modify CinemachineCoreGetInputTouchAxis.cs to react to your on screen joystick somehow.

    Bye,

    Jean
     
    Gregoryl likes this.
  6. Blarp

    Blarp

    Joined:
    May 13, 2014
    Posts:
    269
    Looking to setup up something very similar to this in the new inputer system. On-screen joystick for movement and touch for camera orbit.

    Probably the #1 character control system for mobile. Would be great to have a tangible example from our Unity overlords to adopt this quicker!
     
  7. romi-fauzi

    romi-fauzi

    Joined:
    Aug 16, 2013
    Posts:
    161
    @Blarp , here is the modification from the CinemachineGetInputTouchAxis.cs, I've created to make the cinemachine works with the CrossPlatformInput touch control from the StandardAssets. Just attach it to the Cinemachine virtual camera (free look or 3rd person cam), and the touch screen joystic with axes set to Mouse X and Mouse Y will drive the Cinemachine Camera, hope this helps!


    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput;
    4.  
    5. /// <summary>
    6. /// This is an add-on behaviour that globally maps the touch control
    7. /// to standard input channels, such as mouse X and mouse Y.
    8. /// Drop it on any game object in your scene.
    9. /// </summary>
    10. public class CinemachineCrossPlatformInput : MonoBehaviour
    11. {
    12.     /// <summary>Sensitivity multiplier for x-axis</summary>
    13.     [Tooltip("Sensitivity multiplier for x-axis")]
    14.     public float TouchSensitivityX = 3f;
    15.  
    16.     /// <summary>Sensitivity multiplier for y-axis</summary>
    17.     [Tooltip("Sensitivity multiplier for y-axis")]
    18.     public float TouchSensitivityY = 3f;
    19.  
    20.     /// <summary>Input channel to spoof for X axis</summary>
    21.     [Tooltip("Input channel to spoof for X axis")]
    22.     public string TouchXInputMapTo = "Mouse X";
    23.  
    24.     /// <summary>Input channel to spoof for Y axis</summary>
    25.     [Tooltip("Input channel to spoof for Y axis")]
    26.     public string TouchYInputMapTo = "Mouse Y";
    27.  
    28.     void Start()
    29.     {
    30.         CinemachineCore.GetInputAxis = GetInputAxis;
    31.     }
    32.  
    33.     private float GetInputAxis(string axisName)
    34.     {
    35.         if (axisName == TouchXInputMapTo)
    36.             return CrossPlatformInputManager.GetAxis(TouchXInputMapTo) * TouchSensitivityX;
    37.         if (axisName == TouchYInputMapTo)
    38.             return CrossPlatformInputManager.GetAxis(TouchYInputMapTo) * TouchSensitivityY;
    39.  
    40.         return CrossPlatformInputManager.GetAxis(axisName);
    41.     }
    42. }
    43.  
    44.