GravityBall.cs
5.5 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravityBall : MonoBehaviour
{
public static GravityBall Instance
{
get
{
if (!m_Instance)
{
m_Instance = FindObjectOfType<GravityBall>();
if (!m_Instance)
{
Debug.LogError("You didn't Crate GravityBall!");
}
}
return m_Instance;
}
}
private static GravityBall m_Instance;
public LayerMask m_ContactTarget;
//private SphereCollider m_Collider;
private const float m_ColliderThickness = 0.001f;
public bool m_IsTouched { get; private set; }
private Vector3 m_StartPos;
private Material m_RenderMaterial;
private Rigidbody m_Rigidbody;
public Color m_IdleColor = Color.black;
public Color m_PressedColor = Color.red;
public string m_XDistanceInputName = "Horizontal";
public string m_ZDistanceINputName = "Vertical";
public string m_XSwipeInputName = "Scroll";
public string m_YSwipeInputName = "Spin";
public string m_ZSwipeInputName = "Stroke";
public float m_ControlRange = 0.08f;
[Range(0.01f, 0.2f)]
public float m_RotationDeadzone = 0.01f;
[Range(0.1f, 5f)]
public float m_RotationSenstive = 1f;
private void Awake()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
private void Start()
{
m_StartPos = m_Rigidbody.position;
m_RenderMaterial = GetComponentInChildren<Renderer>().material;
m_RenderMaterial.color = m_IdleColor;
}
private void Update()
{
if (GetButton())
{
m_RenderMaterial.color = m_PressedColor;
}
else
{
m_RenderMaterial.color = m_IdleColor;
}
}
private void FixedUpdate()
{
// 범위를 벗어난 경우 범위 내부로 강제로 되돌리기
if (m_IsTouched)
{
if (Vector3.Distance(m_StartPos, m_Rigidbody.position) > m_ControlRange)
{
m_Rigidbody.MovePosition(m_StartPos + (m_Rigidbody.position - m_StartPos) * m_ControlRange);
}
}
}
public bool GetButton()
{
if (!m_IsTouched)
{
return false;
}
if ((m_StartPos.y - m_Rigidbody.position.y) >= m_ControlRange * 0.5f)
{
return true;
}
return false;
}
public float GetInputAxis(string inputName)
{
if (!m_IsTouched)
{
return 0;
}
// Swipe를 하고 있다면, 값을 억누르기
float inputFactorBySwipe = 1.0f;
if (m_Rigidbody.angularVelocity.sqrMagnitude >= 1.0f)
{
inputFactorBySwipe = 1.0f / m_Rigidbody.angularVelocity.sqrMagnitude;
}
Vector3 deltaVec = m_Rigidbody.position - m_StartPos;
Vector3 relativeVec = deltaVec / m_ControlRange;
if (inputName == m_XDistanceInputName)
{
return Mathf.Clamp(relativeVec.x, -1.0f, 1.0f) * inputFactorBySwipe;
}
else if (inputName == m_ZDistanceINputName)
{
return Mathf.Clamp(relativeVec.z, -1.0f, 1.0f) * inputFactorBySwipe;
}
Debug.LogError("Can't Find Input Name: " + inputName);
return 0;
}
public float GetInputSwipe(string inputName)
{
if (!m_IsTouched)
{
return 0;
}
Vector3 angularVelocity = m_Rigidbody.angularVelocity;
angularVelocity *= m_RotationSenstive;
float absX = Mathf.Abs(angularVelocity.x);
float absY = Mathf.Abs(angularVelocity.y);
float absZ = Mathf.Abs(angularVelocity.z);
angularVelocity.x = absX <= m_RotationDeadzone ? 0 : angularVelocity.x;
angularVelocity.y = absY <= m_RotationDeadzone ? 0 : angularVelocity.y;
angularVelocity.z = absZ <= m_RotationDeadzone ? 0 : angularVelocity.z;
if (absX >= absY && absX >= absZ)
{
angularVelocity.y = 0;
angularVelocity.z = 0;
}
else if(absY >= absX && absY >= absZ)
{
angularVelocity.x = 0;
angularVelocity.z = 0;
}
else if (absZ >= absX && absZ >= absY)
{
angularVelocity.x = 0;
angularVelocity.y = 0;
}
float factorByButton = GetButton() ? 0.1f : 1.0f;
if (inputName == m_XSwipeInputName)
{
return Mathf.Clamp(angularVelocity.x, -1f, 1f) * factorByButton;
}
else if (inputName == m_YSwipeInputName)
{
return Mathf.Clamp(angularVelocity.y, -1f, 1f) * factorByButton;
}
else if (inputName == m_ZSwipeInputName)
{
return Mathf.Clamp(angularVelocity.z, -1f, 1f) * factorByButton;
}
Debug.LogError("Can't Find Input Name: " + inputName);
return 0;
}
private void OnCollisionExit(Collision collision)
{
m_IsTouched = false;
m_Rigidbody.MoveRotation(Quaternion.identity);
}
private void OnCollisionStay(Collision collision)
{
m_IsTouched = true;
}
void OnDrawGizmos()
{
// Draw a wire sphere at each of the points
Gizmos.color = Color.blue;
if (m_Rigidbody == null)
{
Gizmos.DrawWireSphere(transform.position, m_ControlRange);
}
else
{
Gizmos.DrawWireSphere(m_StartPos, m_ControlRange);
}
}
}