- Home /
Can't get touchpad to Increase sensitivity as distance between start position increases
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: Touchpad using System; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI;
namespace UnityStandardAssets.CrossPlatformInput
{
[RequireComponent(typeof(Image))]
public class TouchPad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
// Options for which axes to use
public enum AxisOption
{
Both, // Use both
OnlyHorizontal, // Only horizontal
OnlyVertical // Only vertical
}
public enum ControlStyle
{
Absolute, // operates from teh center of the image
Relative, // operates from the center of the initial touch
Swipe, // swipe to touch touch no maintained center
}
public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
public ControlStyle controlStyle = ControlStyle.Absolute; // control style to use
public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
public float Xsensitivity = 1f;
public float Ysensitivity = 1f;
Vector3 m_StartPos;
Vector2 m_PreviousDelta;
Vector3 m_JoytickOutput;
bool m_UseX; // Toggle for using the x axis
bool m_UseY; // Toggle for using the Y axis
CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
bool m_Dragging;
int m_Id = -1;
Vector2 m_PreviousTouchPos; // swipe style control touch
#if !UNITY_EDITOR
private Vector3 m_Center;
private Image m_Image;
#else
Vector3 m_PreviousMouse;
#endif
void OnEnable()
{
CreateVirtualAxes();
}
void Start()
{
#if !UNITY_EDITOR
m_Image = GetComponent<Image>();
m_Center = m_Image.transform.position;
#endif
}
void CreateVirtualAxes()
{
// set axes to use
m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
// create new axes based on axes to use
if (m_UseX)
{
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
}
if (m_UseY)
{
m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
}
}
void UpdateVirtualAxes(Vector3 value)
{
value = value.normalized;
if (m_UseX)
{
m_HorizontalVirtualAxis.Update(value.x);
}
if (m_UseY)
{
m_VerticalVirtualAxis.Update(value.y);
}
}
public void OnPointerDown(PointerEventData data)
{
m_Dragging = true;
m_Id = data.pointerId;
#if !UNITY_EDITOR
if (controlStyle != ControlStyle.Absolute )
m_Center = data.position;
#endif
}
void Update()
{
if (!m_Dragging)
{
return;
}
if (Input.touchCount >= m_Id + 1 && m_Id != -1)
{
#if !UNITY_EDITOR
if (controlStyle == ControlStyle.Swipe)
{
m_Center = m_PreviousTouchPos;
m_PreviousTouchPos = Input.touches[m_Id].position;
}
Vector2 pointerDelta = new Vector2(Input.touches[m_Id].position.x - m_Center.x , Input.touches[m_Id].position.y - m_Center.y).normalized;
pointerDelta.x *= Xsensitivity;
pointerDelta.y *= Ysensitivity;
#else
Vector2 pointerDelta;
pointerDelta.x = Input.mousePosition.x - m_PreviousMouse.x;
pointerDelta.y = Input.mousePosition.y - m_PreviousMouse.y;
m_PreviousMouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);
#endif
UpdateVirtualAxes(new Vector3(pointerDelta.x, pointerDelta.y, 0));
}
}
public void OnPointerUp(PointerEventData data)
{
m_Dragging = false;
m_Id = -1;
UpdateVirtualAxes(Vector3.zero);
}
void OnDisable()
{
if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
if (CrossPlatformInputManager.AxisExists(verticalAxisName))
CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
}
}
}
Player Control - This script is on the camera in the scene using UnityEngine; using System.Collections; using UnityStandardAssets.CrossPlatformInput;
public class PlayerControl : MonoBehaviour {
private float speed;
private float horiz;
private float vert;
// Use this for initialization
void Start () {
speed = 10f;
}
// Update is called once per frame
void Update () {
horiz = CrossPlatformInputManager.GetAxis("Horizontal") * speed;
vert = CrossPlatformInputManager.GetAxis("Vertical") * speed;
horiz *= Time.deltaTime;
vert *= Time.deltaTime;
transform.Translate(horiz, vert, 0f);
}
}
Joystick script which may help with implementing the distance
using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace UnityStandardAssets.CrossPlatformInput
{
public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
public enum AxisOption
{
// Options for which axes to use
Both, // Use both
OnlyHorizontal, // Only horizontal
OnlyVertical // Only vertical
}
public int MovementRange = 100;
public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
Vector3 m_StartPos;
bool m_UseX; // Toggle for using the x axis
bool m_UseY; // Toggle for using the Y axis
CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
void OnEnable()
{
CreateVirtualAxes();
}
void Start()
{
m_StartPos = transform.position;
}
void UpdateVirtualAxes(Vector3 value)
{
var delta = m_StartPos - value;
delta.y = -delta.y;
delta /= MovementRange;
if (m_UseX)
{
m_HorizontalVirtualAxis.Update(-delta.x);
}
if (m_UseY)
{
m_VerticalVirtualAxis.Update(delta.y);
}
}
void CreateVirtualAxes()
{
// set axes to use
m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
// create new axes based on axes to use
if (m_UseX)
{
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
}
if (m_UseY)
{
m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
}
}
public void OnDrag(PointerEventData data)
{
Vector3 newPos = Vector3.zero;
if (m_UseX)
{
int delta = (int)(data.position.x - m_StartPos.x);
delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
newPos.x = delta;
}
if (m_UseY)
{
int delta = (int)(data.position.y - m_StartPos.y);
delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
newPos.y = delta;
}
transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
UpdateVirtualAxes(transform.position);
}
public void OnPointerUp(PointerEventData data)
{
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
public void OnPointerDown(PointerEventData data) { }
void OnDisable()
{
// remove the joysticks from the cross platform input
if (m_UseX)
{
m_HorizontalVirtualAxis.Remove();
}
if (m_UseY)
{
m_VerticalVirtualAxis.Remove();
}
}
}
}
Answer by TimTro · Aug 28, 2015 at 11:14 AM
I still haven't figured this out, can anyone point me in a direction to learn about how to do this?
Your answer
Follow this Question
Related Questions
Controller Joystick Sensitivity 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[Solved] Boolean is true and flase every frame 3 Answers
How to increment value of axis when something happens 2 Answers