summaryrefslogtreecommitdiff
path: root/_ActiveRagdoll/Balance.cs
blob: 06953fa0b09738fd51a34ce8f21790b759542652 (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
using UnityEngine;

//Player Balance 动作-控制身体平衡
public class Balance : MonoBehaviour
{
    #region rigs
    private Rigidbody handLeft;
	private Rigidbody handRight;
	private Rigidbody footLeft; // kneeLeft
	private Rigidbody footRight; // kneeRight
	private Rigidbody hip;
    #endregion 

    private Vector3 centerOfMass; // 5.0759, 0, -7.2038

    private Rigidbody[] allRigs;//所有14个parts

	public float[] balanceForce; // 50 30 30 30

	public float[] footCenterForces; // 50 20 20 20

	private AnimationHandler animationHandler; // 当前动作编号

	private PlayerDeath death;

	private Strength str;

	private float muscleMultiplier; // 1

	private void Start()
	{
		death = GetComponent<PlayerDeath>();
		str = GetComponent<Strength>();
		allRigs = GetComponentsInChildren<Rigidbody>();
		HandLeft componentInChildren = GetComponentInChildren<HandLeft>();
		if ((bool)componentInChildren)
		{
			handLeft = componentInChildren.GetComponent<Rigidbody>();
		}
		HandRight componentInChildren2 = GetComponentInChildren<HandRight>();
		if ((bool)componentInChildren2)
		{
			handRight = componentInChildren2.GetComponent<Rigidbody>();
		}
		footLeft = GetComponentInChildren<KneeLeft>().GetComponent<Rigidbody>();
		footRight = GetComponentInChildren<KneeRight>().GetComponent<Rigidbody>();
		hip = GetComponentInChildren<Hip>().GetComponent<Rigidbody>();
		animationHandler = GetComponent<AnimationHandler>();
	}

	private void FixedUpdate()
	{
		if (!death.dead)
		{
			muscleMultiplier = str.strength;
			centerOfMass = Vector3.zero;
			Rigidbody[] array = allRigs;
			foreach (Rigidbody rigidbody in array)
			{
				centerOfMass += rigidbody.worldCenterOfMass;
			}
			centerOfMass /= (float)allRigs.Length;
			centerOfMass.y = 0f;
			BalanceLegs();
			CenterLegs();
		}
	}

	private void CenterLegs()
	{
		Vector3 vector = footLeft.transform.position + footLeft.transform.forward * 0.5f;
		Vector3 vector2 = footRight.transform.position + footRight.transform.forward * 0.5f;
		Vector3 vector3 = vector;
		if (vector.y + 0.3f < hip.worldCenterOfMass.y)
		{
			vector3.y = 0f;
			footLeft.AddForceAtPosition((centerOfMass - vector3) * muscleMultiplier * footCenterForces[animationHandler.animationState], vector, ForceMode.Acceleration);
		}
		Vector3 vector4 = vector2;
		if (vector4.y + 0.3f < hip.worldCenterOfMass.y)
		{
			vector4.y = 0f;
			footRight.AddForceAtPosition((centerOfMass - vector4) * muscleMultiplier * footCenterForces[animationHandler.animationState], vector2, ForceMode.Acceleration);
		}
	}

	private void BalanceLegs()
	{
		Vector3 vector = footLeft.transform.position + footLeft.transform.forward * 0.5f;
		Vector3 vector2 = footRight.transform.position + footRight.transform.forward * 0.5f;
		Vector3 vector3 = (vector + vector2) / 2f;

		Vector3 pos = vector3;
		pos.y += 0.3f;

        if (!(vector3.y + 0.3f > hip.worldCenterOfMass.y))
		{
			vector3.y = 0f;
			Vector3 vector4 = centerOfMass - vector3; // centerOfMass是xoz平面,不含y
            REPL.footCenter.GetComponent<DebugRigidBody>().customDraw = () =>
            {
                Draw.Sphere(vector, 0.05f);
                Draw.Sphere(vector2, 0.05f);
                Draw.Sphere(pos, 0.05f);
                Draw.Sphere(vector3, 0.05f);
                Draw.Sphere(hip.worldCenterOfMass, 0.2f);

                Draw.Sphere(centerOfMass, 0.05f);
                Draw.Line3D(vector3 + new Vector3(0, footLeft.transform.position.y, 0), centerOfMass + new Vector3(0, footLeft.transform.position.y, 0));
            };

            footLeft.AddForceAtPosition(vector4 * muscleMultiplier * balanceForce[animationHandler.animationState], vector, ForceMode.Acceleration);
			footRight.AddForceAtPosition(vector4 * muscleMultiplier * balanceForce[animationHandler.animationState], vector2, ForceMode.Acceleration);
		}
	}
}