-
Notifications
You must be signed in to change notification settings - Fork 12
/
JoyStick.cs
69 lines (60 loc) · 1.73 KB
/
JoyStick.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JoyStick : Singleton<JoyStick>
{
[SerializeField]
Transform Root,Pad;
[SerializeField]
float MaxR = 1;
Vector2 Origin = new Vector2(0,0);
[SerializeField]
bool _IsOriginSet = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
ListenJoyStick();
ReleaseTouch();
}
void ListenJoyStick(){
if(!Input.GetMouseButton(0))
return;
if(!_IsOriginSet){
_IsOriginSet = true;
Origin = Input.mousePosition;
Root.position = Origin;
Pad.transform.position = Origin;
Root.gameObject.SetActive(true);
return;
}
Vector2 currentTouch = (Vector2)Input.mousePosition-Origin;
if(currentTouch == Vector2.zero)
return;
if(currentTouch.magnitude <= MaxR){
Pad.transform.position = Input.mousePosition;
return;
}
float currentAngle = Mathf.Atan2(currentTouch.y,currentTouch.x);
float X = Origin.x + MaxR * Mathf.Cos(currentAngle);
float Y = Origin.y + MaxR * Mathf.Sin(currentAngle);
Pad.transform.position = new Vector2(X,Y);
}
void ReleaseTouch(){
if(!_IsOriginSet)
return;
if(Input.GetMouseButtonUp(0)){
_IsOriginSet = false;
Root.gameObject.SetActive(false);
}
}
public Vector2 GetJoyVector(){
if(!_IsOriginSet)
return Vector2.zero;
Vector2 tmp = (Vector2)Input.mousePosition-Origin;
return tmp.normalized;
}
}