From 6ce8b9e22fc13be34b442c7b6af48b42cd44275a Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Wed, 13 Mar 2024 11:00:58 +0800 Subject: +init --- AnimationObject.cs | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 AnimationObject.cs (limited to 'AnimationObject.cs') diff --git a/AnimationObject.cs b/AnimationObject.cs new file mode 100644 index 0000000..0c2d636 --- /dev/null +++ b/AnimationObject.cs @@ -0,0 +1,140 @@ +using UnityEngine; +using UnityEngine.Events; + +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(); + rig = GetComponent(); + stepHandler = base.transform.root.GetComponent(); + holding = base.transform.root.GetComponent(); + death = base.transform.root.GetComponent(); + Hip componentInChildren = base.transform.root.GetComponentInChildren(); + 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(); + } + } + + 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; + } + } +} -- cgit v1.1-26-g67d0