Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question CinemachineInputProvider not working

Discussion in 'Input System' started by brush_Ooo, Apr 17, 2024.

  1. brush_Ooo

    brush_Ooo

    Joined:
    Jul 17, 2021
    Posts:
    7
    Hello.

    I am using the following system

    Input System
    CinemachineInputProvider

    I need to control Cinemachine with a second finger.

    I have already set up the Input Action to operate it with RightStick, I have attached CinemachineInputProvider to Virtual Camera and Input Action to XYAxis for camera operation.

    However, when built on IOS, the camera barely moves with the second finger.

    In the editor, it works fine with the XBox controller.

    Here is the source code for testing
    I know this is strange English, but don't give up on me.
    Please let me know.

    Code (CSharp):
    1. using UnityEngine.InputSystem.EnhancedTouch;
    2. using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
    3. using TouchPhase = UnityEngine.InputSystem.TouchPhase;
    4. using UnityEngine;
    5. using System.Collections.Generic;
    6. using System;
    7. using UnityEngine.UI;
    8. using UnityEngine.InputSystem.OnScreen;
    9. using UnityEngine.InputSystem.Layouts;
    10.  
    11. public class Test : OnScreenControl
    12. {
    13.     private int touchId = -1;
    14.     private int targetFingerIndex = 1;
    15.     private Dictionary<TouchPhase, Action<Touch>> touchFunctions;
    16.  
    17.     [SerializeField] private float deathPoint = 0.1f;
    18.     [SerializeField] private Graphic joystickBackGround;
    19.     [SerializeField] private Graphic joyStickbutton;
    20.     [InputControl(layout = "Stick"), SerializeField] private string m_ControlPath = "<Gamepad>/rightStick";
    21.  
    22.     private Vector2 deltaDrag;
    23.     private Vector2 axisValue;
    24.  
    25.     public Vector2 AxisValue
    26.     {
    27.         get => this.axisValue;
    28.         private set
    29.         {
    30.             MonoBehaviour.print(this.axisValue); // (40.32, 15.08)
    31.             this.axisValue = value;
    32.             this.SendValueToControl(this.axisValue);
    33.         }
    34.     }
    35.  
    36.     protected override string controlPathInternal
    37.     {
    38.         get => this.m_ControlPath;
    39.         set => this.m_ControlPath = value;
    40.     }
    41.    
    42.     private void Awake()
    43.     {
    44.         EnhancedTouchSupport.Enable();
    45.  
    46.         this.touchFunctions = new Dictionary<TouchPhase, Action<Touch>>()
    47.         {
    48.             {TouchPhase.Began, this.OnDrag},
    49.             {TouchPhase.Moved, this.OnDrag},
    50.             {TouchPhase.Stationary, this.OnStationary},
    51.             {TouchPhase.Canceled, this.OnPointerUp},
    52.             {TouchPhase.Ended, this.OnPointerUp},
    53.         };
    54.     }
    55.    
    56.     private void Update()
    57.     {
    58.         this.GetTouch();
    59.         this.InvokeFunction();
    60.     }
    61.  
    62.     private void GetTouch()
    63.     {
    64.         if (this.touchId != -1) return;
    65.         if (Touch.activeTouches.Count <= this.targetFingerIndex) return;
    66.  
    67.         this.touchId = Touch.activeTouches[this.targetFingerIndex].touchId;
    68.     }
    69.  
    70.     private void InvokeFunction()
    71.     {
    72.         if (this.touchId == -1) return;
    73.        
    74.         for (int i = 0; i < Touch.activeTouches.Count; i++)
    75.         {
    76.             var targetTouch = Touch.activeTouches[i];
    77.             if (targetTouch.touchId != this.touchId) continue;
    78.  
    79.             this.touchFunctions[targetTouch.phase].Invoke(targetTouch);
    80.         }
    81.     }
    82.    
    83.     public void OnDrag(Touch touch)
    84.     {
    85.         Vector2 targetAxis = Vector2.zero;
    86.  
    87.         if (RectTransformUtility.ScreenPointToLocalPointInRectangle(this.joystickBackGround.rectTransform, touch.screenPosition, null, out Vector2 pos))
    88.         {
    89.             this.joyStickbutton.rectTransform.anchoredPosition = pos;
    90.             var relative = pos - this.deltaDrag;
    91.  
    92.             targetAxis = new Vector3(
    93.                 relative.x * Screen.width * 0.001f,
    94.                 relative.y * 0.001f * Screen.height
    95.             );
    96.             this.deltaDrag = pos;
    97.         }
    98.  
    99.         if (targetAxis.magnitude <= this.deathPoint)
    100.         {
    101.             this.AxisValue = Vector2.zero;
    102.         }
    103.         else
    104.         {
    105.             this.AxisValue = targetAxis;
    106.         }
    107.     }
    108.  
    109.     private void OnStationary(Touch touch)
    110.     {
    111.         this.AxisValue = Vector2.zero;
    112.     }
    113.  
    114.     public void OnPointerUp(Touch _)
    115.     {
    116.         this.deltaDrag = this.AxisValue = Vector2.zero;
    117.         this.touchId = -1;
    118.     }
    119. }
     
  2. brush_Ooo

    brush_Ooo

    Joined:
    Jul 17, 2021
    Posts:
    7
    I planted a print in the CinemachineInputProvider.GetAxisValue method, but the value displayed was (0.00, 0.00).

    It seems that the player index is not set properly, so I will look into this a bit more.

    However, it is a mystery why there are still moments when the camera can be manipulated a bit.
     
  3. brush_Ooo

    brush_Ooo

    Joined:
    Jul 17, 2021
    Posts:
    7
    Maybe it is a bug.

    OnScreenControl.SendValueToControl is not working.

    SendValueToControl in the Update method for testing, it works, but if you call SendValueToControl in another method, it does not work.

    Code (CSharp):
    1. private void Update()
    2. {
    3.     this.SendValueToControl(new Vector2(0.5f, 0));
    4. }
     
  4. brush_Ooo

    brush_Ooo

    Joined:
    Jul 17, 2021
    Posts:
    7
    It may not be a problem with OnScreenControl.SendValueToControl.

    OnValueToControl.SendValueToControl and use that value for other operations instead of Cinemachine, it works fine, so I suspect it is a CinemachineInputProvider issue.

    But I don't understand why it works if I call the SendValueToControl method directly within the Update method.
     
  5. brush_Ooo

    brush_Ooo

    Joined:
    Jul 17, 2021
    Posts:
    7