Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class Joystick : MonoBehaviour, IDragHandler
- {
- Vector2 startPos;
- public Canvas canvas;
- public float Radius;
- public Vector2 Axis;
- private void Start()
- {
- startPos = this.transform.position;
- }
- public Vector2 axis
- {
- get
- {
- return Axis;
- }
- }
- public float Horizontal
- {
- get
- {
- return Axis.x;
- }
- }
- public float Vertical
- {
- get
- {
- return Axis.y;
- }
- }
- public bool isMoving
- {
- get
- {
- return Axis != Vector2.zero;
- }
- }
- public void OnDrag (PointerEventData data)
- {
- Vector2 pos;
- RectTransformUtility.ScreenPointToLocalPointInRectangle (canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out pos);
- Vector2 newpos = this.transform.position = canvas.transform.TransformPoint (pos) - (Vector3)startPos;
- newpos.x = Mathf.Clamp(newpos.x, -Radius, Radius);
- newpos.y = Mathf.Clamp(newpos.y, -Radius, Radius);
- Axis = pos / Radius;
- this.transform.localPosition = newpos;
- }
- public void OnMouseUp()
- {
- this.transform.localPosition = startPos;
- Axis = Vector2.zero;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement