Clamping joystick

Hi, I’m using crossplatform joystick and I want to clamp it to it’s max range so it will only move in a circular with center pivot.

alt text

Here is the code for clamping from standard assets

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

			if (m_UseX)
			{
				int delta = (int)(data.position.x - m_StartPos.x);
				newPos.x = delta;
			}

			if (m_UseY)
			{
				int delta = (int)(data.position.y - m_StartPos.y);
				newPos.y = delta;
			}
			transform.position = Vector3.ClampMagnitude( new Vector3(newPos.x, newPos.y, newPos.z), MovementRange) + m_StartPos;
			UpdateVirtualAxes(transform.position);
		}

You want the filled circle to stay on the bounds of the circle ?

If so, change the following line

transform.position = Vector3.ClampMagnitude( new Vector3(newPos.x, newPos.y, newPos.z), MovementRange) + m_StartPos;

by

transform.position = (new Vector3(newPos.x, newPos.y, newPos.z)).normalized * MovementRange + m_StartPos;