Skip to content

Instantly share code, notes, and snippets.

@cwmagnus
Created October 4, 2018 18:22
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
A simple virtual joystick script to use with Unity for mobile games.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
/// <summary>
/// Virtual joystick for mobile joystick control
/// </summary>
public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image backPanel;
private Image knob;
public Vector3 InputDirection { get; set; }
/// <summary>
/// Get the joystick UI.
/// </summary>
private void Start()
{
backPanel = GetComponent<Image>();
knob = transform.GetChild(0).GetComponent<Image>();
}
/// <summary>
/// Drag the knob of the joystick.
/// </summary>
/// <param name="pointerEventData">Data from the touch.</param>
public virtual void OnDrag(PointerEventData pointerEventData)
{
Vector2 position = Vector2.zero;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle
(backPanel.rectTransform,
pointerEventData.position,
pointerEventData.pressEventCamera,
out position))
{
// Get the touch position
position.x = (position.x / backPanel.rectTransform.sizeDelta.x);
position.y = (position.y / backPanel.rectTransform.sizeDelta.y);
// Calculate the move position
float x = (backPanel.rectTransform.pivot.x == 1) ?
position.x * 2 + 1 : position.x * 2 - 1;
float y = (backPanel.rectTransform.pivot.y == 1) ?
position.y * 2 + 1 : position.y * 2 - 1;
// Get the input position
InputDirection = new Vector3(x, 0, y);
InputDirection = (InputDirection.magnitude > 1) ?
InputDirection.normalized : InputDirection;
// Move the knob
knob.rectTransform.anchoredPosition =
new Vector3(InputDirection.x * (backPanel.rectTransform.sizeDelta.x / 3),
InputDirection.z * (backPanel.rectTransform.sizeDelta.y / 3));
}
}
/// <summary>
/// Click on the knob.
/// </summary>
/// <param name="pointerEventData">Data from the touch.</param>
public virtual void OnPointerDown(PointerEventData pointerEventData)
{
OnDrag(pointerEventData);
}
/// <summary>
/// Click off the knob.
/// </summary>
/// <param name="pointerEventData">Data from the touch.</param>
public virtual void OnPointerUp(PointerEventData pointerEventData)
{
InputDirection = Vector3.zero;
knob.rectTransform.anchoredPosition = Vector3.zero;
}
}
@Stevanrose1992
Copy link

how do i set it up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment