'How can I make the Unity On-screen Stick move only on the X axis? (Unity 2D)
I'm trying to make a movement system for mobile phones in Unity 2D using Unity's On-screen Stick from the new Input System, but I'm facing a problem doing so.
This is the default code from it:
public void OnDrag(PointerEventData eventData)
{
if (eventData == null)
throw new System.ArgumentNullException(nameof(eventData));
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponentInParent<RectTransform>(), eventData.position, eventData.pressEventCamera, out var position);
var delta = position - m_PointerDownPos;
delta = Vector2.ClampMagnitude(delta, movementRange);
((RectTransform)transform).anchoredPosition = m_StartPos + (Vector3)delta;
var newPos = new Vector2(delta.x / movementRange, delta.y / movementRange);
SendValueToControl(newPos);
}
instead of delta = Vector2.ClampMagnitude(delta, movementRange);, I replaced it with
delta.x = Mathf.Clamp(delta.x, -movementRange, movementRange);
delta.y = Mathf.Clamp(0, 0, 0);
but for some reason, if I drag the joystick to the end, the sprite won't move and only if I position it in a specific way it'll move.
Hopefully, someone can give me some suggestions.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
