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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovingSphere : MonoBehaviour
{
Rigidbody body;
Renderer renderer;
[SerializeField, Range(0f, 100f)]
float maxSpeed = 10f;
[SerializeField, Range(0f, 100f)]
float maxAcceleration = 10f;
[SerializeField, Range(0f, 100f)]
float maxAirAcceleration = 1f;
Vector3 velocity;
[SerializeField, Range(0f, 10f)]
float jumpHeight = 2f;
[SerializeField, Range(0, 5)]
int maxAirJumps;
bool desiredJump;
int jumpPhase;
// dont snap to surface if going too fast
[SerializeField, Range(0f, 100f)]
float maxSnapSpeed = 100f;
[SerializeField, Min(0f)]
float raycastProbeDistance = 1f;
[SerializeField]
LayerMask probeMask = -1;
LayerMask stairsMask = -1;
int stepsSinceLastGrounded;
int stepsSinceLastJump;
Vector3 contactNormal;
Vector3 steepNormal;
int groundContactCount;
int steepContactCount;
bool OnGround => groundContactCount > 0;
bool OnSteep => steepContactCount > 0;
// 1.0 means walls
// 0.0 means floors
[SerializeField, Range(0f, 1f)]
float maxSlopeAngle = 0.44f;
float minGroundDotProduct;
[SerializeField, Range(0f, 1f)]
float maxStairAngle = 0.6f;
float minStairsDotProduct;
Vector3 inputVelocity;
void Awake()
{
body = GetComponent<Rigidbody>();
renderer = GetComponent<Renderer>();
}
void Update()
{
Vector2 playerInput = new Vector2(
Input.GetAxis("Horizontal"),
Input.GetAxis("Vertical")
);
playerInput = Vector2.ClampMagnitude(playerInput, 1f);
desiredJump |= Input.GetButtonDown("Jump");
inputVelocity = new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
Color purple = Color.red + Color.blue;
//renderer.material.SetColor(
// "_BaseColor", purple - (purple * (groundContactCount * 0.33f))
// );
renderer.material.SetColor(
"_BaseColor", OnGround ? purple * 0.9f : purple * 0.1f
);
}
void FixedUpdate()
{
UpdateState();
AdjustVelocity();
if(desiredJump)
{
desiredJump = false;
Jump();
}
body.velocity = velocity;
ClearState();
}
void ClearState() {
groundContactCount = steepContactCount = 0;
contactNormal = steepNormal = Vector3.zero;
}
void UpdateState()
{
// if changed in editor, update these
minGroundDotProduct = Mathf.Cos(maxSlopeAngle * 90 * Mathf.Deg2Rad);
minStairsDotProduct = Mathf.Cos(maxStairAngle * 90 * Mathf.Deg2Rad);
stepsSinceLastGrounded += 1;
stepsSinceLastJump += 1;
velocity = body.velocity;
if(OnGround || SnapToGround() || CheckSteepContacts())
{
stepsSinceLastGrounded = 0;
if(stepsSinceLastJump > 1)
jumpPhase = 0;
if(groundContactCount > 1)
contactNormal.Normalize();
}
else
contactNormal = Vector3.up;
}
void Jump()
{
Vector3 jumpDirection;
if(OnGround)
jumpDirection = contactNormal;
else if(OnSteep)
{
jumpDirection = steepNormal;
jumpPhase = 0;
}
else if((maxAirJumps > 0) && (jumpPhase <= maxAirJumps))
{
if(jumpPhase == 0)
jumpPhase = 1;
jumpDirection = contactNormal;
}
else
return;
stepsSinceLastJump = 0;
jumpPhase += 1;
float jumpSpeed = Mathf.Sqrt(-2f * Physics.gravity.y * jumpHeight);
jumpDirection = (jumpDirection + Vector3.up).normalized;
float alignedSpeed = Vector3.Dot(velocity, jumpDirection);
if(alignedSpeed > 0f)
jumpSpeed = Mathf.Max(jumpSpeed - alignedSpeed, 0f);
velocity += jumpDirection * jumpSpeed;
}
bool SnapToGround()
{
if(stepsSinceLastGrounded > 1 || stepsSinceLastJump <= 2)
return false;
float speed = velocity.magnitude;
if(speed > maxSnapSpeed)
return false;
if(!Physics.Raycast(
body.position,
Vector3.down,
out RaycastHit hit,
raycastProbeDistance,
probeMask
))
return false;
if(hit.normal.y < GetMinDot(hit.collider.gameObject.layer))
return false;
groundContactCount = 1;
contactNormal = hit.normal;
float dot = Vector3.Dot(velocity, hit.normal);
if(dot > 0f)
velocity = (velocity - hit.normal * dot).normalized * speed;
return true;
}
void OnCollisionStay(Collision collision) {
EvaluateCollision(collision);
}
void OnCollisionEnter(Collision collision) {
EvaluateCollision(collision);
}
void EvaluateCollision(Collision collision) {
float minDot = GetMinDot(collision.gameObject.layer);
for(int i = 0; i < collision.contactCount; i++)
{
Vector3 normal = collision.GetContact(i).normal;
if(normal.y >= minDot)
{
groundContactCount += 1;
contactNormal += normal;
}
else if(normal.y > -0.01f)
{
steepContactCount += 1;
steepNormal += normal;
}
}
}
void AdjustVelocity()
{
Vector3 xAxis = ProjectOnContactPlane(Vector3.right).normalized;
Vector3 zAxis = ProjectOnContactPlane(Vector3.forward).normalized;
Vector3 currentVel = new Vector3(
Vector3.Dot(velocity, xAxis),
0f,
Vector3.Dot(velocity, zAxis)
);
float maxSpeedChange = Time.deltaTime;
if(OnGround)
maxSpeedChange *= maxAcceleration;
else
maxSpeedChange *= maxAirAcceleration;
Vector3 newVel = new Vector3(
Mathf.MoveTowards(currentVel.x, inputVelocity.x, maxSpeedChange),
0f,
Mathf.MoveTowards(currentVel.z, inputVelocity.z, maxSpeedChange)
);
velocity += xAxis * (newVel.x - currentVel.x) + zAxis * (newVel.z - currentVel.z);
}
Vector3 ProjectOnContactPlane(Vector3 vector)
{
return vector - contactNormal * Vector3.Dot(vector, contactNormal);
}
float GetMinDot(int layer)
{
if ((stairsMask & (1 << layer)) > 0)
return minStairsDotProduct;
else
return minGroundDotProduct;
}
bool CheckSteepContacts()
{
if(steepContactCount > 1)
{
steepNormal.Normalize();
if(steepNormal.y >= minGroundDotProduct)
{
groundContactCount = 1;
contactNormal = steepNormal;
return true;
}
}
return false;
}
}
|