-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPhysics.cs
More file actions
72 lines (62 loc) · 2.09 KB
/
MyPhysics.cs
File metadata and controls
72 lines (62 loc) · 2.09 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
using UnityEngine;
public static class MyPhysics
{
public readonly static float decellaration = 10f, gravity = -9.81f, gravityMultiplier = 1f, maxGravity = -70f;
public static void ApplyDragOnVelocityVector(Entity entity)
{
// basically just lowers the magnitude of the velocity vector on xz coordinates, and apply gravity on y coordinate
// should be called every frame when you want to lower the magnitude
// use arguments to handle below cases
// but NOT IF this is done by something else, ex. dynamic rigidbody
// another case when you shouldn't is when you don't want to negate the movement of the entity
// xz coordinates
float currentSpeed = GetCurrentSpeed(entity);
Vector3 velocityVector = entity.velocityVector;
if (currentSpeed <= 1.5f)
{
currentSpeed = 0f;
}
else
{
// if there is some force on xz coordinates
currentSpeed -= decellaration * Time.deltaTime;
}
velocityVector.x = velocityVector.normalized.x * currentSpeed;
velocityVector.z = velocityVector.normalized.z * currentSpeed;
entity.velocityVector = velocityVector;
}
public static void ApplyGravity(Entity entity)
{
// y coordinate
float y = entity.velocityVector.y;
if (y < 0f && entity.isGrounded)
{
y = -1f;
}
else if (maxGravity < y)
{
y += gravity * gravityMultiplier * Time.deltaTime;
}
else
{
y = maxGravity;
}
entity.velocityVector.y = y;
}
public static float GetCurrentSpeed(Entity entity)
{
Vector3 velocityVector = entity.velocityVector;
if(velocityVector.x != 0)
{
return Mathf.Abs(velocityVector.x / velocityVector.normalized.x);
}
else if(velocityVector.z != 0)
{
return Mathf.Abs(velocityVector.z / velocityVector.normalized.z);
}
else
{
return 0f;
}
}
}