blob: 9eb0322cdbbeb72b5414b790844aae4bb7533d7d (
plain)
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
|
using Rigging.Data;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace Rigging.Animations
{
// 物理控制的动作
public class AnimationObject : MonoBehaviour
{
public AnimationParam<PhysicsAnimation> animations;
private Rigidbody rig;
private StepHandler stepHandler;
private Transform hip;
public float smoothing;
public float multiplier = 1f;
private bool isCurrentlyLeft;
public bool isLeft;
public UnityEvent switchToForwardEvent;
public UnityEvent switchToBackwardsEvent;
public bool dontAnimateIfHoldingSomething;
private float muscleMultiplier = 1f;
private void Start()
{
rig = GetComponent<Rigidbody>();
stepHandler = base.transform.root.GetComponentInChildren<StepHandler>();
Rigging.BodyPart.Hip componentInChildren = base.transform.root.GetComponentInChildren<Rigging.BodyPart.Hip>();
if ((bool)componentInChildren)
{
hip = componentInChildren.transform;
}
animations.SetPlayer(transform.root.GetComponent<Player>());
}
private void FixedUpdate()
{
SetMultiplier();
AddTorque();
AddForce();
}
private void AddForce()
{
Vector3 vector = Vector3.zero;
if (isCurrentlyLeft == isLeft)
{
Vector3 forwardForce = animations.current.forwardForce;
if (animations.current.forceSpace == PhysicsAnimation.ForceSpace.World)
{
vector = forwardForce;
}
if (animations.current.forceSpace == PhysicsAnimation.ForceSpace.Hip)
{
vector = hip.TransformDirection(forwardForce);
}
}
else
{
Vector3 backwardsForce = animations.current.backwardsForce;
if (animations.current.forceSpace == PhysicsAnimation.ForceSpace.World)
{
vector = backwardsForce;
}
if (animations.current.forceSpace == PhysicsAnimation.ForceSpace.Hip)
{
vector = hip.TransformDirection(backwardsForce);
}
}
rig.AddForce(vector * muscleMultiplier * multiplier, ForceMode.Acceleration);
}
private void AddTorque()
{
Vector3 vector = ((isCurrentlyLeft != isLeft) ? hip.TransformDirection(animations.current.backwardsTorque) : hip.TransformDirection(animations.current.forwardTorque));
Vector3 toque = vector * muscleMultiplier * multiplier;
//Debug.DrawLine(rig.transform.position, rig.transform.position + toque.normalized * 0.3f);
rig.AddTorque(toque, ForceMode.Acceleration);
}
private void SetMultiplier()
{
if (smoothing != 0f)
{
float b = 1f;
if (isCurrentlyLeft != stepHandler.isLeft)
{
b = 0f;
}
multiplier = Mathf.Lerp(multiplier, b, 1f / smoothing);
if (multiplier < 0.1f)
{
ChangeStep();
}
}
else
{
ChangeStep();
}
}
private void ChangeStep()
{
if (isCurrentlyLeft != stepHandler.isLeft)
{
if (stepHandler.isLeft == isLeft)
{
switchToForwardEvent.Invoke();
}
else
{
switchToBackwardsEvent.Invoke();
}
isCurrentlyLeft = stepHandler.isLeft;
}
}
}
}
|