Joystick default value

Hello all.

Currently using the Unity 5 Joystick. I need to make the Joystick return point to be the end of an axis.

So instead of :

        X  
  1=====0=====-1

It will be:

  X  
  1=====0=====-1

I have tried adding variables to Joystick.cs like so:

public float DefaultRange = 1.0f;
Vector3 m_DefaultPos;

And editing functions like this:

        void Start()
        {
            m_StartPos = transform.position;

			m_DefaultPos = m_StartPos;

			if (m_UseX)
			{
				m_DefaultPos.x += MovementRange*DefaultRange;
			}

			if (m_UseY)
			{
				m_DefaultPos.y += MovementRange*DefaultRange;
			}
        }

		public void OnPointerUp(PointerEventData data)
		{
			transform.position = m_DefaultPos;
			m_CurrentPos = m_DefaultPos;
			UpdateVirtualAxes(m_DefaultPos);
		}

The Joystick does return to a new default position but it snaps back to 0 when I tap down and start dragging. I feel like I should edit the OnDrag function, but it just confuses me. I need some help fixing this. I will attach a full copy of the modified Joystick.cs below.

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 float DefaultRange = 0.0f;
		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;
		Vector3 m_CurrentPos;
		Vector3 m_DefaultPos;
		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;

			m_DefaultPos = m_StartPos;

			if (m_UseX)
			{
				m_DefaultPos.x += MovementRange*DefaultRange;
			}

			if (m_UseY)
			{
				m_DefaultPos.y += MovementRange*DefaultRange;
			}
        }

		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)
			{
				if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
				{
					CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
				}
				m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
				CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
			}
			if (m_UseY)
			{
				if (CrossPlatformInputManager.AxisExists(verticalAxisName))
				{
					CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
				}
				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_CurrentPos.x);
				delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
				newPos.x = delta;
			}

			if (m_UseY)
			{
				int delta = (int)(data.position.y - m_CurrentPos.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);
//			transform.position = new Vector3(m_CurrentPos.x + newPos.x, m_CurrentPos.y + newPos.y, m_CurrentPos.z + newPos.z);
			UpdateVirtualAxes(transform.position);
		}


		public void OnPointerUp(PointerEventData data)
		{
			transform.position = m_DefaultPos;
			m_CurrentPos = m_DefaultPos;
			UpdateVirtualAxes(m_DefaultPos);
		}


		public void OnPointerDown(PointerEventData data) { 
		
			m_CurrentPos = data.position;
		
		}

		void OnDisable()
		{
			// remove the joysticks from the cross platform input
			if (m_UseX)
			{
				m_HorizontalVirtualAxis.Remove();
			}
			if (m_UseY)
			{
				m_VerticalVirtualAxis.Remove();
			}
		}
	}
}

Any insight on this would be appreicated.

Finally fixed it.

Final modified OnDrag. Got it working with my other edits:

public void OnDrag(PointerEventData data)
		{
			Vector3 newPos = Vector3.zero;

			if (m_UseX)
			{
				int delta = (int)(data.position.x - m_CurrentPos.x);
				delta = Mathf.Clamp(delta, (int)(-MovementRange+(-MovementRange*DefaultRange)), (int)(MovementRange+(-MovementRange*DefaultRange)));
				newPos.x = delta;
			}

			if (m_UseY)
			{
				int delta = (int)(data.position.y - m_CurrentPos.y);
				delta = Mathf.Clamp(delta, (int)(-MovementRange+(-MovementRange*DefaultRange)), (int)(MovementRange+(-MovementRange*DefaultRange)));
				newPos.y = delta;
			}

			transform.position = new Vector3(m_DefaultPos.x + newPos.x,m_DefaultPos.y + newPos.y, m_DefaultPos.z + newPos.z);
			UpdateVirtualAxes(transform.position);
		}

OLD POST:

Gorrammit, a few minutes after I posted this question, I stumbled on the answer.

Here’s the modified OnDrag

		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);
		}

It works well so far. But I’d like others’ insight into this to maybe make it a bit more elegant.

Edit: I just found that this conflicts with another edit I made that stops the joystick snapping under your finger when you press it. Derp.