Unity-SpaceShooter/Assets/Scripts/SimpleTouchPad.cs

50 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class SimpleTouchPad : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
public float smoothing;
private Vector2 origin, direction, smoothDirection;
private bool touched;
private int pointerID;
void Awake()
{
direction = Vector2.zero;
touched = false;
}
public void OnPointerDown(PointerEventData data)
{
if (!touched)
{
touched = true;
pointerID = data.pointerId;
origin = data.position;
}
}
public void OnDrag(PointerEventData data)
{
if (data.pointerId == pointerID)
{
Vector2 currentPosition = data.position;
Vector2 directionRaw = currentPosition - origin;
direction = directionRaw.normalized;
}
}
public void OnPointerUp(PointerEventData data)
{
if (data.pointerId == pointerID)
{
direction = Vector2.zero;
touched = false;
}
}
public Vector2 GetDirection()
{
smoothDirection = Vector2.MoveTowards(smoothDirection, direction, smoothing);
return smoothDirection;
}
}