diff options
author | chai <215380520@qq.com> | 2024-04-16 11:08:04 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-04-16 11:08:04 +0800 |
commit | 80983c575ec565078f757f638f3726708647080a (patch) | |
tree | 14cad7cf19d4670ecfba3f371e49540e0042155c /ActiveRagdoll/Assets/TABG/Scripts/Animation/AnimationObject.cs | |
parent | 0464810f2f83c86560c3422d664380d474cb3e56 (diff) |
*misc
Diffstat (limited to 'ActiveRagdoll/Assets/TABG/Scripts/Animation/AnimationObject.cs')
-rw-r--r-- | ActiveRagdoll/Assets/TABG/Scripts/Animation/AnimationObject.cs | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/ActiveRagdoll/Assets/TABG/Scripts/Animation/AnimationObject.cs b/ActiveRagdoll/Assets/TABG/Scripts/Animation/AnimationObject.cs new file mode 100644 index 0000000..9e9039f --- /dev/null +++ b/ActiveRagdoll/Assets/TABG/Scripts/Animation/AnimationObject.cs @@ -0,0 +1,153 @@ +using Rigging.Data; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Events; + +namespace Rigging.Animations +{ + + // 物理控制的动作 + public class AnimationObject : MonoBehaviour + { + + public 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 Holding holding; + + private AnimationHandler animationHandler; + + //private PlayerDeath death; + + private float muscleMultiplier = 1f; + + private void Start() + { + animationHandler = base.transform.root.GetComponent<AnimationHandler>(); + rig = GetComponent<Rigidbody>(); + stepHandler = base.transform.root.GetComponent<StepHandler>(); + //holding = base.transform.root.GetComponent<Holding>(); + //death = base.transform.root.GetComponent<PlayerDeath>(); + Hip componentInChildren = base.transform.root.GetComponentInChildren<Hip>(); + if ((bool)componentInChildren) + { + hip = componentInChildren.transform; + } + } + + private void FixedUpdate() + { + //if ((bool)death) + //{ + // if (death.muscleFunction < 0f) + // { + // return; + // } + // muscleMultiplier = death.muscleFunction; + //} + //if ((!holding || !dontAnimateIfHoldingSomething || !holding.heldObject) && animationHandler.animationState < animations.Length) + //{ + // SetMultiplier(); + // AddTorque(); + // AddForce(); + //} + SetMultiplier(); + AddTorque(); + AddForce(); + } + + private void AddForce() + { + Vector3 vector = Vector3.zero; + if (isCurrentlyLeft == isLeft) + { + Vector3 forwardForce = animations[animationHandler.animationState].forwardForce; + if (animations[animationHandler.animationState].forceSpace == PhysicsAnimation.ForceSpace.World) + { + vector = forwardForce; + } + if (animations[animationHandler.animationState].forceSpace == PhysicsAnimation.ForceSpace.Hip) + { + vector = hip.TransformDirection(forwardForce); + } + } + else + { + Vector3 backwardsForce = animations[animationHandler.animationState].backwardsForce; + if (animations[animationHandler.animationState].forceSpace == PhysicsAnimation.ForceSpace.World) + { + vector = backwardsForce; + } + if (animations[animationHandler.animationState].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[animationHandler.animationState].backwardsTorque) : hip.TransformDirection(animations[animationHandler.animationState].forwardTorque)); + rig.AddTorque(vector * muscleMultiplier * multiplier, 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; + } + } + } + +} |