Search Unity

TouchPad.cs Sensivity bug

Discussion in 'Android' started by Xazerek, May 10, 2015.

  1. Xazerek

    Xazerek

    Joined:
    Nov 3, 2014
    Posts:
    134
    Do u know how to change sensivity in this scripts? This is not working - "public float Ysensitivity = 1f;".

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

    Acagamesru

    Joined:
    Mar 2, 2016
    Posts:
    5
    any can help? I have the same problem, work in 1e-05
     
    Last edited: Mar 28, 2017
    sj631 likes this.
  3. MSP3

    MSP3

    Joined:
    Dec 11, 2017
    Posts:
    2
    Did you manage to resolve this?
     
    sj631 likes this.
  4. Hansel41

    Hansel41

    Joined:
    Mar 15, 2020
    Posts:
    10
    still waitin' for the answer, Stuck at sensitivity problem in touchpad.cs
    When i move my finger slowly, the sensitivity is too high to resist, but i move my finger fast the sensitivity is then incredibly low, Any help would be appriciated. Thankyou in advance
     
    sj631 likes this.
  5. anilaltundag

    anilaltundag

    Joined:
    May 27, 2020
    Posts:
    1
    Code (CSharp):
    1.  void UpdateVirtualAxes(Vector3 value)
    2.         {
    3.             value = value.normalized;
    4.             if (m_UseX)
    5.             {
    6.                 m_HorizontalVirtualAxis.Update(value.x * Xsensitivity);//
    7.             }
    8.  
    9.             if (m_UseY)
    10.             {
    11.                 m_VerticalVirtualAxis.Update(value.y * Ysensitivity);//
    12.             }
    13.         }
    This is 86th tile of Touchpad Script. It solved the problem for me.
     
  6. sj631

    sj631

    Joined:
    Dec 23, 2016
    Posts:
    22
    When i move my finger slowly, the sensitivity is too high to resist, but i move my finger fast the sensitivity is then incredibly low