-
Notifications
You must be signed in to change notification settings - Fork 39
/
SlaveController.cs
163 lines (134 loc) · 4.2 KB
/
SlaveController.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
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
using UnityEngine;
using System;
public enum RagdollState
{
FOLLOWING_ANIMATION,
LOOSING_STRENGTH,
GAINING_STRENGTH,
DEAD
}
public class SlaveController : MonoBehaviour
{
/// <summary>
/// Controlls Ragdoll.
/// </summary>
// RAGDOLL STATE
public RagdollState state;
// PARAMETERS
[SerializeField]
[Tooltip("Determines how fast ragdoll loses strength when in contact.")]
private float looseStrengthLerp = 1.0f;
[SerializeField]
[Tooltip("Determines how fast ragdoll gains strength after being freed from contact.")]
private float gainStrengthLerp = 0.05f;
[SerializeField]
[Tooltip("Minimum force strength during collision.")]
private float minContactForce = 0.1f;
[SerializeField]
[Tooltip("Minimum torque strength during collision.")]
private float minContactTorque = 0.1f;
[SerializeField]
[Tooltip("Time of being dead expressed in seconds passed in sumulation.")]
private float deadTime = 4.0f;
// USEFUL VARIABLES
private AnimationFollowing animFollow;
private float maxTorqueCoefficient;
private float maxForceCoefficient;
private float currentDeadStep;
private float currentStrength;
[NonSerialized] public int currentNumberOfCollisions;
// Start is called before the first frame update.
void Start()
{
HumanoidSetUp setUp = this.GetComponentInParent<HumanoidSetUp>();
animFollow = setUp.animFollow;
maxForceCoefficient = animFollow.forceCoefficient;
maxTorqueCoefficient = animFollow.torqueCoefficient;
currentNumberOfCollisions = 0;
currentDeadStep = deadTime;
currentStrength = 1.0f;
}
// Unity method for physics update.
void FixedUpdate()
{
// Apply animation following
animFollow.FollowAnimation();
// print(currentNumberOfCollisions);
// print(animFollow.forceCoefficient);
state = GetRagdollState();
switch (state)
{
case RagdollState.DEAD:
currentDeadStep += Time.fixedDeltaTime;
if (currentDeadStep >= deadTime)
ComeAlive();
break;
case RagdollState.LOOSING_STRENGTH:
LooseStrength();
break;
case RagdollState.GAINING_STRENGTH:
GainStrength();
break;
case RagdollState.FOLLOWING_ANIMATION:
break;
default:
break;
}
}
private RagdollState GetRagdollState()
{
if (!animFollow.isAlive)
{
return RagdollState.DEAD;
}
else if (currentNumberOfCollisions != 0)
{
return RagdollState.LOOSING_STRENGTH;
}
else if (currentStrength < 1)
{
return RagdollState.GAINING_STRENGTH;
}
else
{
return RagdollState.FOLLOWING_ANIMATION;
}
}
private void LooseStrength()
{
currentStrength -= looseStrengthLerp * Time.fixedDeltaTime;
currentStrength = Mathf.Clamp(currentStrength, 0, 1);
InterpolateStrength(currentStrength);
}
private void GainStrength()
{
currentStrength += gainStrengthLerp * Time.fixedDeltaTime;
currentStrength = Mathf.Clamp(currentStrength, 0, 1);
InterpolateStrength(currentStrength);
}
private void InterpolateStrength(float ratio)
{
animFollow.forceCoefficient = Mathf.Lerp(minContactForce, maxForceCoefficient, ratio);
animFollow.torqueCoefficient = Mathf.Lerp(minContactTorque, maxTorqueCoefficient, ratio);
}
[ContextMenu("Die")]
private void Die()
{
animFollow.isAlive = false;
currentDeadStep = 0;
ResetForces();
}
[ContextMenu("Come alive")]
private void ComeAlive()
{
animFollow.isAlive = true;
}
// Sets animation following forces to zero. After calling this method, ragdoll will gradually regain strength.
[ContextMenu("Reset forces")]
private void ResetForces()
{
animFollow.forceCoefficient = 0f;
animFollow.torqueCoefficient = 0f;
currentStrength = 0;
}
}