Search Unity

Can't get touchpad to increase sensitivity as distance between start position increases

Discussion in 'Scripting' started by TimTro, Aug 25, 2015.

  1. TimTro

    TimTro

    Joined:
    Jun 1, 2015
    Posts:
    20
    Hi all, I'm using Unity's Cross Platform Input assets to add controls to my scenes camera. I am using the Dual Touch Controls with one half disabled and the other half taking up the whole screen. The user can press their finger down anywhere and then drag to move around.

    The problem is they always move at a constant speed. If they barely drag out from the center they would be going the same speed as if they swiped all the way across the screen. The joystick code does what I'm looking for more closely in that regard but the position of that is fixed and must be visible.

    Any help would be greatly appreciated! Here are the scripts I'm using so far:

    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.          public enum ControlStyle
    19.          {
    20.              Absolute, // operates from teh center of the image
    21.              Relative, // operates from the center of the initial touch
    22.              Swipe, // swipe to touch touch no maintained center
    23.          }
    24.          public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
    25.          public ControlStyle controlStyle = ControlStyle.Absolute; // control style to use
    26.          public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
    27.          public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
    28.          public float Xsensitivity = 1f;
    29.          public float Ysensitivity = 1f;
    30.          Vector3 m_StartPos;
    31.          Vector2 m_PreviousDelta;
    32.          Vector3 m_JoytickOutput;
    33.          bool m_UseX; // Toggle for using the x axis
    34.          bool m_UseY; // Toggle for using the Y axis
    35.          CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
    36.          CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
    37.          bool m_Dragging;
    38.          int m_Id = -1;
    39.          Vector2 m_PreviousTouchPos; // swipe style control touch
    40. #if !UNITY_EDITOR
    41.      private Vector3 m_Center;
    42.      private Image m_Image;
    43. #else
    44.          Vector3 m_PreviousMouse;
    45. #endif
    46.          void OnEnable()
    47.          {
    48.              CreateVirtualAxes();
    49.          }
    50.          void Start()
    51.          {
    52. #if !UNITY_EDITOR
    53.              m_Image = GetComponent<Image>();
    54.              m_Center = m_Image.transform.position;
    55. #endif
    56.          }
    57.          void CreateVirtualAxes()
    58.          {
    59.              // set axes to use
    60.              m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
    61.              m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
    62.              // create new axes based on axes to use
    63.              if (m_UseX)
    64.              {
    65.                  m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
    66.                  CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
    67.              }
    68.              if (m_UseY)
    69.              {
    70.                  m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
    71.                  CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
    72.              }
    73.          }
    74.          void UpdateVirtualAxes(Vector3 value)
    75.          {
    76.              value = value.normalized;
    77.              if (m_UseX)
    78.              {
    79.                  m_HorizontalVirtualAxis.Update(value.x);
    80.              }
    81.              if (m_UseY)
    82.              {
    83.                  m_VerticalVirtualAxis.Update(value.y);
    84.              }
    85.          }
    86.          public void OnPointerDown(PointerEventData data)
    87.          {
    88.              m_Dragging = true;
    89.              m_Id = data.pointerId;
    90. #if !UNITY_EDITOR
    91.          if (controlStyle != ControlStyle.Absolute )
    92.              m_Center = data.position;
    93. #endif
    94.          }
    95.          void Update()
    96.          {
    97.              if (!m_Dragging)
    98.              {
    99.                  return;
    100.              }
    101.              if (Input.touchCount >= m_Id + 1 && m_Id != -1)
    102.              {
    103. #if !UNITY_EDITOR
    104.              if (controlStyle == ControlStyle.Swipe)
    105.              {
    106.                  m_Center = m_PreviousTouchPos;
    107.                  m_PreviousTouchPos = Input.touches[m_Id].position;
    108.              }
    109.              Vector2 pointerDelta = new Vector2(Input.touches[m_Id].position.x - m_Center.x , Input.touches[m_Id].position.y - m_Center.y).normalized;
    110.              pointerDelta.x *= Xsensitivity;
    111.              pointerDelta.y *= Ysensitivity;
    112. #else
    113.                  Vector2 pointerDelta;
    114.                  pointerDelta.x = Input.mousePosition.x - m_PreviousMouse.x;
    115.                  pointerDelta.y = Input.mousePosition.y - m_PreviousMouse.y;
    116.                  m_PreviousMouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);
    117. #endif
    118.                  UpdateVirtualAxes(new Vector3(pointerDelta.x, pointerDelta.y, 0));
    119.              }
    120.          }
    121.          public void OnPointerUp(PointerEventData data)
    122.          {
    123.              m_Dragging = false;
    124.              m_Id = -1;
    125.              UpdateVirtualAxes(Vector3.zero);
    126.          }
    127.          void OnDisable()
    128.          {
    129.              if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
    130.                  CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
    131.              if (CrossPlatformInputManager.AxisExists(verticalAxisName))
    132.                  CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
    133.          }
    134.      }
    135. }
    Player Control - This script is on the camera in the scene using UnityEngine; using System.Collections; using UnityStandardAssets.CrossPlatformInput;

    Code (CSharp):
    1. public class PlayerControl : MonoBehaviour {
    2.      private float speed;
    3.      private float horiz;
    4.      private float vert;
    5.      // Use this for initialization
    6.      void Start () {
    7.          speed = 10f;
    8.      }
    9.      // Update is called once per frame
    10.      void Update () {
    11.          horiz = CrossPlatformInputManager.GetAxis("Horizontal") * speed;
    12.          vert = CrossPlatformInputManager.GetAxis("Vertical") * speed;
    13.          horiz *= Time.deltaTime;
    14.          vert *= Time.deltaTime;
    15.          transform.Translate(horiz, vert, 0f);
    16.      }
    17. }
    Joystick script which may help with implementing the distance, not sure how though

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. namespace UnityStandardAssets.CrossPlatformInput
    5. {
    6.      public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    7.      {
    8.          public enum AxisOption
    9.          {
    10.              // Options for which axes to use
    11.              Both, // Use both
    12.              OnlyHorizontal, // Only horizontal
    13.              OnlyVertical // Only vertical
    14.          }
    15.          public int MovementRange = 100;
    16.          public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
    17.          public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
    18.          public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
    19.          Vector3 m_StartPos;
    20.          bool m_UseX; // Toggle for using the x axis
    21.          bool m_UseY; // Toggle for using the Y axis
    22.          CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
    23.          CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
    24.          void OnEnable()
    25.          {
    26.              CreateVirtualAxes();
    27.          }
    28.          void Start()
    29.          {
    30.              m_StartPos = transform.position;
    31.          }
    32.          void UpdateVirtualAxes(Vector3 value)
    33.          {
    34.              var delta = m_StartPos - value;
    35.              delta.y = -delta.y;
    36.              delta /= MovementRange;
    37.              if (m_UseX)
    38.              {
    39.                  m_HorizontalVirtualAxis.Update(-delta.x);
    40.              }
    41.              if (m_UseY)
    42.              {
    43.                  m_VerticalVirtualAxis.Update(delta.y);
    44.              }
    45.          }
    46.          void CreateVirtualAxes()
    47.          {
    48.              // set axes to use
    49.              m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
    50.              m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
    51.              // create new axes based on axes to use
    52.              if (m_UseX)
    53.              {
    54.                  m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
    55.                  CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
    56.              }
    57.              if (m_UseY)
    58.              {
    59.                  m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
    60.                  CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
    61.              }
    62.          }
    63.          public void OnDrag(PointerEventData data)
    64.          {
    65.              Vector3 newPos = Vector3.zero;
    66.              if (m_UseX)
    67.              {
    68.                  int delta = (int)(data.position.x - m_StartPos.x);
    69.                  delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
    70.                  newPos.x = delta;
    71.              }
    72.              if (m_UseY)
    73.              {
    74.                  int delta = (int)(data.position.y - m_StartPos.y);
    75.                  delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
    76.                  newPos.y = delta;
    77.              }
    78.              transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
    79.              UpdateVirtualAxes(transform.position);
    80.          }
    81.          public void OnPointerUp(PointerEventData data)
    82.          {
    83.              transform.position = m_StartPos;
    84.              UpdateVirtualAxes(m_StartPos);
    85.          }
    86.          public void OnPointerDown(PointerEventData data) { }
    87.          void OnDisable()
    88.          {
    89.              // remove the joysticks from the cross platform input
    90.              if (m_UseX)
    91.              {
    92.                  m_HorizontalVirtualAxis.Remove();
    93.              }
    94.              if (m_UseY)
    95.              {
    96.                  m_VerticalVirtualAxis.Remove();
    97.              }
    98.          }
    99.      }
    100. }
     
  2. TimTro

    TimTro

    Joined:
    Jun 1, 2015
    Posts:
    20
    I still haven't figured this out, can anyone point me in a direction to learn about how to do this?