From 0f725335c208e3a1ec109d82512a4201cf37123f Mon Sep 17 00:00:00 2001 From: Eruandou Date: Sun, 20 Aug 2023 17:22:41 -0300 Subject: [PATCH 01/19] Add Input Handler for ragdoll --- .../ActiveRagdoll/RagdollController.cs | 72 ++++++++++--------- .../ActiveRagdoll/RagdollHandContact.cs | 12 ++-- .../ActiveRagdoll/RagdollInputHandler.cs | 20 ++++++ .../ActiveRagdoll/RagdollInputHandler.cs.meta | 3 + .../Utils/QuaternionExtensions.cs.meta | 11 +++ .../Scripts/Utils/Vector3Extensions.cs.meta | 11 +++ 6 files changed, 90 insertions(+), 39 deletions(-) create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollInputHandler.cs create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollInputHandler.cs.meta create mode 100644 Assets/Scripts/Utils/QuaternionExtensions.cs.meta create mode 100644 Assets/Scripts/Utils/Vector3Extensions.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index 02e51df..12ddf5e 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -1,16 +1,16 @@ using System; using System.Collections; using System.Runtime.CompilerServices; +using ActiveRagdoll; using UnityEngine; using Utils; -public class RagdollController : MonoBehaviour, IInputListener +public class RagdollController : MonoBehaviour { [SerializeField] private UDictionary RagdollDict = new UDictionary(); [SerializeField] private Rigidbody rightHand; [SerializeField] private Rigidbody leftHand; - [SerializeField] private Transform centerOfMass; [Header("Movement Properties")] public bool forwardIsCameraDirection = true; @@ -18,14 +18,6 @@ public class RagdollController : MonoBehaviour, IInputListener public float turnSpeed = 6f; public float jumpForce = 18f; - public Vector2 MovementAxis { get; set; } = Vector2.zero; - public Vector2 AimAxis { get; set; } - public float JumpValue { get; set; } = 0; - public float GrabLeftValue { get; set; } = 0; - public float GrabRightValue { get; set; } = 0; - public bool PunchLeftValue { get; set; } - public bool PunchRightValue { get; set; } - [Header("Balance Properties")] public bool autoGetUpWhenPossible = true; public bool useStepPrediction = true; public float balanceHeight = 2.5f; @@ -83,6 +75,7 @@ public class RagdollController : MonoBehaviour, IInputListener private bool reachLeftAxisUsed; private bool reachRightAxisUsed; + private readonly RagdollInputHandler inputHandler = new(); [HideInInspector] public bool jumping; [HideInInspector] public bool isJumping; [HideInInspector] public bool inAir; @@ -111,15 +104,24 @@ public class RagdollController : MonoBehaviour, IInputListener private Quaternion LowerLeftLegTarget; private static int groundLayer; - private WaitForSeconds punchDelayWaitTime = new WaitForSeconds(0.3f); + private readonly WaitForSeconds punchDelayWaitTime = new(0.3f); void Awake() { - //cam = Camera.main; - InputManager.Instance.RegisterListener(this); groundLayer = LayerMask.NameToLayer("Ground"); + inputHandler.Init(); SetupJointDrives(); SetupOriginalPose(); + SetupHandContacts(); + } + + private void SetupHandContacts() + { + RagdollHandContact[] handContacts = GetComponentsInChildren(); + foreach (var handContact in handContacts) + { + handContact.Init(inputHandler); + } } private void SetupJointDrives() @@ -168,7 +170,6 @@ private void Update() if (balanced && useStepPrediction) { PerformStepPrediction(); - UpdateCenterOfMass(); } if (!useStepPrediction) @@ -204,10 +205,10 @@ private void PerformPlayerRotation() //buffer all changes to quaternion before applying to memory location Quaternion rootJointTargetRotation = rootJoint.targetRotation; - if (MovementAxis.x != 0) + if (inputHandler.MovementAxis.x != 0) { rootJointTargetRotation = Quaternion.Lerp(rootJointTargetRotation, - rootJointTargetRotation.DisplaceY(-MovementAxis.x * turnSpeed), + rootJointTargetRotation.DisplaceY(-inputHandler.MovementAxis.x * turnSpeed), 6 * Time.fixedDeltaTime); } @@ -256,7 +257,7 @@ private bool CanResetPoseAfterLanding() private void PerformPlayerGetUpJumping() { - if (JumpValue > 0) + if (inputHandler.JumpValue > 0) { if (!jumpAxisUsed) { @@ -403,18 +404,19 @@ private void PlayerMovement() private void MoveInCameraDirection() { - Direction = RagdollDict[ROOT].transform.rotation * new Vector3(MovementAxis.x, 0.0f, MovementAxis.y); + Direction = RagdollDict[ROOT].transform.rotation * + new Vector3(inputHandler.MovementAxis.x, 0.0f, inputHandler.MovementAxis.y); Direction.y = 0f; Rigidbody rootRigidbody = RagdollDict[ROOT].Rigidbody; var velocity = rootRigidbody.velocity; rootRigidbody.velocity = Vector3.Lerp(velocity, (Direction * moveSpeed) + new Vector3(0, velocity.y, 0), 0.8f); - if (MovementAxis.x != 0 || MovementAxis.y != 0 && balanced) + if (inputHandler.MovementAxis.x != 0 || inputHandler.MovementAxis.y != 0 && balanced) { StartWalkingForward(); } - else if (MovementAxis is { x: 0, y: 0 }) + else if (inputHandler.MovementAxis is { x: 0, y: 0 }) { StopWalkingForward(); } @@ -442,19 +444,19 @@ private void StopWalkingForward() private void MoveInOwnDirection() { - if (MovementAxis.y != 0) + if (inputHandler.MovementAxis.y != 0) { var rootRigidbody = RagdollDict[ROOT].Rigidbody; - var v3 = rootRigidbody.transform.forward * (MovementAxis.y * moveSpeed); + var v3 = rootRigidbody.transform.forward * (inputHandler.MovementAxis.y * moveSpeed); v3.y = rootRigidbody.velocity.y; rootRigidbody.velocity = v3; } - if (MovementAxis.y > 0) + if (inputHandler.MovementAxis.y > 0) { StartWalkingForwardInOwnDirection(); } - else if (MovementAxis.y < 0) + else if (inputHandler.MovementAxis.y < 0) { StartWalkingBackward(); } @@ -507,10 +509,10 @@ private void SetJointAngularDrivesForLegs(in JointDrive jointDrive) private void PlayerReach() { - MouseYAxisBody = Mathf.Clamp(MouseYAxisBody += (AimAxis.y / reachSensitivity), -0.2f, 0.1f); + MouseYAxisBody = Mathf.Clamp(MouseYAxisBody += (inputHandler.AimAxis.y / reachSensitivity), -0.2f, 0.1f); RagdollDict[BODY].Joint.targetRotation = new Quaternion(MouseYAxisBody, 0, 0, 1); - if (GrabLeftValue != 0 && !punchingLeft) + if (inputHandler.GrabLeftValue != 0 && !punchingLeft) { if (!reachLeftAxisUsed) { @@ -521,12 +523,12 @@ private void PlayerReach() reachLeftAxisUsed = true; } - MouseYAxisArms = Mathf.Clamp(MouseYAxisArms += (AimAxis.y / reachSensitivity), -1.2f, 1.2f); + MouseYAxisArms = Mathf.Clamp(MouseYAxisArms += (inputHandler.AimAxis.y / reachSensitivity), -1.2f, 1.2f); RagdollDict[UPPER_LEFT_ARM].Joint.targetRotation = new Quaternion(-0.58f - (MouseYAxisArms), -0.88f - (MouseYAxisArms), -0.8f, 1); } - if (GrabLeftValue == 0 && !punchingLeft) + if (inputHandler.GrabLeftValue == 0 && !punchingLeft) { if (reachLeftAxisUsed) { @@ -547,7 +549,7 @@ private void PlayerReach() } } - if (GrabRightValue != 0 && !punchingRight) + if (inputHandler.GrabRightValue != 0 && !punchingRight) { if (!reachRightAxisUsed) { @@ -557,12 +559,12 @@ private void PlayerReach() reachRightAxisUsed = true; } - MouseYAxisArms = Mathf.Clamp(MouseYAxisArms += (AimAxis.y / reachSensitivity), -1.2f, 1.2f); + MouseYAxisArms = Mathf.Clamp(MouseYAxisArms += (inputHandler.AimAxis.y / reachSensitivity), -1.2f, 1.2f); RagdollDict[UPPER_RIGHT_ARM].Joint.targetRotation = new Quaternion(0.58f + (MouseYAxisArms), -0.88f - (MouseYAxisArms), 0.8f, 1); } - if (GrabRightValue == 0 && !punchingRight) + if (inputHandler.GrabRightValue == 0 && !punchingRight) { if (reachRightAxisUsed) { @@ -637,7 +639,7 @@ private IEnumerator PunchDelayCoroutine(bool isRightArm, ConfigurableJoint upper { yield return punchDelayWaitTime; //Mainly because we can't pass in ref of bool value to coroutine, if not using unsafe void* - bool punchValueToCheck = isRightArm ? PunchRightValue : PunchLeftValue; + bool punchValueToCheck = isRightArm ? inputHandler.PunchRightValue : inputHandler.PunchLeftValue; if (punchValueToCheck) yield break; upperArmJoint.targetRotation = upperArmTarget.Invoke(); @@ -645,10 +647,11 @@ private IEnumerator PunchDelayCoroutine(bool isRightArm, ConfigurableJoint upper } private void HandleLeftPunch() => - HandlePunch(ref punchingLeft, PunchLeftValue, false, UPPER_LEFT_ARM, LOWER_LEFT_ARM, leftHand, + HandlePunch(ref punchingLeft, inputHandler.PunchLeftValue, false, UPPER_LEFT_ARM, LOWER_LEFT_ARM, leftHand, () => UpperLeftArmTarget, () => LowerLeftArmTarget); - private void HandleRightPunch() => HandlePunch(ref punchingRight, PunchRightValue, true, UPPER_RIGHT_ARM, + private void HandleRightPunch() => HandlePunch(ref punchingRight, inputHandler.PunchRightValue, true, + UPPER_RIGHT_ARM, LOWER_RIGHT_ARM, rightHand, () => UpperRightArmTarget, () => LowerLeftArmTarget); private void PerformWalking() @@ -851,7 +854,6 @@ private void PerformStepPrediction() [MethodImpl(MethodImplOptions.AggressiveInlining)] private void UpdateCenterOfMass() { - //Be wary of this, it's called up to 2 times per frame Vector3 massPositionDisplacement = Vector3.zero; float totalMass = 0; diff --git a/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs b/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs index 80f63c8..a057540 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs @@ -7,10 +7,14 @@ public class RagdollHandContact : MonoBehaviour public bool hasJoint; private const string CAN_BE_GRABBED = "CanBeGrabbed"; - + private IInputListener inputListener; + public void Init(IInputListener newInputListener) + { + inputListener = newInputListener; + } private void Update() { - HandleJointRelease(Left ? ragdollController.GrabLeftValue : ragdollController.GrabRightValue); + HandleJointRelease(Left ? inputListener.GrabLeftValue : inputListener.GrabRightValue); } private void HandleJointRelease(float reachAxisValue) @@ -53,11 +57,11 @@ private bool CanPerformGrabAction() { if (Left) { - return ragdollController.GrabLeftValue != 0 && !ragdollController.punchingLeft; + return inputListener.GrabLeftValue != 0 && !ragdollController.punchingLeft; } else { - return ragdollController.GrabRightValue != 0 && !ragdollController.punchingRight; + return inputListener.GrabRightValue != 0 && !ragdollController.punchingRight; } } diff --git a/Assets/Scripts/ActiveRagdoll/RagdollInputHandler.cs b/Assets/Scripts/ActiveRagdoll/RagdollInputHandler.cs new file mode 100644 index 0000000..a64d6a9 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollInputHandler.cs @@ -0,0 +1,20 @@ +using UnityEngine; + +namespace ActiveRagdoll +{ + public class RagdollInputHandler : IInputListener + { + public Vector2 MovementAxis { get; set; } + public Vector2 AimAxis { get; set; } + public float JumpValue { get; set; } + public float GrabLeftValue { get; set; } + public float GrabRightValue { get; set; } + public bool PunchLeftValue { get; set; } + public bool PunchRightValue { get; set; } + + public void Init() + { + InputManager.Instance.RegisterListener(this); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollInputHandler.cs.meta b/Assets/Scripts/ActiveRagdoll/RagdollInputHandler.cs.meta new file mode 100644 index 0000000..25f5c4c --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollInputHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0a4935dcad5f4b60897926ee70ebefd7 +timeCreated: 1692562239 \ No newline at end of file diff --git a/Assets/Scripts/Utils/QuaternionExtensions.cs.meta b/Assets/Scripts/Utils/QuaternionExtensions.cs.meta new file mode 100644 index 0000000..4819559 --- /dev/null +++ b/Assets/Scripts/Utils/QuaternionExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4de070c76d377a242a84c2a508bde949 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Utils/Vector3Extensions.cs.meta b/Assets/Scripts/Utils/Vector3Extensions.cs.meta new file mode 100644 index 0000000..0a3ea6e --- /dev/null +++ b/Assets/Scripts/Utils/Vector3Extensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ef57b5b2ba5d2364793179efccbfc14b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From b2fea712e4b96778d46686c2990b5dbee68ae6f4 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Sun, 20 Aug 2023 18:13:43 -0300 Subject: [PATCH 02/19] Refactor player reach calculation --- .../ActiveRagdoll/RagdollController.cs | 93 +++++++------------ Assets/Scripts/Utils/SingletonObject.cs | 6 ++ 2 files changed, 42 insertions(+), 57 deletions(-) diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index 12ddf5e..58cdfef 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -512,79 +512,58 @@ private void PlayerReach() MouseYAxisBody = Mathf.Clamp(MouseYAxisBody += (inputHandler.AimAxis.y / reachSensitivity), -0.2f, 0.1f); RagdollDict[BODY].Joint.targetRotation = new Quaternion(MouseYAxisBody, 0, 0, 1); - if (inputHandler.GrabLeftValue != 0 && !punchingLeft) + HandleLeftSideReach(); + HandleRightSideReach(); + } + + private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reachSideAxisUsed, string upperArmJoint, + string lowerArmJoint, bool isRightArm) + { + if (punchingSide) + return; + if (grabValue != 0) { - if (!reachLeftAxisUsed) + if (!reachSideAxisUsed) { - SetJointAngularDrives(UPPER_LEFT_ARM, in ReachStiffness); - SetJointAngularDrives(LOWER_LEFT_ARM, in ReachStiffness); + SetJointAngularDrives(upperArmJoint, in ReachStiffness); + SetJointAngularDrives(lowerArmJoint, in ReachStiffness); SetJointAngularDrives(BODY, in CoreStiffness); - reachLeftAxisUsed = true; - reachLeftAxisUsed = true; + reachSideAxisUsed = true; } + int multiplier = isRightArm ? 1 : -1; MouseYAxisArms = Mathf.Clamp(MouseYAxisArms += (inputHandler.AimAxis.y / reachSensitivity), -1.2f, 1.2f); - RagdollDict[UPPER_LEFT_ARM].Joint.targetRotation = - new Quaternion(-0.58f - (MouseYAxisArms), -0.88f - (MouseYAxisArms), -0.8f, 1); + RagdollDict[upperArmJoint].Joint.targetRotation = + new Quaternion((0.58f + (MouseYAxisArms)) * multiplier, -0.88f - (MouseYAxisArms), 0.8f * multiplier, + 1); } - - if (inputHandler.GrabLeftValue == 0 && !punchingLeft) + else { - if (reachLeftAxisUsed) + if (!reachSideAxisUsed) + return; + if (balanced) { - if (balanced) - { - SetJointAngularDrives(UPPER_LEFT_ARM, in PoseOn); - SetJointAngularDrives(LOWER_LEFT_ARM, in PoseOn); - SetJointAngularDrives(BODY, in PoseOn); - } - else if (!balanced) - { - SetJointAngularDrives(UPPER_LEFT_ARM, in DriveOff); - SetJointAngularDrives(LOWER_LEFT_ARM, in DriveOff); - } - - ResetPose = true; - reachLeftAxisUsed = false; + SetJointAngularDrives(upperArmJoint, in PoseOn); + SetJointAngularDrives(lowerArmJoint, in PoseOn); + SetJointAngularDrives(BODY, in PoseOn); } - } - - if (inputHandler.GrabRightValue != 0 && !punchingRight) - { - if (!reachRightAxisUsed) + else { - SetJointAngularDrives(UPPER_RIGHT_ARM, in ReachStiffness); - SetJointAngularDrives(LOWER_RIGHT_ARM, in ReachStiffness); - SetJointAngularDrives(BODY, in CoreStiffness); - reachRightAxisUsed = true; + SetJointAngularDrives(upperArmJoint, in DriveOff); + SetJointAngularDrives(lowerArmJoint, in DriveOff); } - MouseYAxisArms = Mathf.Clamp(MouseYAxisArms += (inputHandler.AimAxis.y / reachSensitivity), -1.2f, 1.2f); - RagdollDict[UPPER_RIGHT_ARM].Joint.targetRotation = - new Quaternion(0.58f + (MouseYAxisArms), -0.88f - (MouseYAxisArms), 0.8f, 1); + ResetPose = true; + reachSideAxisUsed = false; } + } - if (inputHandler.GrabRightValue == 0 && !punchingRight) - { - if (reachRightAxisUsed) - { - if (balanced) - { - SetJointAngularDrives(UPPER_RIGHT_ARM, in PoseOn); - SetJointAngularDrives(LOWER_RIGHT_ARM, in PoseOn); - SetJointAngularDrives(BODY, in PoseOn); - } - else if (!balanced) - { - SetJointAngularDrives(UPPER_RIGHT_ARM, in DriveOff); - SetJointAngularDrives(LOWER_RIGHT_ARM, in DriveOff); - } - ResetPose = true; - reachRightAxisUsed = false; - } - } - } + private void HandleRightSideReach() => HandlePlayerReach(punchingRight, inputHandler.GrabRightValue, + ref reachRightAxisUsed, UPPER_RIGHT_ARM, LOWER_RIGHT_ARM, true); + + private void HandleLeftSideReach() => HandlePlayerReach(punchingLeft, inputHandler.GrabLeftValue, + ref reachLeftAxisUsed, UPPER_LEFT_ARM, LOWER_LEFT_ARM, false); private void PerformPlayerPunch() { diff --git a/Assets/Scripts/Utils/SingletonObject.cs b/Assets/Scripts/Utils/SingletonObject.cs index 3e723c1..4f9f222 100644 --- a/Assets/Scripts/Utils/SingletonObject.cs +++ b/Assets/Scripts/Utils/SingletonObject.cs @@ -21,4 +21,10 @@ public static T Instance return instance; } } + + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] + private static void ResetInstance() + { + instance = null; + } } \ No newline at end of file From 78d1de07eeedd4d945d82c9c17505f3ddbf15da4 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Sun, 20 Aug 2023 18:27:34 -0300 Subject: [PATCH 03/19] Separate const ragdoll parts into class --- .../ActiveRagdoll/RagdollController.cs | 144 ++++++++---------- Assets/Scripts/ActiveRagdoll/RagdollParts.cs | 19 +++ .../ActiveRagdoll/RagdollParts.cs.meta | 3 + 3 files changed, 87 insertions(+), 79 deletions(-) create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollParts.cs create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollParts.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index 58cdfef..6f12658 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -37,20 +37,6 @@ public class RagdollController : MonoBehaviour public bool canPunch = true; public float punchForce = 15f; - private const string ROOT = "Root"; - private const string BODY = "Body"; - private const string HEAD = "Head"; - private const string UPPER_RIGHT_ARM = "UpperRightArm"; - private const string LOWER_RIGHT_ARM = "LowerRightArm"; - private const string UPPER_LEFT_ARM = "UpperLeftArm"; - private const string LOWER_LEFT_ARM = "LowerLeftArm"; - private const string UPPER_RIGHT_LEG = "UpperRightLeg"; - private const string LOWER_RIGHT_LEG = "LowerRightLeg"; - private const string UPPER_LEFT_LEG = "UpperLeftLeg"; - private const string LOWER_LEFT_LEG = "LowerLeftLeg"; - private const string RIGHT_FOOT = "RightFoot"; - private const string LEFT_FOOT = "LeftFoot"; - //Hidden variables private float timer; private float Step_R_timer; @@ -135,16 +121,16 @@ private void SetupJointDrives() private void SetupOriginalPose() { - BodyTarget = GetJointTargetRotation(ROOT); - HeadTarget = GetJointTargetRotation(HEAD); - UpperRightArmTarget = GetJointTargetRotation(UPPER_RIGHT_ARM); - LowerRightArmTarget = GetJointTargetRotation(LOWER_RIGHT_ARM); - UpperLeftArmTarget = GetJointTargetRotation(UPPER_LEFT_ARM); - LowerLeftArmTarget = GetJointTargetRotation(LOWER_LEFT_ARM); - UpperRightLegTarget = GetJointTargetRotation(UPPER_RIGHT_LEG); - LowerRightLegTarget = GetJointTargetRotation(LOWER_RIGHT_LEG); - UpperLeftLegTarget = GetJointTargetRotation(UPPER_LEFT_LEG); - LowerLeftLegTarget = GetJointTargetRotation(LOWER_LEFT_LEG); + BodyTarget = GetJointTargetRotation(RagdollParts.ROOT); + HeadTarget = GetJointTargetRotation(RagdollParts.HEAD); + UpperRightArmTarget = GetJointTargetRotation(RagdollParts.UPPER_RIGHT_ARM); + LowerRightArmTarget = GetJointTargetRotation(RagdollParts.LOWER_RIGHT_ARM); + UpperLeftArmTarget = GetJointTargetRotation(RagdollParts.UPPER_LEFT_ARM); + LowerLeftArmTarget = GetJointTargetRotation(RagdollParts.LOWER_LEFT_ARM); + UpperRightLegTarget = GetJointTargetRotation(RagdollParts.UPPER_RIGHT_LEG); + LowerRightLegTarget = GetJointTargetRotation(RagdollParts.LOWER_RIGHT_LEG); + UpperLeftLegTarget = GetJointTargetRotation(RagdollParts.UPPER_LEFT_LEG); + LowerLeftLegTarget = GetJointTargetRotation(RagdollParts.LOWER_LEFT_LEG); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -191,7 +177,7 @@ private void FixedUpdate() private void PerformPlayerRotation() { - ConfigurableJoint rootJoint = RagdollDict[ROOT].Joint; + ConfigurableJoint rootJoint = RagdollDict[RagdollParts.ROOT].Joint; if (forwardIsCameraDirection) { @@ -230,11 +216,11 @@ private void ResetPlayerPose() { if (ResetPose && !jumping) { - RagdollDict[BODY].Joint.targetRotation = BodyTarget; - RagdollDict[UPPER_RIGHT_ARM].Joint.targetRotation = UpperRightArmTarget; - RagdollDict[LOWER_RIGHT_ARM].Joint.targetRotation = LowerRightArmTarget; - RagdollDict[UPPER_LEFT_ARM].Joint.targetRotation = UpperLeftArmTarget; - RagdollDict[LOWER_LEFT_ARM].Joint.targetRotation = LowerLeftArmTarget; + RagdollDict[RagdollParts.BODY].Joint.targetRotation = BodyTarget; + RagdollDict[RagdollParts.UPPER_RIGHT_ARM].Joint.targetRotation = UpperRightArmTarget; + RagdollDict[RagdollParts.LOWER_RIGHT_ARM].Joint.targetRotation = LowerRightArmTarget; + RagdollDict[RagdollParts.UPPER_LEFT_ARM].Joint.targetRotation = UpperLeftArmTarget; + RagdollDict[RagdollParts.LOWER_LEFT_ARM].Joint.targetRotation = LowerLeftArmTarget; MouseYAxisArms = 0; ResetPose = false; @@ -285,7 +271,7 @@ private void PerformPlayerGetUpJumping() { isJumping = true; - Rigidbody rootRigidbody = RagdollDict[ROOT].Rigidbody; + Rigidbody rootRigidbody = RagdollDict[RagdollParts.ROOT].Rigidbody; rootRigidbody.velocity = rootRigidbody.velocity.ModifyY(rootRigidbody.transform.up.y * jumpForce); } @@ -305,7 +291,7 @@ private void PerformPlayerGetUpJumping() private void GroundCheck() { - Transform rootTransform = RagdollDict[ROOT].transform; + Transform rootTransform = RagdollDict[RagdollParts.ROOT].transform; Ray ray = new Ray(rootTransform.position, -rootTransform.up); bool isHittingGround = Physics.Raycast(ray, out _, balanceHeight, 1 << groundLayer); @@ -343,7 +329,7 @@ private bool ShouldSetBalanced() !reachRightAxisUsed && !reachLeftAxisUsed && !balanced && - RagdollDict[ROOT].Rigidbody.velocity.magnitude < 1f && + RagdollDict[RagdollParts.ROOT].Rigidbody.velocity.magnitude < 1f && autoGetUpWhenPossible; } @@ -354,27 +340,27 @@ private void SetRagdollState(bool shouldRagdoll, ref JointDrive rootJointDrive, isRagdoll = shouldRagdoll; balanced = !shouldRagdoll; - SetJointAngularDrives(ROOT, in rootJointDrive); - SetJointAngularDrives(HEAD, in poseJointDrive); + SetJointAngularDrives(RagdollParts.ROOT, in rootJointDrive); + SetJointAngularDrives(RagdollParts.HEAD, in poseJointDrive); if (!reachRightAxisUsed) { - SetJointAngularDrives(UPPER_RIGHT_ARM, in poseJointDrive); - SetJointAngularDrives(LOWER_RIGHT_ARM, in poseJointDrive); + SetJointAngularDrives(RagdollParts.UPPER_RIGHT_ARM, in poseJointDrive); + SetJointAngularDrives(RagdollParts.LOWER_RIGHT_ARM, in poseJointDrive); } if (!reachLeftAxisUsed) { - SetJointAngularDrives(UPPER_LEFT_ARM, in poseJointDrive); - SetJointAngularDrives(LOWER_LEFT_ARM, in poseJointDrive); + SetJointAngularDrives(RagdollParts.UPPER_LEFT_ARM, in poseJointDrive); + SetJointAngularDrives(RagdollParts.LOWER_LEFT_ARM, in poseJointDrive); } - SetJointAngularDrives(UPPER_RIGHT_LEG, in poseJointDrive); - SetJointAngularDrives(LOWER_RIGHT_LEG, in poseJointDrive); - SetJointAngularDrives(UPPER_LEFT_LEG, in poseJointDrive); - SetJointAngularDrives(LOWER_LEFT_LEG, in poseJointDrive); - SetJointAngularDrives(RIGHT_FOOT, in poseJointDrive); - SetJointAngularDrives(LEFT_FOOT, in poseJointDrive); + SetJointAngularDrives(RagdollParts.UPPER_RIGHT_LEG, in poseJointDrive); + SetJointAngularDrives(RagdollParts.LOWER_RIGHT_LEG, in poseJointDrive); + SetJointAngularDrives(RagdollParts.UPPER_LEFT_LEG, in poseJointDrive); + SetJointAngularDrives(RagdollParts.LOWER_LEFT_LEG, in poseJointDrive); + SetJointAngularDrives(RagdollParts.RIGHT_FOOT, in poseJointDrive); + SetJointAngularDrives(RagdollParts.LEFT_FOOT, in poseJointDrive); if (shouldResetPose) ResetPose = true; @@ -404,10 +390,10 @@ private void PlayerMovement() private void MoveInCameraDirection() { - Direction = RagdollDict[ROOT].transform.rotation * + Direction = RagdollDict[RagdollParts.ROOT].transform.rotation * new Vector3(inputHandler.MovementAxis.x, 0.0f, inputHandler.MovementAxis.y); Direction.y = 0f; - Rigidbody rootRigidbody = RagdollDict[ROOT].Rigidbody; + Rigidbody rootRigidbody = RagdollDict[RagdollParts.ROOT].Rigidbody; var velocity = rootRigidbody.velocity; rootRigidbody.velocity = Vector3.Lerp(velocity, (Direction * moveSpeed) + new Vector3(0, velocity.y, 0), 0.8f); @@ -446,7 +432,7 @@ private void MoveInOwnDirection() { if (inputHandler.MovementAxis.y != 0) { - var rootRigidbody = RagdollDict[ROOT].Rigidbody; + var rootRigidbody = RagdollDict[RagdollParts.ROOT].Rigidbody; var v3 = rootRigidbody.transform.forward * (inputHandler.MovementAxis.y * moveSpeed); v3.y = rootRigidbody.velocity.y; rootRigidbody.velocity = v3; @@ -499,18 +485,18 @@ private void InternalChangeWalkState(bool walkForward, bool walkBackward, bool i private void SetJointAngularDrivesForLegs(in JointDrive jointDrive) { - SetJointAngularDrives(UPPER_RIGHT_LEG, in jointDrive); - SetJointAngularDrives(LOWER_RIGHT_LEG, in jointDrive); - SetJointAngularDrives(UPPER_LEFT_LEG, in jointDrive); - SetJointAngularDrives(LOWER_LEFT_LEG, in jointDrive); - SetJointAngularDrives(RIGHT_FOOT, in jointDrive); - SetJointAngularDrives(LEFT_FOOT, in jointDrive); + SetJointAngularDrives(RagdollParts.UPPER_RIGHT_LEG, in jointDrive); + SetJointAngularDrives(RagdollParts.LOWER_RIGHT_LEG, in jointDrive); + SetJointAngularDrives(RagdollParts.UPPER_LEFT_LEG, in jointDrive); + SetJointAngularDrives(RagdollParts.LOWER_LEFT_LEG, in jointDrive); + SetJointAngularDrives(RagdollParts.RIGHT_FOOT, in jointDrive); + SetJointAngularDrives(RagdollParts.LEFT_FOOT, in jointDrive); } private void PlayerReach() { MouseYAxisBody = Mathf.Clamp(MouseYAxisBody += (inputHandler.AimAxis.y / reachSensitivity), -0.2f, 0.1f); - RagdollDict[BODY].Joint.targetRotation = new Quaternion(MouseYAxisBody, 0, 0, 1); + RagdollDict[RagdollParts.BODY].Joint.targetRotation = new Quaternion(MouseYAxisBody, 0, 0, 1); HandleLeftSideReach(); HandleRightSideReach(); @@ -527,7 +513,7 @@ private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reac { SetJointAngularDrives(upperArmJoint, in ReachStiffness); SetJointAngularDrives(lowerArmJoint, in ReachStiffness); - SetJointAngularDrives(BODY, in CoreStiffness); + SetJointAngularDrives(RagdollParts.BODY, in CoreStiffness); reachSideAxisUsed = true; } @@ -545,7 +531,7 @@ private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reac { SetJointAngularDrives(upperArmJoint, in PoseOn); SetJointAngularDrives(lowerArmJoint, in PoseOn); - SetJointAngularDrives(BODY, in PoseOn); + SetJointAngularDrives(RagdollParts.BODY, in PoseOn); } else { @@ -560,10 +546,10 @@ private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reac private void HandleRightSideReach() => HandlePlayerReach(punchingRight, inputHandler.GrabRightValue, - ref reachRightAxisUsed, UPPER_RIGHT_ARM, LOWER_RIGHT_ARM, true); + ref reachRightAxisUsed, RagdollParts.UPPER_RIGHT_ARM, RagdollParts.LOWER_RIGHT_ARM, true); private void HandleLeftSideReach() => HandlePlayerReach(punchingLeft, inputHandler.GrabLeftValue, - ref reachLeftAxisUsed, UPPER_LEFT_ARM, LOWER_LEFT_ARM, false); + ref reachLeftAxisUsed, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, false); private void PerformPlayerPunch() { @@ -584,7 +570,7 @@ private void HandlePunch( if (punchingArmState == punchingArmValue) return; - ConfigurableJoint bodyJoint = RagdollDict[BODY].Joint; + ConfigurableJoint bodyJoint = RagdollDict[RagdollParts.BODY].Joint; ConfigurableJoint upperArmJoint = RagdollDict[upperArmLabel].Joint; ConfigurableJoint lowerArmJoint = RagdollDict[lowerArmLabel].Joint; @@ -605,8 +591,8 @@ private void HandlePunch( upperArmJoint.targetRotation = new Quaternion(-0.74f * punchRotationMultiplier, 0.04f, 0f, 1); lowerArmJoint.targetRotation = new Quaternion(-0.2f * punchRotationMultiplier, 0, 0, 1); - handRigidbody.AddForce(RagdollDict[ROOT].transform.forward * punchForce, ForceMode.Impulse); - RagdollDict[BODY].Rigidbody.AddForce(RagdollDict[BODY].transform.forward * punchForce, ForceMode.Impulse); + handRigidbody.AddForce(RagdollDict[RagdollParts.ROOT].transform.forward * punchForce, ForceMode.Impulse); + RagdollDict[RagdollParts.BODY].Rigidbody.AddForce(RagdollDict[RagdollParts.BODY].transform.forward * punchForce, ForceMode.Impulse); StartCoroutine(PunchDelayCoroutine(isRightPunch, upperArmJoint, lowerArmJoint, upperArmTargetMethod, lowerArmTargetMethod)); @@ -626,12 +612,12 @@ private IEnumerator PunchDelayCoroutine(bool isRightArm, ConfigurableJoint upper } private void HandleLeftPunch() => - HandlePunch(ref punchingLeft, inputHandler.PunchLeftValue, false, UPPER_LEFT_ARM, LOWER_LEFT_ARM, leftHand, + HandlePunch(ref punchingLeft, inputHandler.PunchLeftValue, false, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, leftHand, () => UpperLeftArmTarget, () => LowerLeftArmTarget); private void HandleRightPunch() => HandlePunch(ref punchingRight, inputHandler.PunchRightValue, true, - UPPER_RIGHT_ARM, - LOWER_RIGHT_ARM, rightHand, () => UpperRightArmTarget, () => LowerLeftArmTarget); + RagdollParts.UPPER_RIGHT_ARM, + RagdollParts.LOWER_RIGHT_ARM, rightHand, () => UpperRightArmTarget, () => LowerLeftArmTarget); private void PerformWalking() { @@ -668,9 +654,9 @@ private void PerformWalking() } private void ResetStepLeft() => - ResetStep(UPPER_LEFT_LEG, LOWER_LEFT_LEG, in UpperLeftLegTarget, in LowerLeftLegTarget, 7f, 18f); + ResetStep(RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, in UpperLeftLegTarget, in LowerLeftLegTarget, 7f, 18f); - private void ResetStepRight() => ResetStep(UPPER_RIGHT_LEG, LOWER_RIGHT_LEG, in UpperRightLegTarget, + private void ResetStepRight() => ResetStep(RagdollParts.UPPER_RIGHT_LEG, RagdollParts.LOWER_RIGHT_LEG, in UpperRightLegTarget, in LowerRightLegTarget, 8f, 17f); private void ResetStep(string upperLegLabel, @@ -688,15 +674,15 @@ private void ResetStep(string upperLegLabel, lowerLegLerpMultiplier * Time.fixedDeltaTime); Vector3 feetForce = -Vector3.up * (FeetMountForce * Time.deltaTime); - RagdollDict[RIGHT_FOOT].Rigidbody.AddForce(feetForce, ForceMode.Impulse); - RagdollDict[LEFT_FOOT].Rigidbody.AddForce(feetForce, ForceMode.Impulse); + RagdollDict[RagdollParts.RIGHT_FOOT].Rigidbody.AddForce(feetForce, ForceMode.Impulse); + RagdollDict[RagdollParts.LEFT_FOOT].Rigidbody.AddForce(feetForce, ForceMode.Impulse); } - private void TakeStepLeft() => TakeStep(ref Step_L_timer, LEFT_FOOT, ref StepLeft, ref StepRight, UPPER_LEFT_LEG, - LOWER_LEFT_LEG, UPPER_RIGHT_LEG); + private void TakeStepLeft() => TakeStep(ref Step_L_timer, RagdollParts.LEFT_FOOT, ref StepLeft, ref StepRight, RagdollParts.UPPER_LEFT_LEG, + RagdollParts.LOWER_LEFT_LEG, RagdollParts.UPPER_RIGHT_LEG); - private void TakeStepRight() => TakeStep(ref Step_R_timer, RIGHT_FOOT, ref StepRight, ref StepLeft, UPPER_RIGHT_LEG, - LOWER_RIGHT_LEG, UPPER_LEFT_LEG); + private void TakeStepRight() => TakeStep(ref Step_R_timer, RagdollParts.RIGHT_FOOT, ref StepRight, ref StepLeft, RagdollParts.UPPER_RIGHT_LEG, + RagdollParts.LOWER_RIGHT_LEG, RagdollParts.UPPER_LEFT_LEG); private void TakeStep(ref float stepTimer, string footLabel, @@ -785,10 +771,10 @@ private void Walk( } } - private void WalkBackwards() => Walk(LEFT_FOOT, RIGHT_FOOT, ref StepLeft, ref StepRight, ref Alert_Leg_Left, + private void WalkBackwards() => Walk(RagdollParts.LEFT_FOOT, RagdollParts.RIGHT_FOOT, ref StepLeft, ref StepRight, ref Alert_Leg_Left, ref Alert_Leg_Right); - private void WalkForwards() => Walk(RIGHT_FOOT, LEFT_FOOT, ref StepRight, ref StepLeft, ref Alert_Leg_Right, + private void WalkForwards() => Walk(RagdollParts.RIGHT_FOOT, RagdollParts.LEFT_FOOT, ref StepRight, ref StepLeft, ref Alert_Leg_Right, ref Alert_Leg_Left); private void PerformStepPrediction() @@ -803,8 +789,8 @@ private void PerformStepPrediction() Alert_Leg_Left = false; } - if (centerOfMass.position.z < RagdollDict[RIGHT_FOOT].transform.position.z && - centerOfMass.position.z < RagdollDict[LEFT_FOOT].transform.position.z) + if (centerOfMass.position.z < RagdollDict[RagdollParts.RIGHT_FOOT].transform.position.z && + centerOfMass.position.z < RagdollDict[RagdollParts.LEFT_FOOT].transform.position.z) { WalkBackward = true; } @@ -816,8 +802,8 @@ private void PerformStepPrediction() } } - if (centerOfMass.position.z > RagdollDict[RIGHT_FOOT].transform.position.z && - centerOfMass.position.z > RagdollDict[LEFT_FOOT].transform.position.z) + if (centerOfMass.position.z > RagdollDict[RagdollParts.RIGHT_FOOT].transform.position.z && + centerOfMass.position.z > RagdollDict[RagdollParts.LEFT_FOOT].transform.position.z) { WalkForward = true; } diff --git a/Assets/Scripts/ActiveRagdoll/RagdollParts.cs b/Assets/Scripts/ActiveRagdoll/RagdollParts.cs new file mode 100644 index 0000000..b8c3ba1 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollParts.cs @@ -0,0 +1,19 @@ +namespace ActiveRagdoll +{ + public class RagdollParts + { + internal const string ROOT = "Root"; + internal const string BODY = "Body"; + internal const string HEAD = "Head"; + internal const string UPPER_RIGHT_ARM = "UpperRightArm"; + internal const string LOWER_RIGHT_ARM = "LowerRightArm"; + internal const string UPPER_LEFT_ARM = "UpperLeftArm"; + internal const string LOWER_LEFT_ARM = "LowerLeftArm"; + internal const string UPPER_RIGHT_LEG = "UpperRightLeg"; + internal const string LOWER_RIGHT_LEG = "LowerRightLeg"; + internal const string UPPER_LEFT_LEG = "UpperLeftLeg"; + internal const string LOWER_LEFT_LEG = "LowerLeftLeg"; + internal const string RIGHT_FOOT = "RightFoot"; + internal const string LEFT_FOOT = "LeftFoot"; + } +} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollParts.cs.meta b/Assets/Scripts/ActiveRagdoll/RagdollParts.cs.meta new file mode 100644 index 0000000..bd0b0f2 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollParts.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f9be3084a1244dc3929f837801bb735f +timeCreated: 1692566079 \ No newline at end of file From aa4d80e96af58cf79e6181f9d05aa28486613d27 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Sun, 20 Aug 2023 18:31:13 -0300 Subject: [PATCH 04/19] Add Joint Handler --- .../ActiveRagdoll/RagdollController.cs | 68 +++++++++---------- .../ActiveRagdoll/RagdollJointHandler.cs | 44 ++++++++++++ .../ActiveRagdoll/RagdollJointHandler.cs.meta | 3 + 3 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index 6f12658..be6b7ac 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -72,12 +72,6 @@ public class RagdollController : MonoBehaviour private Vector3 Direction; private Vector3 CenterOfMassPoint; - private JointDrive BalanceOn; - private JointDrive PoseOn; - private JointDrive CoreStiffness; - private JointDrive ReachStiffness; - private JointDrive DriveOff; - private Quaternion HeadTarget; private Quaternion BodyTarget; private Quaternion UpperRightArmTarget; @@ -88,6 +82,7 @@ public class RagdollController : MonoBehaviour private Quaternion LowerRightLegTarget; private Quaternion UpperLeftLegTarget; private Quaternion LowerLeftLegTarget; + [SerializeField] private RagdollJointHandler jointHandler; private static int groundLayer; private readonly WaitForSeconds punchDelayWaitTime = new(0.3f); @@ -96,7 +91,6 @@ void Awake() { groundLayer = LayerMask.NameToLayer("Ground"); inputHandler.Init(); - SetupJointDrives(); SetupOriginalPose(); SetupHandContacts(); } @@ -110,15 +104,6 @@ private void SetupHandContacts() } } - private void SetupJointDrives() - { - BalanceOn = JointDriveHelper.CreateJointDrive(balanceStrength); - PoseOn = JointDriveHelper.CreateJointDrive(limbStrength); - CoreStiffness = JointDriveHelper.CreateJointDrive(coreStrength); - ReachStiffness = JointDriveHelper.CreateJointDrive(armReachStiffness); - DriveOff = JointDriveHelper.CreateJointDrive(25); - } - private void SetupOriginalPose() { BodyTarget = GetJointTargetRotation(RagdollParts.ROOT); @@ -366,9 +351,10 @@ private void SetRagdollState(bool shouldRagdoll, ref JointDrive rootJointDrive, ResetPose = true; } - private void DeactivateRagdoll() => SetRagdollState(false, ref BalanceOn, ref PoseOn, true); + private void DeactivateRagdoll() => + SetRagdollState(false, ref jointHandler.BalanceOn, ref jointHandler.PoseOn, true); - public void ActivateRagdoll() => SetRagdollState(true, ref DriveOff, ref DriveOff, false); + public void ActivateRagdoll() => SetRagdollState(true, ref jointHandler.DriveOff, ref jointHandler.DriveOff, false); private void SetJointAngularDrives(string jointName, in JointDrive jointDrive) { @@ -453,13 +439,13 @@ private void MoveInOwnDirection() } private void StartWalkingForwardInOwnDirection() => - SetWalkMovingState(() => (!WalkForward && !moveAxisUsed), true, false, true, true, PoseOn); + SetWalkMovingState(() => (!WalkForward && !moveAxisUsed), true, false, true, true, jointHandler.PoseOn); private void StartWalkingBackward() => - SetWalkMovingState(() => !WalkBackward && !moveAxisUsed, false, true, true, true, PoseOn); + SetWalkMovingState(() => !WalkBackward && !moveAxisUsed, false, true, true, true, jointHandler.PoseOn); private void StopWalking() => SetWalkMovingState(() => WalkForward || WalkBackward && moveAxisUsed, false, false, - false, false, DriveOff); + false, false, jointHandler.DriveOff); private void SetWalkMovingState(Func activationCondition, bool walkForwardSetState, bool walkBackwardSetState, bool isMoveAxisUsed, bool isKeyCurrentlyDown, in JointDrive legsJointDrive) @@ -511,9 +497,9 @@ private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reac { if (!reachSideAxisUsed) { - SetJointAngularDrives(upperArmJoint, in ReachStiffness); - SetJointAngularDrives(lowerArmJoint, in ReachStiffness); - SetJointAngularDrives(RagdollParts.BODY, in CoreStiffness); + SetJointAngularDrives(upperArmJoint, in jointHandler.ReachStiffness); + SetJointAngularDrives(lowerArmJoint, in jointHandler.ReachStiffness); + SetJointAngularDrives(RagdollParts.BODY, in jointHandler.CoreStiffness); reachSideAxisUsed = true; } @@ -529,14 +515,14 @@ private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reac return; if (balanced) { - SetJointAngularDrives(upperArmJoint, in PoseOn); - SetJointAngularDrives(lowerArmJoint, in PoseOn); - SetJointAngularDrives(RagdollParts.BODY, in PoseOn); + SetJointAngularDrives(upperArmJoint, in jointHandler.PoseOn); + SetJointAngularDrives(lowerArmJoint, in jointHandler.PoseOn); + SetJointAngularDrives(RagdollParts.BODY, in jointHandler.PoseOn); } else { - SetJointAngularDrives(upperArmJoint, in DriveOff); - SetJointAngularDrives(lowerArmJoint, in DriveOff); + SetJointAngularDrives(upperArmJoint, in jointHandler.DriveOff); + SetJointAngularDrives(lowerArmJoint, in jointHandler.DriveOff); } ResetPose = true; @@ -592,7 +578,8 @@ private void HandlePunch( lowerArmJoint.targetRotation = new Quaternion(-0.2f * punchRotationMultiplier, 0, 0, 1); handRigidbody.AddForce(RagdollDict[RagdollParts.ROOT].transform.forward * punchForce, ForceMode.Impulse); - RagdollDict[RagdollParts.BODY].Rigidbody.AddForce(RagdollDict[RagdollParts.BODY].transform.forward * punchForce, ForceMode.Impulse); + RagdollDict[RagdollParts.BODY].Rigidbody + .AddForce(RagdollDict[RagdollParts.BODY].transform.forward * punchForce, ForceMode.Impulse); StartCoroutine(PunchDelayCoroutine(isRightPunch, upperArmJoint, lowerArmJoint, upperArmTargetMethod, lowerArmTargetMethod)); @@ -612,7 +599,8 @@ private IEnumerator PunchDelayCoroutine(bool isRightArm, ConfigurableJoint upper } private void HandleLeftPunch() => - HandlePunch(ref punchingLeft, inputHandler.PunchLeftValue, false, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, leftHand, + HandlePunch(ref punchingLeft, inputHandler.PunchLeftValue, false, RagdollParts.UPPER_LEFT_ARM, + RagdollParts.LOWER_LEFT_ARM, leftHand, () => UpperLeftArmTarget, () => LowerLeftArmTarget); private void HandleRightPunch() => HandlePunch(ref punchingRight, inputHandler.PunchRightValue, true, @@ -654,9 +642,11 @@ private void PerformWalking() } private void ResetStepLeft() => - ResetStep(RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, in UpperLeftLegTarget, in LowerLeftLegTarget, 7f, 18f); + ResetStep(RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, in UpperLeftLegTarget, + in LowerLeftLegTarget, 7f, 18f); - private void ResetStepRight() => ResetStep(RagdollParts.UPPER_RIGHT_LEG, RagdollParts.LOWER_RIGHT_LEG, in UpperRightLegTarget, + private void ResetStepRight() => ResetStep(RagdollParts.UPPER_RIGHT_LEG, RagdollParts.LOWER_RIGHT_LEG, + in UpperRightLegTarget, in LowerRightLegTarget, 8f, 17f); private void ResetStep(string upperLegLabel, @@ -678,10 +668,12 @@ private void ResetStep(string upperLegLabel, RagdollDict[RagdollParts.LEFT_FOOT].Rigidbody.AddForce(feetForce, ForceMode.Impulse); } - private void TakeStepLeft() => TakeStep(ref Step_L_timer, RagdollParts.LEFT_FOOT, ref StepLeft, ref StepRight, RagdollParts.UPPER_LEFT_LEG, + private void TakeStepLeft() => TakeStep(ref Step_L_timer, RagdollParts.LEFT_FOOT, ref StepLeft, ref StepRight, + RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, RagdollParts.UPPER_RIGHT_LEG); - private void TakeStepRight() => TakeStep(ref Step_R_timer, RagdollParts.RIGHT_FOOT, ref StepRight, ref StepLeft, RagdollParts.UPPER_RIGHT_LEG, + private void TakeStepRight() => TakeStep(ref Step_R_timer, RagdollParts.RIGHT_FOOT, ref StepRight, ref StepLeft, + RagdollParts.UPPER_RIGHT_LEG, RagdollParts.LOWER_RIGHT_LEG, RagdollParts.UPPER_LEFT_LEG); private void TakeStep(ref float stepTimer, @@ -771,10 +763,12 @@ private void Walk( } } - private void WalkBackwards() => Walk(RagdollParts.LEFT_FOOT, RagdollParts.RIGHT_FOOT, ref StepLeft, ref StepRight, ref Alert_Leg_Left, + private void WalkBackwards() => Walk(RagdollParts.LEFT_FOOT, RagdollParts.RIGHT_FOOT, ref StepLeft, ref StepRight, + ref Alert_Leg_Left, ref Alert_Leg_Right); - private void WalkForwards() => Walk(RagdollParts.RIGHT_FOOT, RagdollParts.LEFT_FOOT, ref StepRight, ref StepLeft, ref Alert_Leg_Right, + private void WalkForwards() => Walk(RagdollParts.RIGHT_FOOT, RagdollParts.LEFT_FOOT, ref StepRight, ref StepLeft, + ref Alert_Leg_Right, ref Alert_Leg_Left); private void PerformStepPrediction() diff --git a/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs b/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs new file mode 100644 index 0000000..9d2b2e9 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs @@ -0,0 +1,44 @@ +using UnityEngine; + +namespace ActiveRagdoll +{ + [System.Serializable] + public class RagdollJointHandler + { + [SerializeField] private float balanceStrength = 5000f; + [SerializeField] private float coreStrength = 1500f; + [SerializeField] private float limbStrength = 500f; + [SerializeField] private float armReachStiffness = 2000f; + + internal JointDrive BalanceOn; + internal JointDrive PoseOn; + internal JointDrive CoreStiffness; + internal JointDrive ReachStiffness; + internal JointDrive DriveOff; + + public RagdollJointHandler() + { + Init(); + } + + private void Init() + { + BalanceOn = CreateJointDrive(balanceStrength); + PoseOn = CreateJointDrive(limbStrength); + CoreStiffness = CreateJointDrive(coreStrength); + ReachStiffness = CreateJointDrive(armReachStiffness); + DriveOff = CreateJointDrive(25); + } + + private static JointDrive CreateJointDrive(float positionSpring) + { + var jointDrive = new JointDrive + { + positionSpring = positionSpring, + positionDamper = 0, + maximumForce = Mathf.Infinity + }; + return jointDrive; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs.meta b/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs.meta new file mode 100644 index 0000000..d715837 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8aa314aa9198450b986abf25dadcfd3f +timeCreated: 1692566224 \ No newline at end of file From ca16aeb8638e3b8961bae952aead512210e94e32 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Sun, 20 Aug 2023 20:47:24 -0300 Subject: [PATCH 05/19] Add tooltips to joint handler --- Assets/Scripts/ActiveRagdoll/RagdollController.cs | 4 ---- Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs | 12 +++++++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index be6b7ac..0aaa10f 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -21,16 +21,12 @@ public class RagdollController : MonoBehaviour [Header("Balance Properties")] public bool autoGetUpWhenPossible = true; public bool useStepPrediction = true; public float balanceHeight = 2.5f; - public float balanceStrength = 5000f; - public float coreStrength = 1500f; - public float limbStrength = 500f; public float StepDuration = 0.2f; public float StepHeight = 1.7f; public float FeetMountForce = 25f; [Header("Reach Properties")] public float reachSensitivity = 25f; - public float armReachStiffness = 2000f; [Header("Actions")] public bool canBeKnockoutByImpact = true; public float requiredForceToBeKO = 20f; diff --git a/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs b/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs index 9d2b2e9..bccccc7 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs @@ -5,9 +5,15 @@ namespace ActiveRagdoll [System.Serializable] public class RagdollJointHandler { - [SerializeField] private float balanceStrength = 5000f; - [SerializeField] private float coreStrength = 1500f; - [SerializeField] private float limbStrength = 500f; + [SerializeField, Tooltip("The balancing strength to be used by this ragdoll")] + private float balanceStrength = 5000f; + + [SerializeField, Tooltip("The force to be applied in order to stiffen the core of the ragdoll")] + private float coreStrength = 1500f; + + [SerializeField, Tooltip("Mati llena este dato porque no se que es xd")] + private float limbStrength = 500f; + [SerializeField] private float armReachStiffness = 2000f; internal JointDrive BalanceOn; From 23452ca9697b658c5d724873a5f3390ce3111318 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Sun, 20 Aug 2023 21:04:14 -0300 Subject: [PATCH 06/19] Implement ragdoll default target state --- .../ActiveRagdoll/RagdollController.cs | 62 +++++-------------- .../RagdollDefaultTargetState.cs | 48 ++++++++++++++ .../RagdollDefaultTargetState.cs.meta | 3 + 3 files changed, 67 insertions(+), 46 deletions(-) create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index 0aaa10f..3d187cc 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -67,19 +67,8 @@ public class RagdollController : MonoBehaviour [SerializeField] private Camera cam; private Vector3 Direction; private Vector3 CenterOfMassPoint; - - private Quaternion HeadTarget; - private Quaternion BodyTarget; - private Quaternion UpperRightArmTarget; - private Quaternion LowerRightArmTarget; - private Quaternion UpperLeftArmTarget; - private Quaternion LowerLeftArmTarget; - private Quaternion UpperRightLegTarget; - private Quaternion LowerRightLegTarget; - private Quaternion UpperLeftLegTarget; - private Quaternion LowerLeftLegTarget; [SerializeField] private RagdollJointHandler jointHandler; - + private RagdollDefaultTargetState DefaultTargetState; private static int groundLayer; private readonly WaitForSeconds punchDelayWaitTime = new(0.3f); @@ -87,7 +76,7 @@ void Awake() { groundLayer = LayerMask.NameToLayer("Ground"); inputHandler.Init(); - SetupOriginalPose(); + DefaultTargetState = new RagdollDefaultTargetState(RagdollDict); SetupHandContacts(); } @@ -100,26 +89,6 @@ private void SetupHandContacts() } } - private void SetupOriginalPose() - { - BodyTarget = GetJointTargetRotation(RagdollParts.ROOT); - HeadTarget = GetJointTargetRotation(RagdollParts.HEAD); - UpperRightArmTarget = GetJointTargetRotation(RagdollParts.UPPER_RIGHT_ARM); - LowerRightArmTarget = GetJointTargetRotation(RagdollParts.LOWER_RIGHT_ARM); - UpperLeftArmTarget = GetJointTargetRotation(RagdollParts.UPPER_LEFT_ARM); - LowerLeftArmTarget = GetJointTargetRotation(RagdollParts.LOWER_LEFT_ARM); - UpperRightLegTarget = GetJointTargetRotation(RagdollParts.UPPER_RIGHT_LEG); - LowerRightLegTarget = GetJointTargetRotation(RagdollParts.LOWER_RIGHT_LEG); - UpperLeftLegTarget = GetJointTargetRotation(RagdollParts.UPPER_LEFT_LEG); - LowerLeftLegTarget = GetJointTargetRotation(RagdollParts.LOWER_LEFT_LEG); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private Quaternion GetJointTargetRotation(string jointName) - { - return RagdollDict[jointName].Joint.targetRotation; - } - private void Update() { if (!inAir) @@ -197,11 +166,11 @@ private void ResetPlayerPose() { if (ResetPose && !jumping) { - RagdollDict[RagdollParts.BODY].Joint.targetRotation = BodyTarget; - RagdollDict[RagdollParts.UPPER_RIGHT_ARM].Joint.targetRotation = UpperRightArmTarget; - RagdollDict[RagdollParts.LOWER_RIGHT_ARM].Joint.targetRotation = LowerRightArmTarget; - RagdollDict[RagdollParts.UPPER_LEFT_ARM].Joint.targetRotation = UpperLeftArmTarget; - RagdollDict[RagdollParts.LOWER_LEFT_ARM].Joint.targetRotation = LowerLeftArmTarget; + RagdollDict[RagdollParts.BODY].Joint.targetRotation = DefaultTargetState.BodyTarget; + RagdollDict[RagdollParts.UPPER_RIGHT_ARM].Joint.targetRotation = DefaultTargetState.UpperRightArmTarget; + RagdollDict[RagdollParts.LOWER_RIGHT_ARM].Joint.targetRotation = DefaultTargetState.LowerRightArmTarget; + RagdollDict[RagdollParts.UPPER_LEFT_ARM].Joint.targetRotation = DefaultTargetState.UpperLeftArmTarget; + RagdollDict[RagdollParts.LOWER_LEFT_ARM].Joint.targetRotation = DefaultTargetState.LowerLeftArmTarget; MouseYAxisArms = 0; ResetPose = false; @@ -597,11 +566,12 @@ private IEnumerator PunchDelayCoroutine(bool isRightArm, ConfigurableJoint upper private void HandleLeftPunch() => HandlePunch(ref punchingLeft, inputHandler.PunchLeftValue, false, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, leftHand, - () => UpperLeftArmTarget, () => LowerLeftArmTarget); + () => DefaultTargetState.UpperLeftArmTarget, () => DefaultTargetState.LowerLeftArmTarget); private void HandleRightPunch() => HandlePunch(ref punchingRight, inputHandler.PunchRightValue, true, RagdollParts.UPPER_RIGHT_ARM, - RagdollParts.LOWER_RIGHT_ARM, rightHand, () => UpperRightArmTarget, () => LowerLeftArmTarget); + RagdollParts.LOWER_RIGHT_ARM, rightHand, () => DefaultTargetState.UpperRightArmTarget, + () => DefaultTargetState.LowerLeftArmTarget); private void PerformWalking() { @@ -638,17 +608,17 @@ private void PerformWalking() } private void ResetStepLeft() => - ResetStep(RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, in UpperLeftLegTarget, - in LowerLeftLegTarget, 7f, 18f); + ResetStep(RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, DefaultTargetState.UpperLeftLegTarget, + DefaultTargetState.LowerLeftLegTarget, 7f, 18f); private void ResetStepRight() => ResetStep(RagdollParts.UPPER_RIGHT_LEG, RagdollParts.LOWER_RIGHT_LEG, - in UpperRightLegTarget, - in LowerRightLegTarget, 8f, 17f); + DefaultTargetState.UpperRightLegTarget, + DefaultTargetState.LowerRightLegTarget, 8f, 17f); private void ResetStep(string upperLegLabel, string lowerLegLabel, - in Quaternion upperLegTarget, - in Quaternion lowerLegTarget, + Quaternion upperLegTarget, + Quaternion lowerLegTarget, float upperLegLerpMultiplier, float lowerLegLerpMultiplier) { diff --git a/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs b/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs new file mode 100644 index 0000000..798e4f3 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs @@ -0,0 +1,48 @@ +using System.Runtime.CompilerServices; +using UnityEngine; +using UnityEngine.Assertions; + +namespace ActiveRagdoll +{ + public class RagdollDefaultTargetState + { + public Quaternion HeadTarget { get; private set; } + public Quaternion BodyTarget { get; private set; } + public Quaternion UpperRightArmTarget { get; private set; } + public Quaternion LowerRightArmTarget { get; private set; } + public Quaternion UpperLeftArmTarget { get; private set; } + public Quaternion LowerLeftArmTarget { get; private set; } + public Quaternion UpperRightLegTarget { get; private set; } + public Quaternion LowerRightLegTarget { get; private set; } + public Quaternion UpperLeftLegTarget { get; private set; } + public Quaternion LowerLeftLegTarget { get; private set; } + private UDictionary ragdollJoints; + public RagdollDefaultTargetState(UDictionary ragdollJoints) + { + Assert.IsNotNull(ragdollJoints); + this.ragdollJoints = ragdollJoints; + SetupOriginalPose(); + } + + private void SetupOriginalPose() + { + BodyTarget = GetJointTargetRotation(RagdollParts.ROOT); + HeadTarget = GetJointTargetRotation(RagdollParts.HEAD); + UpperRightArmTarget = GetJointTargetRotation(RagdollParts.UPPER_RIGHT_ARM); + LowerRightArmTarget = GetJointTargetRotation(RagdollParts.LOWER_RIGHT_ARM); + UpperLeftArmTarget = GetJointTargetRotation(RagdollParts.UPPER_LEFT_ARM); + LowerLeftArmTarget = GetJointTargetRotation(RagdollParts.LOWER_LEFT_ARM); + UpperRightLegTarget = GetJointTargetRotation(RagdollParts.UPPER_RIGHT_LEG); + LowerRightLegTarget = GetJointTargetRotation(RagdollParts.LOWER_RIGHT_LEG); + UpperLeftLegTarget = GetJointTargetRotation(RagdollParts.UPPER_LEFT_LEG); + LowerLeftLegTarget = GetJointTargetRotation(RagdollParts.LOWER_LEFT_LEG); + ragdollJoints = null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Quaternion GetJointTargetRotation(string jointName) + { + return ragdollJoints[jointName].Joint.targetRotation; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs.meta b/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs.meta new file mode 100644 index 0000000..86ebbea --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 656b1f12a86245589df191a19997d407 +timeCreated: 1692575652 \ No newline at end of file From de2c18fcf22eadbcc83735337a61fff67c1e3190 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Sun, 20 Aug 2023 21:04:25 -0300 Subject: [PATCH 07/19] Optimize feet ground layer check --- .../ActiveRagdoll/RagdollFeetContact.cs | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/ActiveRagdoll/RagdollFeetContact.cs b/Assets/Scripts/ActiveRagdoll/RagdollFeetContact.cs index d96688c..17b64bf 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollFeetContact.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollFeetContact.cs @@ -4,15 +4,29 @@ public class RagdollFeetContact : MonoBehaviour { [SerializeField] private RagdollController RagdollPlayer; private const string GROUND = "Ground"; - - void OnCollisionEnter(Collision col) + private int groundLayer = -1; + + private int GroundLayer + { + get + { + if (groundLayer == -1) + { + groundLayer = LayerMask.NameToLayer(GROUND); + } + + return groundLayer; + } + } + + private void OnCollisionEnter(Collision col) { - if(!RagdollPlayer.isJumping && RagdollPlayer.inAir) + if (!RagdollPlayer.isJumping && RagdollPlayer.inAir) { - if(col.gameObject.layer == LayerMask.NameToLayer(GROUND)) + if (col.gameObject.layer == GroundLayer) { RagdollPlayer.PlayerLanded(); } } } -} +} \ No newline at end of file From 5669001bee13c123c84ab97895aa636d61f80567 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 18:08:09 -0300 Subject: [PATCH 08/19] Optimize Joint usage in hands --- .../ActiveRagdoll/RagdollHandContact.cs | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs b/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs index a057540..8df9c3e 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs @@ -4,36 +4,37 @@ public class RagdollHandContact : MonoBehaviour { public RagdollController ragdollController; public bool Left; - public bool hasJoint; + private bool HasJoint => joint != null; private const string CAN_BE_GRABBED = "CanBeGrabbed"; private IInputListener inputListener; + private FixedJoint joint; + public void Init(IInputListener newInputListener) { inputListener = newInputListener; } + private void Update() { + //TODO: Add grabValue change event HandleJointRelease(Left ? inputListener.GrabLeftValue : inputListener.GrabRightValue); } private void HandleJointRelease(float reachAxisValue) { - if (hasJoint && reachAxisValue == 0) - { - DestroyJoint(); - } + if (!HasJoint) + return; - if (hasJoint && gameObject.GetComponent() == null) + if (reachAxisValue == 0) { - hasJoint = false; + DestroyJoint(); } } private void DestroyJoint() { - gameObject.GetComponent().breakForce = 0; - hasJoint = false; + joint.breakForce = 0; } @@ -50,7 +51,7 @@ private void OnCollisionEnter(Collision col) private bool CanGrab(Collision col) { - return col.gameObject.CompareTag(CAN_BE_GRABBED) && !hasJoint; + return col.gameObject.CompareTag(CAN_BE_GRABBED) && !HasJoint; } private bool CanPerformGrabAction() @@ -67,9 +68,9 @@ private bool CanPerformGrabAction() private void PerformGrabAction(Rigidbody connectedBody) { - hasJoint = true; - gameObject.AddComponent(); - gameObject.GetComponent().breakForce = Mathf.Infinity; - gameObject.GetComponent().connectedBody = connectedBody; + // hasJoint = true; + joint = gameObject.AddComponent(); + joint.breakForce = Mathf.Infinity; + joint.connectedBody = connectedBody; } } \ No newline at end of file From cd0f7b4b51e090abccce85c312a8ce65bf39edaf Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 18:59:41 -0300 Subject: [PATCH 09/19] Add locomotion controller --- .../ActiveRagdoll/RagdollController.cs | 411 ++++++++---------- .../RagdollDefaultTargetState.cs | 35 +- .../ActiveRagdoll/RagdollJointHandler.cs | 74 ++++ .../RagdollLocomotionController.cs | 91 ++++ .../RagdollLocomotionController.cs.meta | 3 + 5 files changed, 351 insertions(+), 263 deletions(-) create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index 3d187cc..ead3b96 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -7,8 +7,6 @@ public class RagdollController : MonoBehaviour { - [SerializeField] private UDictionary RagdollDict = new UDictionary(); - [SerializeField] private Rigidbody rightHand; [SerializeField] private Rigidbody leftHand; [SerializeField] private Transform centerOfMass; @@ -34,30 +32,14 @@ public class RagdollController : MonoBehaviour public float punchForce = 15f; //Hidden variables - private float timer; - private float Step_R_timer; - private float Step_L_timer; + private float jumpingResetTimer; private float MouseYAxisArms; private float MouseXAxisArms; private float MouseYAxisBody; - private bool WalkForward; - private bool WalkBackward; - private bool StepRight; - private bool StepLeft; - private bool Alert_Leg_Right; - private bool Alert_Leg_Left; - private bool balanced = true; - private bool GettingUp; - private bool ResetPose; - private bool isRagdoll; - private bool isKeyDown; - private bool moveAxisUsed; - private bool jumpAxisUsed; - private bool reachLeftAxisUsed; - private bool reachRightAxisUsed; private readonly RagdollInputHandler inputHandler = new(); + private RagdollLocomotionController locomotionController; [HideInInspector] public bool jumping; [HideInInspector] public bool isJumping; [HideInInspector] public bool inAir; @@ -66,9 +48,9 @@ public class RagdollController : MonoBehaviour [SerializeField] private Camera cam; private Vector3 Direction; - private Vector3 CenterOfMassPoint; + private Vector3 CenterOfMassPoint; //TODO: Check usage [SerializeField] private RagdollJointHandler jointHandler; - private RagdollDefaultTargetState DefaultTargetState; + private RagdollDefaultTargetState defaultTargetState; private static int groundLayer; private readonly WaitForSeconds punchDelayWaitTime = new(0.3f); @@ -76,13 +58,14 @@ void Awake() { groundLayer = LayerMask.NameToLayer("Ground"); inputHandler.Init(); - DefaultTargetState = new RagdollDefaultTargetState(RagdollDict); + locomotionController = new RagdollLocomotionController(jointHandler); + defaultTargetState = new RagdollDefaultTargetState(jointHandler); SetupHandContacts(); } private void SetupHandContacts() { - RagdollHandContact[] handContacts = GetComponentsInChildren(); + var handContacts = GetComponentsInChildren(); foreach (var handContact in handContacts) { handContact.Init(inputHandler); @@ -103,7 +86,7 @@ private void Update() PlayerReach(); - if (balanced && useStepPrediction) + if (locomotionController.balanced && useStepPrediction) { PerformStepPrediction(); } @@ -127,7 +110,12 @@ private void FixedUpdate() private void PerformPlayerRotation() { - ConfigurableJoint rootJoint = RagdollDict[RagdollParts.ROOT].Joint; + if (!jointHandler.TryGetJointWithID(RagdollParts.ROOT, out var joint)) + { + return; + } + + ConfigurableJoint rootJoint = joint.Joint; if (forwardIsCameraDirection) { @@ -164,16 +152,20 @@ private void PerformPlayerRotation() private void ResetPlayerPose() { - if (ResetPose && !jumping) + if (locomotionController.resetPose && !jumping) { - RagdollDict[RagdollParts.BODY].Joint.targetRotation = DefaultTargetState.BodyTarget; - RagdollDict[RagdollParts.UPPER_RIGHT_ARM].Joint.targetRotation = DefaultTargetState.UpperRightArmTarget; - RagdollDict[RagdollParts.LOWER_RIGHT_ARM].Joint.targetRotation = DefaultTargetState.LowerRightArmTarget; - RagdollDict[RagdollParts.UPPER_LEFT_ARM].Joint.targetRotation = DefaultTargetState.UpperLeftArmTarget; - RagdollDict[RagdollParts.LOWER_LEFT_ARM].Joint.targetRotation = DefaultTargetState.LowerLeftArmTarget; + jointHandler.GetConfigurableJointWithID(RagdollParts.BODY).targetRotation = defaultTargetState.BodyTarget; + jointHandler.GetConfigurableJointWithID(RagdollParts.UPPER_RIGHT_ARM).targetRotation = + defaultTargetState.UpperRightArmTarget; + jointHandler.GetConfigurableJointWithID(RagdollParts.LOWER_RIGHT_ARM).targetRotation = + defaultTargetState.LowerRightArmTarget; + jointHandler.GetConfigurableJointWithID(RagdollParts.UPPER_LEFT_ARM).targetRotation = + defaultTargetState.UpperLeftArmTarget; + jointHandler.GetConfigurableJointWithID(RagdollParts.LOWER_LEFT_ARM).targetRotation = + defaultTargetState.LowerLeftArmTarget; MouseYAxisArms = 0; - ResetPose = false; + locomotionController.resetPose = false; } } @@ -182,7 +174,7 @@ public void PlayerLanded() if (CanResetPoseAfterLanding()) { inAir = false; - ResetPose = true; + locomotionController.resetPose = true; } } @@ -195,25 +187,25 @@ private void PerformPlayerGetUpJumping() { if (inputHandler.JumpValue > 0) { - if (!jumpAxisUsed) + if (!locomotionController.jumpAxisUsed) { - if (balanced && !inAir) + if (locomotionController.balanced && !inAir) { jumping = true; } - else if (!balanced) + else if (!locomotionController.balanced) { DeactivateRagdoll(); } } - jumpAxisUsed = true; + locomotionController.jumpAxisUsed = true; } else { - jumpAxisUsed = false; + locomotionController.jumpAxisUsed = false; } @@ -221,17 +213,17 @@ private void PerformPlayerGetUpJumping() { isJumping = true; - Rigidbody rootRigidbody = RagdollDict[RagdollParts.ROOT].Rigidbody; + Rigidbody rootRigidbody = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).Rigidbody; rootRigidbody.velocity = rootRigidbody.velocity.ModifyY(rootRigidbody.transform.up.y * jumpForce); } if (isJumping) { - timer += Time.fixedDeltaTime; + jumpingResetTimer += Time.fixedDeltaTime; - if (timer > 0.2f) + if (jumpingResetTimer > 0.2f) { - timer = 0.0f; + jumpingResetTimer = 0.0f; jumping = false; isJumping = false; inAir = true; @@ -241,28 +233,28 @@ private void PerformPlayerGetUpJumping() private void GroundCheck() { - Transform rootTransform = RagdollDict[RagdollParts.ROOT].transform; - Ray ray = new Ray(rootTransform.position, -rootTransform.up); + Transform rootTransform = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).transform; + Ray ray = new Ray(rootTransform.position, Vector3.down); bool isHittingGround = Physics.Raycast(ray, out _, balanceHeight, 1 << groundLayer); if (!isHittingGround) { - if (balanced) + if (locomotionController.balanced) { - balanced = false; + locomotionController.balanced = false; } } else if (ShouldSetBalanced()) { - balanced = true; + locomotionController.balanced = true; } - bool needsStateChange = (balanced == isRagdoll); + bool needsStateChange = (locomotionController.balanced == locomotionController.isRagdoll); if (!needsStateChange) return; - if (balanced) + if (locomotionController.balanced) { DeactivateRagdoll(); } @@ -276,56 +268,18 @@ private bool ShouldSetBalanced() { return !inAir && !isJumping && - !reachRightAxisUsed && - !reachLeftAxisUsed && - !balanced && - RagdollDict[RagdollParts.ROOT].Rigidbody.velocity.magnitude < 1f && + !locomotionController.reachRightAxisUsed && + !locomotionController.reachLeftAxisUsed && + !locomotionController.balanced && + jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).Rigidbody.velocity.magnitude < 1f && autoGetUpWhenPossible; } - - private void SetRagdollState(bool shouldRagdoll, ref JointDrive rootJointDrive, ref JointDrive poseJointDrive, - bool shouldResetPose) - { - isRagdoll = shouldRagdoll; - balanced = !shouldRagdoll; - - SetJointAngularDrives(RagdollParts.ROOT, in rootJointDrive); - SetJointAngularDrives(RagdollParts.HEAD, in poseJointDrive); - - if (!reachRightAxisUsed) - { - SetJointAngularDrives(RagdollParts.UPPER_RIGHT_ARM, in poseJointDrive); - SetJointAngularDrives(RagdollParts.LOWER_RIGHT_ARM, in poseJointDrive); - } - - if (!reachLeftAxisUsed) - { - SetJointAngularDrives(RagdollParts.UPPER_LEFT_ARM, in poseJointDrive); - SetJointAngularDrives(RagdollParts.LOWER_LEFT_ARM, in poseJointDrive); - } - - SetJointAngularDrives(RagdollParts.UPPER_RIGHT_LEG, in poseJointDrive); - SetJointAngularDrives(RagdollParts.LOWER_RIGHT_LEG, in poseJointDrive); - SetJointAngularDrives(RagdollParts.UPPER_LEFT_LEG, in poseJointDrive); - SetJointAngularDrives(RagdollParts.LOWER_LEFT_LEG, in poseJointDrive); - SetJointAngularDrives(RagdollParts.RIGHT_FOOT, in poseJointDrive); - SetJointAngularDrives(RagdollParts.LEFT_FOOT, in poseJointDrive); - - if (shouldResetPose) - ResetPose = true; - } - private void DeactivateRagdoll() => - SetRagdollState(false, ref jointHandler.BalanceOn, ref jointHandler.PoseOn, true); + locomotionController.SetRagdollState(false, ref jointHandler.BalanceOn, ref jointHandler.PoseOn, true); - public void ActivateRagdoll() => SetRagdollState(true, ref jointHandler.DriveOff, ref jointHandler.DriveOff, false); - - private void SetJointAngularDrives(string jointName, in JointDrive jointDrive) - { - RagdollDict[jointName].Joint.angularXDrive = jointDrive; - RagdollDict[jointName].Joint.angularYZDrive = jointDrive; - } + public void ActivateRagdoll() => + locomotionController.SetRagdollState(true, ref jointHandler.DriveOff, ref jointHandler.DriveOff, false); private void PlayerMovement() { @@ -341,15 +295,15 @@ private void PlayerMovement() private void MoveInCameraDirection() { - Direction = RagdollDict[RagdollParts.ROOT].transform.rotation * + Direction = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).transform.rotation * new Vector3(inputHandler.MovementAxis.x, 0.0f, inputHandler.MovementAxis.y); Direction.y = 0f; - Rigidbody rootRigidbody = RagdollDict[RagdollParts.ROOT].Rigidbody; + Rigidbody rootRigidbody = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).Rigidbody; var velocity = rootRigidbody.velocity; rootRigidbody.velocity = Vector3.Lerp(velocity, (Direction * moveSpeed) + new Vector3(0, velocity.y, 0), 0.8f); - if (inputHandler.MovementAxis.x != 0 || inputHandler.MovementAxis.y != 0 && balanced) + if (inputHandler.MovementAxis.x != 0 || inputHandler.MovementAxis.y != 0 && locomotionController.balanced) { StartWalkingForward(); } @@ -361,21 +315,21 @@ private void MoveInCameraDirection() private void StartWalkingForward() { - if (!WalkForward && !moveAxisUsed) + if (!locomotionController.walkForward && !locomotionController.moveAxisUsed) { - WalkForward = true; - moveAxisUsed = true; - isKeyDown = true; + locomotionController.walkForward = true; + locomotionController.moveAxisUsed = true; + locomotionController.isKeyDown = true; } } private void StopWalkingForward() { - if (WalkForward && moveAxisUsed) + if (locomotionController.walkForward && locomotionController.moveAxisUsed) { - WalkForward = false; - moveAxisUsed = false; - isKeyDown = false; + locomotionController.walkForward = false; + locomotionController.moveAxisUsed = false; + locomotionController.isKeyDown = false; } } @@ -383,7 +337,7 @@ private void MoveInOwnDirection() { if (inputHandler.MovementAxis.y != 0) { - var rootRigidbody = RagdollDict[RagdollParts.ROOT].Rigidbody; + var rootRigidbody = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).Rigidbody; var v3 = rootRigidbody.transform.forward * (inputHandler.MovementAxis.y * moveSpeed); v3.y = rootRigidbody.velocity.y; rootRigidbody.velocity = v3; @@ -404,12 +358,18 @@ private void MoveInOwnDirection() } private void StartWalkingForwardInOwnDirection() => - SetWalkMovingState(() => (!WalkForward && !moveAxisUsed), true, false, true, true, jointHandler.PoseOn); + SetWalkMovingState(() => (!locomotionController.walkForward && !locomotionController.moveAxisUsed), true, false, + true, true, + jointHandler.PoseOn); private void StartWalkingBackward() => - SetWalkMovingState(() => !WalkBackward && !moveAxisUsed, false, true, true, true, jointHandler.PoseOn); + SetWalkMovingState(() => !locomotionController.walkBackward && !locomotionController.moveAxisUsed, false, true, + true, true, + jointHandler.PoseOn); - private void StopWalking() => SetWalkMovingState(() => WalkForward || WalkBackward && moveAxisUsed, false, false, + private void StopWalking() => SetWalkMovingState( + () => locomotionController.walkForward || + locomotionController.walkBackward && locomotionController.moveAxisUsed, false, false, false, false, jointHandler.DriveOff); private void SetWalkMovingState(Func activationCondition, bool walkForwardSetState, bool walkBackwardSetState, @@ -426,28 +386,29 @@ private void InternalChangeWalkState(bool walkForward, bool walkBackward, bool i bool isKeyCurrentlyDown, in JointDrive legsJointDrive) { - WalkForward = walkForward; - WalkBackward = walkBackward; - moveAxisUsed = isMoveAxisUsed; - isKeyDown = isKeyCurrentlyDown; - if (isRagdoll) + locomotionController.walkForward = walkForward; + locomotionController.walkBackward = walkBackward; + locomotionController.moveAxisUsed = isMoveAxisUsed; + locomotionController.isKeyDown = isKeyCurrentlyDown; + if (locomotionController.isRagdoll) SetJointAngularDrivesForLegs(in legsJointDrive); } private void SetJointAngularDrivesForLegs(in JointDrive jointDrive) { - SetJointAngularDrives(RagdollParts.UPPER_RIGHT_LEG, in jointDrive); - SetJointAngularDrives(RagdollParts.LOWER_RIGHT_LEG, in jointDrive); - SetJointAngularDrives(RagdollParts.UPPER_LEFT_LEG, in jointDrive); - SetJointAngularDrives(RagdollParts.LOWER_LEFT_LEG, in jointDrive); - SetJointAngularDrives(RagdollParts.RIGHT_FOOT, in jointDrive); - SetJointAngularDrives(RagdollParts.LEFT_FOOT, in jointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.UPPER_RIGHT_LEG, in jointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LOWER_RIGHT_LEG, in jointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.UPPER_LEFT_LEG, in jointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LOWER_LEFT_LEG, in jointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.RIGHT_FOOT, in jointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LEFT_FOOT, in jointDrive); } private void PlayerReach() { MouseYAxisBody = Mathf.Clamp(MouseYAxisBody += (inputHandler.AimAxis.y / reachSensitivity), -0.2f, 0.1f); - RagdollDict[RagdollParts.BODY].Joint.targetRotation = new Quaternion(MouseYAxisBody, 0, 0, 1); + jointHandler.GetConfigurableJointWithID(RagdollParts.BODY).targetRotation = + new Quaternion(MouseYAxisBody, 0, 0, 1); HandleLeftSideReach(); HandleRightSideReach(); @@ -462,15 +423,15 @@ private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reac { if (!reachSideAxisUsed) { - SetJointAngularDrives(upperArmJoint, in jointHandler.ReachStiffness); - SetJointAngularDrives(lowerArmJoint, in jointHandler.ReachStiffness); - SetJointAngularDrives(RagdollParts.BODY, in jointHandler.CoreStiffness); + jointHandler.SetJointAngularDrives(upperArmJoint, in jointHandler.ReachStiffness); + jointHandler.SetJointAngularDrives(lowerArmJoint, in jointHandler.ReachStiffness); + jointHandler.SetJointAngularDrives(RagdollParts.BODY, in jointHandler.CoreStiffness); reachSideAxisUsed = true; } int multiplier = isRightArm ? 1 : -1; MouseYAxisArms = Mathf.Clamp(MouseYAxisArms += (inputHandler.AimAxis.y / reachSensitivity), -1.2f, 1.2f); - RagdollDict[upperArmJoint].Joint.targetRotation = + jointHandler.GetConfigurableJointWithID(upperArmJoint).targetRotation = new Quaternion((0.58f + (MouseYAxisArms)) * multiplier, -0.88f - (MouseYAxisArms), 0.8f * multiplier, 1); } @@ -478,29 +439,29 @@ private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reac { if (!reachSideAxisUsed) return; - if (balanced) + if (locomotionController.balanced) { - SetJointAngularDrives(upperArmJoint, in jointHandler.PoseOn); - SetJointAngularDrives(lowerArmJoint, in jointHandler.PoseOn); - SetJointAngularDrives(RagdollParts.BODY, in jointHandler.PoseOn); + jointHandler.SetJointAngularDrives(upperArmJoint, in jointHandler.PoseOn); + jointHandler.SetJointAngularDrives(lowerArmJoint, in jointHandler.PoseOn); + jointHandler.SetJointAngularDrives(RagdollParts.BODY, in jointHandler.PoseOn); } else { - SetJointAngularDrives(upperArmJoint, in jointHandler.DriveOff); - SetJointAngularDrives(lowerArmJoint, in jointHandler.DriveOff); + jointHandler.SetJointAngularDrives(upperArmJoint, in jointHandler.DriveOff); + jointHandler.SetJointAngularDrives(lowerArmJoint, in jointHandler.DriveOff); } - ResetPose = true; + locomotionController.resetPose = true; reachSideAxisUsed = false; } } private void HandleRightSideReach() => HandlePlayerReach(punchingRight, inputHandler.GrabRightValue, - ref reachRightAxisUsed, RagdollParts.UPPER_RIGHT_ARM, RagdollParts.LOWER_RIGHT_ARM, true); + ref locomotionController.reachRightAxisUsed, RagdollParts.UPPER_RIGHT_ARM, RagdollParts.LOWER_RIGHT_ARM, true); private void HandleLeftSideReach() => HandlePlayerReach(punchingLeft, inputHandler.GrabLeftValue, - ref reachLeftAxisUsed, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, false); + ref locomotionController.reachLeftAxisUsed, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, false); private void PerformPlayerPunch() { @@ -521,16 +482,16 @@ private void HandlePunch( if (punchingArmState == punchingArmValue) return; - ConfigurableJoint bodyJoint = RagdollDict[RagdollParts.BODY].Joint; - ConfigurableJoint upperArmJoint = RagdollDict[upperArmLabel].Joint; - ConfigurableJoint lowerArmJoint = RagdollDict[lowerArmLabel].Joint; + RagdollJoint bodyJoint = jointHandler.GetRagdollJointWithID(RagdollParts.BODY); + ConfigurableJoint upperArmJoint = jointHandler.GetConfigurableJointWithID(upperArmLabel); + ConfigurableJoint lowerArmJoint = jointHandler.GetConfigurableJointWithID(lowerArmLabel); punchingArmState = punchingArmValue; int punchRotationMultiplier = isRightPunch ? -1 : 1; if (punchingArmValue) { - bodyJoint.targetRotation = new Quaternion(-0.15f, 0.15f * punchRotationMultiplier, 0, 1); + bodyJoint.Joint.targetRotation = new Quaternion(-0.15f, 0.15f * punchRotationMultiplier, 0, 1); upperArmJoint.targetRotation = new Quaternion(0.62f * punchRotationMultiplier, -0.51f, 0.02f, 1); lowerArmJoint.targetRotation = new Quaternion(-1.31f * punchRotationMultiplier, 0.5f, 0.5f * punchRotationMultiplier, 1); @@ -538,13 +499,13 @@ private void HandlePunch( else { - bodyJoint.targetRotation = new Quaternion(-0.15f, -0.15f * punchRotationMultiplier, 0, 1); + bodyJoint.Joint.targetRotation = new Quaternion(-0.15f, -0.15f * punchRotationMultiplier, 0, 1); upperArmJoint.targetRotation = new Quaternion(-0.74f * punchRotationMultiplier, 0.04f, 0f, 1); lowerArmJoint.targetRotation = new Quaternion(-0.2f * punchRotationMultiplier, 0, 0, 1); - handRigidbody.AddForce(RagdollDict[RagdollParts.ROOT].transform.forward * punchForce, ForceMode.Impulse); - RagdollDict[RagdollParts.BODY].Rigidbody - .AddForce(RagdollDict[RagdollParts.BODY].transform.forward * punchForce, ForceMode.Impulse); + handRigidbody.AddForce(jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).transform.forward * punchForce, + ForceMode.Impulse); + bodyJoint.Rigidbody.AddForce(bodyJoint.transform.forward * punchForce, ForceMode.Impulse); StartCoroutine(PunchDelayCoroutine(isRightPunch, upperArmJoint, lowerArmJoint, upperArmTargetMethod, lowerArmTargetMethod)); @@ -566,29 +527,29 @@ private IEnumerator PunchDelayCoroutine(bool isRightArm, ConfigurableJoint upper private void HandleLeftPunch() => HandlePunch(ref punchingLeft, inputHandler.PunchLeftValue, false, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, leftHand, - () => DefaultTargetState.UpperLeftArmTarget, () => DefaultTargetState.LowerLeftArmTarget); + () => defaultTargetState.UpperLeftArmTarget, () => defaultTargetState.LowerLeftArmTarget); private void HandleRightPunch() => HandlePunch(ref punchingRight, inputHandler.PunchRightValue, true, RagdollParts.UPPER_RIGHT_ARM, - RagdollParts.LOWER_RIGHT_ARM, rightHand, () => DefaultTargetState.UpperRightArmTarget, - () => DefaultTargetState.LowerLeftArmTarget); + RagdollParts.LOWER_RIGHT_ARM, rightHand, () => defaultTargetState.UpperRightArmTarget, + () => defaultTargetState.LowerLeftArmTarget); private void PerformWalking() { if (inAir) return; - if (WalkForward) + if (locomotionController.walkForward) { WalkForwards(); } - if (WalkBackward) + if (locomotionController.walkBackward) { WalkBackwards(); } - if (StepRight) + if (locomotionController.stepRight) { TakeStepRight(); } @@ -597,7 +558,7 @@ private void PerformWalking() ResetStepRight(); } - if (StepLeft) + if (locomotionController.stepLeft) { TakeStepLeft(); } @@ -608,12 +569,12 @@ private void PerformWalking() } private void ResetStepLeft() => - ResetStep(RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, DefaultTargetState.UpperLeftLegTarget, - DefaultTargetState.LowerLeftLegTarget, 7f, 18f); + ResetStep(RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, defaultTargetState.UpperLeftLegTarget, + defaultTargetState.LowerLeftLegTarget, 7f, 18f); private void ResetStepRight() => ResetStep(RagdollParts.UPPER_RIGHT_LEG, RagdollParts.LOWER_RIGHT_LEG, - DefaultTargetState.UpperRightLegTarget, - DefaultTargetState.LowerRightLegTarget, 8f, 17f); + defaultTargetState.UpperRightLegTarget, + defaultTargetState.LowerRightLegTarget, 8f, 17f); private void ResetStep(string upperLegLabel, string lowerLegLabel, @@ -622,23 +583,25 @@ private void ResetStep(string upperLegLabel, float upperLegLerpMultiplier, float lowerLegLerpMultiplier) { - RagdollDict[upperLegLabel].Joint.targetRotation = Quaternion.Lerp( - RagdollDict[upperLegLabel].Joint.targetRotation, upperLegTarget, + jointHandler.GetConfigurableJointWithID(upperLegLabel).targetRotation = Quaternion.Lerp( + jointHandler.GetJointTargetRotation(upperLegLabel), upperLegTarget, upperLegLerpMultiplier * Time.fixedDeltaTime); - RagdollDict[lowerLegLabel].Joint.targetRotation = Quaternion.Lerp( - RagdollDict[lowerLegLabel].Joint.targetRotation, lowerLegTarget, + jointHandler.GetConfigurableJointWithID(lowerLegLabel).targetRotation = Quaternion.Lerp( + jointHandler.GetJointTargetRotation(lowerLegLabel), lowerLegTarget, lowerLegLerpMultiplier * Time.fixedDeltaTime); Vector3 feetForce = -Vector3.up * (FeetMountForce * Time.deltaTime); - RagdollDict[RagdollParts.RIGHT_FOOT].Rigidbody.AddForce(feetForce, ForceMode.Impulse); - RagdollDict[RagdollParts.LEFT_FOOT].Rigidbody.AddForce(feetForce, ForceMode.Impulse); + jointHandler.GetRagdollJointWithID(RagdollParts.RIGHT_FOOT).Rigidbody.AddForce(feetForce, ForceMode.Impulse); + jointHandler.GetRagdollJointWithID(RagdollParts.LEFT_FOOT).Rigidbody.AddForce(feetForce, ForceMode.Impulse); } - private void TakeStepLeft() => TakeStep(ref Step_L_timer, RagdollParts.LEFT_FOOT, ref StepLeft, ref StepRight, + private void TakeStepLeft() => TakeStep(ref locomotionController.stepLTimer, RagdollParts.LEFT_FOOT, + ref locomotionController.stepLeft, ref locomotionController.stepRight, RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, RagdollParts.UPPER_RIGHT_LEG); - private void TakeStepRight() => TakeStep(ref Step_R_timer, RagdollParts.RIGHT_FOOT, ref StepRight, ref StepLeft, + private void TakeStepRight() => TakeStep(ref locomotionController.stepRTimer, RagdollParts.RIGHT_FOOT, + ref locomotionController.stepRight, ref locomotionController.stepLeft, RagdollParts.UPPER_RIGHT_LEG, RagdollParts.LOWER_RIGHT_LEG, RagdollParts.UPPER_LEFT_LEG); @@ -651,21 +614,21 @@ private void TakeStep(ref float stepTimer, string upperOppositeJointLabel) { stepTimer += Time.fixedDeltaTime; - RagdollDict[footLabel].Rigidbody + jointHandler.GetRagdollJointWithID(footLabel).Rigidbody .AddForce(-Vector3.up * (FeetMountForce * Time.deltaTime), ForceMode.Impulse); - var upperLegJoint = RagdollDict[upperJointLabel].Joint; + var upperLegJoint = jointHandler.GetConfigurableJointWithID(upperJointLabel); var upperLegJointTargetRotation = upperLegJoint.targetRotation; - var lowerLegJoint = RagdollDict[lowerJointLabel].Joint; + var lowerLegJoint = jointHandler.GetConfigurableJointWithID(lowerJointLabel); var lowerLegJointTargetRotation = lowerLegJoint.targetRotation; - var upperOppositeLegJoint = RagdollDict[upperOppositeJointLabel].Joint; + var upperOppositeLegJoint = jointHandler.GetConfigurableJointWithID(upperOppositeJointLabel); var upperOppositeLegJointTargetRotation = upperOppositeLegJoint.targetRotation; - bool isWalking = WalkForward || WalkBackward; + bool isWalking = locomotionController.walkForward || locomotionController.walkBackward; - if (WalkForward) + if (locomotionController.walkForward) { upperLegJointTargetRotation = upperLegJointTargetRotation.DisplaceX(0.09f * StepHeight); lowerLegJointTargetRotation = lowerLegJointTargetRotation.DisplaceX(-0.09f * StepHeight * 2); @@ -673,7 +636,7 @@ private void TakeStep(ref float stepTimer, upperOppositeLegJointTargetRotation.DisplaceX(-0.12f * StepHeight / 2); } - if (WalkBackward) + if (locomotionController.walkBackward) { //TODO: Is this necessary for something? It's multiplying by 0. upperLegJointTargetRotation = upperLegJointTargetRotation.DisplaceX(-0.00f * StepHeight); @@ -701,77 +664,55 @@ private void TakeStep(ref float stepTimer, } } - private void Walk( - string forwardFootLabel, - string backFootLabel, - ref bool forwardFootState, - ref bool backFootState, - ref bool forwardAlertLeg, - ref bool backAlertLeg) - { - bool forwardFootIsBehind = RagdollDict[forwardFootLabel].transform.position.z < - RagdollDict[backFootLabel].transform.position.z; - bool forwardFootIsAhead = RagdollDict[forwardFootLabel].transform.position.z > - RagdollDict[backFootLabel].transform.position.z; - - if (forwardFootIsBehind && !backFootState && !forwardAlertLeg) - { - forwardFootState = true; - forwardAlertLeg = true; - backAlertLeg = true; - } - - if (forwardFootIsAhead && !forwardFootState && !backAlertLeg) - { - backFootState = true; - backAlertLeg = true; - forwardAlertLeg = true; - } - } - - private void WalkBackwards() => Walk(RagdollParts.LEFT_FOOT, RagdollParts.RIGHT_FOOT, ref StepLeft, ref StepRight, - ref Alert_Leg_Left, - ref Alert_Leg_Right); + private void WalkBackwards() => locomotionController.Walk(RagdollParts.LEFT_FOOT, RagdollParts.RIGHT_FOOT, + ref locomotionController.stepLeft, ref locomotionController.stepRight, + ref locomotionController.alertLegLeft, + ref locomotionController.alertLegRight); - private void WalkForwards() => Walk(RagdollParts.RIGHT_FOOT, RagdollParts.LEFT_FOOT, ref StepRight, ref StepLeft, - ref Alert_Leg_Right, - ref Alert_Leg_Left); + private void WalkForwards() => locomotionController.Walk(RagdollParts.RIGHT_FOOT, RagdollParts.LEFT_FOOT, + ref locomotionController.stepRight, ref locomotionController.stepLeft, + ref locomotionController.alertLegRight, + ref locomotionController.alertLegLeft); private void PerformStepPrediction() { - if (!WalkForward && !WalkBackward) + if (!locomotionController.walkForward && !locomotionController.walkBackward) { - StepRight = false; - StepLeft = false; - Step_R_timer = 0; - Step_L_timer = 0; - Alert_Leg_Right = false; - Alert_Leg_Left = false; + locomotionController.stepRight = false; + locomotionController.stepLeft = false; + locomotionController.stepRTimer = 0; + locomotionController.stepLTimer = 0; + locomotionController.alertLegRight = false; + locomotionController.alertLegLeft = false; } - if (centerOfMass.position.z < RagdollDict[RagdollParts.RIGHT_FOOT].transform.position.z && - centerOfMass.position.z < RagdollDict[RagdollParts.LEFT_FOOT].transform.position.z) + float rightFootZPosition = + jointHandler.GetConfigurableJointWithID(RagdollParts.RIGHT_FOOT).transform.position.z; + float leftFootZPosition = + jointHandler.GetConfigurableJointWithID(RagdollParts.LEFT_FOOT).transform.position.z; + + //TODO: Refactor to reduce amount of code here + if (centerOfMass.position.z < rightFootZPosition && centerOfMass.position.z < leftFootZPosition) { - WalkBackward = true; + locomotionController.walkBackward = true; } else { - if (!isKeyDown) + if (!locomotionController.isKeyDown) { - WalkBackward = false; + locomotionController.walkBackward = false; } } - if (centerOfMass.position.z > RagdollDict[RagdollParts.RIGHT_FOOT].transform.position.z && - centerOfMass.position.z > RagdollDict[RagdollParts.LEFT_FOOT].transform.position.z) + if (centerOfMass.position.z > rightFootZPosition && centerOfMass.position.z > leftFootZPosition) { - WalkForward = true; + locomotionController.walkForward = true; } else { - if (!isKeyDown) + if (!locomotionController.isKeyDown) { - WalkForward = false; + locomotionController.walkForward = false; } } } @@ -779,31 +720,19 @@ private void PerformStepPrediction() [MethodImpl(MethodImplOptions.AggressiveInlining)] private void UpdateCenterOfMass() { - Vector3 massPositionDisplacement = Vector3.zero; - float totalMass = 0; - - foreach (var element in RagdollDict) - { - var joint = element.Value; - var mass = joint.Rigidbody.mass; - massPositionDisplacement += mass * joint.transform.position; - totalMass += mass; - } - - CenterOfMassPoint = (massPositionDisplacement / totalMass); - centerOfMass.position = CenterOfMassPoint; + jointHandler.GetCenterOfMass(out CenterOfMassPoint, centerOfMass); } private void ResetWalkCycle() { - if (!WalkForward && !WalkBackward) - { - StepRight = false; - StepLeft = false; - Step_R_timer = 0; - Step_L_timer = 0; - Alert_Leg_Right = false; - Alert_Leg_Left = false; + if (!locomotionController.walkForward && !locomotionController.walkBackward) + { + locomotionController.stepRight = false; + locomotionController.stepLeft = false; + locomotionController.stepRTimer = 0; + locomotionController.stepLTimer = 0; + locomotionController.alertLegRight = false; + locomotionController.alertLegLeft = false; } } } \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs b/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs index 798e4f3..5aba615 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs @@ -16,33 +16,24 @@ public class RagdollDefaultTargetState public Quaternion LowerRightLegTarget { get; private set; } public Quaternion UpperLeftLegTarget { get; private set; } public Quaternion LowerLeftLegTarget { get; private set; } - private UDictionary ragdollJoints; - public RagdollDefaultTargetState(UDictionary ragdollJoints) - { - Assert.IsNotNull(ragdollJoints); - this.ragdollJoints = ragdollJoints; - SetupOriginalPose(); - } - private void SetupOriginalPose() + public RagdollDefaultTargetState(RagdollJointHandler jointHandler) { - BodyTarget = GetJointTargetRotation(RagdollParts.ROOT); - HeadTarget = GetJointTargetRotation(RagdollParts.HEAD); - UpperRightArmTarget = GetJointTargetRotation(RagdollParts.UPPER_RIGHT_ARM); - LowerRightArmTarget = GetJointTargetRotation(RagdollParts.LOWER_RIGHT_ARM); - UpperLeftArmTarget = GetJointTargetRotation(RagdollParts.UPPER_LEFT_ARM); - LowerLeftArmTarget = GetJointTargetRotation(RagdollParts.LOWER_LEFT_ARM); - UpperRightLegTarget = GetJointTargetRotation(RagdollParts.UPPER_RIGHT_LEG); - LowerRightLegTarget = GetJointTargetRotation(RagdollParts.LOWER_RIGHT_LEG); - UpperLeftLegTarget = GetJointTargetRotation(RagdollParts.UPPER_LEFT_LEG); - LowerLeftLegTarget = GetJointTargetRotation(RagdollParts.LOWER_LEFT_LEG); - ragdollJoints = null; + SetupOriginalPose(jointHandler); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private Quaternion GetJointTargetRotation(string jointName) + private void SetupOriginalPose(RagdollJointHandler jointsHandler) { - return ragdollJoints[jointName].Joint.targetRotation; + BodyTarget = jointsHandler.GetJointTargetRotation(RagdollParts.ROOT); + HeadTarget = jointsHandler.GetJointTargetRotation(RagdollParts.HEAD); + UpperRightArmTarget = jointsHandler.GetJointTargetRotation(RagdollParts.UPPER_RIGHT_ARM); + LowerRightArmTarget = jointsHandler.GetJointTargetRotation(RagdollParts.LOWER_RIGHT_ARM); + UpperLeftArmTarget = jointsHandler.GetJointTargetRotation(RagdollParts.UPPER_LEFT_ARM); + LowerLeftArmTarget = jointsHandler.GetJointTargetRotation(RagdollParts.LOWER_LEFT_ARM); + UpperRightLegTarget = jointsHandler.GetJointTargetRotation(RagdollParts.UPPER_RIGHT_LEG); + LowerRightLegTarget = jointsHandler.GetJointTargetRotation(RagdollParts.LOWER_RIGHT_LEG); + UpperLeftLegTarget = jointsHandler.GetJointTargetRotation(RagdollParts.UPPER_LEFT_LEG); + LowerLeftLegTarget = jointsHandler.GetJointTargetRotation(RagdollParts.LOWER_LEFT_LEG); } } } \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs b/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs index bccccc7..a82a0ab 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs @@ -1,3 +1,4 @@ +using System.Runtime.CompilerServices; using UnityEngine; namespace ActiveRagdoll @@ -16,12 +17,15 @@ public class RagdollJointHandler [SerializeField] private float armReachStiffness = 2000f; + [SerializeField] private UDictionary joints = new(); + internal JointDrive BalanceOn; internal JointDrive PoseOn; internal JointDrive CoreStiffness; internal JointDrive ReachStiffness; internal JointDrive DriveOff; + public RagdollJointHandler() { Init(); @@ -36,6 +40,18 @@ private void Init() DriveOff = CreateJointDrive(25); } + internal void SetJointAngularDrives(string jointName, in JointDrive jointDrive) + { + joints[jointName].Joint.angularXDrive = jointDrive; + joints[jointName].Joint.angularYZDrive = jointDrive; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal Quaternion GetJointTargetRotation(string jointName) + { + return joints[jointName].Joint.targetRotation; + } + private static JointDrive CreateJointDrive(float positionSpring) { var jointDrive = new JointDrive @@ -46,5 +62,63 @@ private static JointDrive CreateJointDrive(float positionSpring) }; return jointDrive; } + + internal bool TryGetJointWithID(string jointID, out RagdollJoint joint) + { + bool gotJoint = joints.TryGetValue(jointID, out joint); +#if UNITY_EDITOR || DEBUG + if (!gotJoint) + Debug.LogWarning($"Joint with id {jointID} not found"); +#endif + return gotJoint; + } + + /// + /// Obtains a ragdoll joint with a specific ID. Only use if absolutely sure the joint exists + /// + /// + /// The specified > + internal RagdollJoint GetRagdollJointWithID(string jointID) + { + if (!joints.TryGetValue(jointID, out var joint)) + { +#if UNITY_EDITOR || DEBUG + Debug.LogWarning($"Ragdoll joint with id {jointID} not found"); +#endif + } + + return joint; + } + + + internal ConfigurableJoint GetConfigurableJointWithID(string jointID) + { + if (joints.TryGetValue(jointID, out var joint)) + { + return joint.Joint; + } + +#if UNITY_EDITOR || DEBUG + Debug.LogWarning($"Configurable joint for Ragdolljoint with id {jointID} not found"); +#endif + return default; + } + + public void GetCenterOfMass(out Vector3 CenterOfMassPoint, Transform centerOfMass) + { + Vector3 massPositionDisplacement = Vector3.zero; + float totalMass = 0; + + foreach (var element in joints) + { + var joint = element.Value; + var mass = joint.Rigidbody.mass; + massPositionDisplacement += mass * joint.transform.position; + totalMass += mass; + } + + CenterOfMassPoint = (massPositionDisplacement / totalMass); + centerOfMass.position = CenterOfMassPoint; + } } } \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs b/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs new file mode 100644 index 0000000..4063896 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs @@ -0,0 +1,91 @@ +using UnityEngine; + +namespace ActiveRagdoll +{ + public class RagdollLocomotionController + { + internal float stepRTimer; + internal float stepLTimer; + internal bool walkForward; + internal bool walkBackward; + internal bool stepRight; + internal bool stepLeft; + internal bool alertLegRight; + internal bool alertLegLeft; + internal bool balanced = true; + internal bool gettingUp; + internal bool resetPose; + internal bool isRagdoll; + internal bool isKeyDown; + internal bool moveAxisUsed; + internal bool jumpAxisUsed; + internal bool reachLeftAxisUsed; + internal bool reachRightAxisUsed; + + private RagdollJointHandler jointHandler; + + public RagdollLocomotionController(RagdollJointHandler jointsHandler) + { + jointHandler = jointsHandler; + } + + public void SetRagdollState(bool shouldRagdoll, ref JointDrive rootJointDrive, + ref JointDrive poseJointDrive, + bool shouldResetPose) + { + isRagdoll = shouldRagdoll; + balanced = !shouldRagdoll; + + jointHandler.SetJointAngularDrives(RagdollParts.ROOT, in rootJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.HEAD, in poseJointDrive); + + if (!reachRightAxisUsed) + { + jointHandler.SetJointAngularDrives(RagdollParts.UPPER_RIGHT_ARM, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LOWER_RIGHT_ARM, in poseJointDrive); + } + + if (!reachLeftAxisUsed) + { + jointHandler.SetJointAngularDrives(RagdollParts.UPPER_LEFT_ARM, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LOWER_LEFT_ARM, in poseJointDrive); + } + + jointHandler.SetJointAngularDrives(RagdollParts.UPPER_RIGHT_LEG, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LOWER_RIGHT_LEG, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.UPPER_LEFT_LEG, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LOWER_LEFT_LEG, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.RIGHT_FOOT, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LEFT_FOOT, in poseJointDrive); + + if (shouldResetPose) + resetPose = true; + } + + internal void Walk(string forwardFootLabel, string backFootLabel, ref bool forwardFootState, + ref bool backFootState, ref bool forwardAlertLeg, ref bool backAlertLeg) + { + float forwardFootTransformZ = + jointHandler.GetConfigurableJointWithID(forwardFootLabel).transform.position.z; + float backFootTransformZ = jointHandler.GetConfigurableJointWithID(backFootLabel).transform.position.z; + + bool forwardFootIsBehind = forwardFootTransformZ < backFootTransformZ; + bool forwardFootIsAhead = forwardFootTransformZ > backFootTransformZ; + + //TODO: Displace logic to not use alternating conditions + if (forwardFootIsBehind && !backFootState && !forwardAlertLeg) + { + forwardFootState = true; + forwardAlertLeg = true; + backAlertLeg = true; + } + + if (forwardFootIsAhead && !forwardFootState && !backAlertLeg) + { + backFootState = true; + backAlertLeg = true; + forwardAlertLeg = true; + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs.meta b/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs.meta new file mode 100644 index 0000000..8e69d75 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a34f8140d980431d8da4e397f1d62d2e +timeCreated: 1692579269 \ No newline at end of file From 2693e5b1f4f0a29d4d30c451458248225efafef7 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 19:13:41 -0300 Subject: [PATCH 10/19] Automate get rigidbodies from ragdoll parts --- Assets/Scripts/ActiveRagdoll/RagdollJoint.cs | 2 +- Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs | 6 ++++++ Assets/Scripts/Camera/CameraController.cs | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/ActiveRagdoll/RagdollJoint.cs b/Assets/Scripts/ActiveRagdoll/RagdollJoint.cs index cef52dc..0c32ad1 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollJoint.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollJoint.cs @@ -2,7 +2,7 @@ public class RagdollJoint : MonoBehaviour { - [SerializeField] private Rigidbody rigidbody; + [SerializeField] private new Rigidbody rigidbody; [SerializeField] private ConfigurableJoint joint; public Rigidbody Rigidbody => rigidbody; diff --git a/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs b/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs index a82a0ab..8eff526 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs @@ -63,6 +63,12 @@ private static JointDrive CreateJointDrive(float positionSpring) return jointDrive; } + internal Rigidbody GetRigidBodyFromJoint(string jointID) + { + RagdollJoint joint = GetRagdollJointWithID(jointID); + return joint.Rigidbody; + } + internal bool TryGetJointWithID(string jointID, out RagdollJoint joint) { bool gotJoint = joints.TryGetValue(jointID, out joint); diff --git a/Assets/Scripts/Camera/CameraController.cs b/Assets/Scripts/Camera/CameraController.cs index f894298..b68cb39 100644 --- a/Assets/Scripts/Camera/CameraController.cs +++ b/Assets/Scripts/Camera/CameraController.cs @@ -16,7 +16,7 @@ public class CameraController : MonoBehaviour public float minAngle = -35.0f; public float maxAngle = -15.0f; - [SerializeField] private Camera camera; + [SerializeField] private new Camera camera; private float currentX = 0.0f; private float currentY = 0.0f; private Quaternion rotation; From 027ba3ae8b83ca94023ab21c534073cc6a643666 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 19:13:57 -0300 Subject: [PATCH 11/19] Add missing hand parts to constants --- Assets/Scripts/ActiveRagdoll/RagdollParts.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Assets/Scripts/ActiveRagdoll/RagdollParts.cs b/Assets/Scripts/ActiveRagdoll/RagdollParts.cs index b8c3ba1..3676a06 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollParts.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollParts.cs @@ -15,5 +15,7 @@ public class RagdollParts internal const string LOWER_LEFT_LEG = "LowerLeftLeg"; internal const string RIGHT_FOOT = "RightFoot"; internal const string LEFT_FOOT = "LeftFoot"; + internal const string RIGHT_HAND = "RightHand"; + internal const string LEFT_HAND = "LeftHand"; } } \ No newline at end of file From 8d1e98ac58866c9ccbaca70b4b7f6ca914cb961a Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 19:14:51 -0300 Subject: [PATCH 12/19] Replace Rigidbody obtaining to use jointHandler --- .../ActiveRagdoll_Player.prefab | 80 +++++++++---------- .../ActiveRagdoll/RagdollController.cs | 25 +++--- 2 files changed, 52 insertions(+), 53 deletions(-) diff --git a/Assets/Prefabs/ActiveRagdollTemplate/ActiveRagdoll_Player.prefab b/Assets/Prefabs/ActiveRagdollTemplate/ActiveRagdoll_Player.prefab index 9bef08c..e548bbf 100644 --- a/Assets/Prefabs/ActiveRagdollTemplate/ActiveRagdoll_Player.prefab +++ b/Assets/Prefabs/ActiveRagdollTemplate/ActiveRagdoll_Player.prefab @@ -3168,44 +3168,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 67122c3deae63fc46aa427f277a4cf0c, type: 3} m_Name: m_EditorClassIdentifier: - RagdollDict: - keys: - - Root - - Body - - Head - - UpperRightArm - - LowerRightArm - - RightHand - - UpperLeftArm - - LowerLeftArm - - LeftHand - - UpperRightLeg - - LowerRightLeg - - RightFoot - - UpperLeftLeg - - LowerLeftLeg - - LeftFoot - values: - - {fileID: 4985531305610238155} - - {fileID: 2342578877390874844} - - {fileID: 8180284542250461611} - - {fileID: 5939825930290575603} - - {fileID: 2424591407695190332} - - {fileID: 7622080051543942553} - - {fileID: 1139281219103369986} - - {fileID: 2646510523600694781} - - {fileID: 5152617412082605911} - - {fileID: 6109431878821211215} - - {fileID: 6701170567278905216} - - {fileID: 7927863737550955148} - - {fileID: 7370826325351747278} - - {fileID: 2835265873350387129} - - {fileID: 6857027438714089387} rightHand: {fileID: 8342660944763911090} leftHand: {fileID: 8342660944763911089} centerOfMass: {fileID: 4185505338602700740} - grabRight: {fileID: 3016776706953809226} - grabLeft: {fileID: 308789990690968185} forwardIsCameraDirection: 1 moveSpeed: 10 turnSpeed: 5 @@ -3213,14 +3178,10 @@ MonoBehaviour: autoGetUpWhenPossible: 1 useStepPrediction: 1 balanceHeight: 2.5 - balanceStrength: 5000 - coreStrength: 1500 - limbStrength: 500 StepDuration: 0.2 StepHeight: 1.7 FeetMountForce: 25 reachSensitivity: 350 - armReachStiffness: 2000 canBeKnockoutByImpact: 1 requiredForceToBeKO: 20 canPunch: 1 @@ -3230,6 +3191,45 @@ MonoBehaviour: inAir: 0 punchingRight: 0 punchingLeft: 0 + cam: {fileID: 0} + jointHandler: + balanceStrength: 5000 + coreStrength: 1500 + limbStrength: 500 + armReachStiffness: 2000 + joints: + keys: + - Root + - Body + - Head + - UpperRightArm + - LowerRightArm + - RightHand + - UpperLeftArm + - LowerLeftArm + - LeftHand + - UpperRightLeg + - LowerRightLeg + - RightFoot + - UpperLeftLeg + - LowerLeftLeg + - LeftFoot + values: + - {fileID: 4985531305610238155} + - {fileID: 2342578877390874844} + - {fileID: 8180284542250461611} + - {fileID: 5939825930290575603} + - {fileID: 2424591407695190332} + - {fileID: 7622080051543942553} + - {fileID: 1139281219103369986} + - {fileID: 2646510523600694781} + - {fileID: 5152617412082605911} + - {fileID: 6109431878821211215} + - {fileID: 6701170567278905216} + - {fileID: 7927863737550955148} + - {fileID: 7370826325351747278} + - {fileID: 2835265873350387129} + - {fileID: 6857027438714089387} --- !u!1 &8342660944763911102 GameObject: m_ObjectHideFlags: 0 @@ -3471,7 +3471,6 @@ MonoBehaviour: m_EditorClassIdentifier: ragdollController: {fileID: 853821725901430288} Left: 1 - hasJoint: 0 --- !u!1 &8342660944763911103 GameObject: m_ObjectHideFlags: 0 @@ -3713,4 +3712,3 @@ MonoBehaviour: m_EditorClassIdentifier: ragdollController: {fileID: 853821725901430288} Left: 0 - hasJoint: 0 diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index ead3b96..441af0a 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -7,8 +7,7 @@ public class RagdollController : MonoBehaviour { - [SerializeField] private Rigidbody rightHand; - [SerializeField] private Rigidbody leftHand; + [SerializeField] private RagdollJointHandler jointHandler; [SerializeField] private Transform centerOfMass; [Header("Movement Properties")] public bool forwardIsCameraDirection = true; @@ -49,10 +48,12 @@ public class RagdollController : MonoBehaviour [SerializeField] private Camera cam; private Vector3 Direction; private Vector3 CenterOfMassPoint; //TODO: Check usage - [SerializeField] private RagdollJointHandler jointHandler; + private RagdollDefaultTargetState defaultTargetState; private static int groundLayer; private readonly WaitForSeconds punchDelayWaitTime = new(0.3f); + private Rigidbody RightHandRigidBody => jointHandler.GetRigidBodyFromJoint(RagdollParts.RIGHT_HAND); + private Rigidbody LeftHandRigidBody => jointHandler.GetRigidBodyFromJoint(RagdollParts.LEFT_HAND); void Awake() { @@ -213,7 +214,7 @@ private void PerformPlayerGetUpJumping() { isJumping = true; - Rigidbody rootRigidbody = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).Rigidbody; + Rigidbody rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); rootRigidbody.velocity = rootRigidbody.velocity.ModifyY(rootRigidbody.transform.up.y * jumpForce); } @@ -271,7 +272,7 @@ private bool ShouldSetBalanced() !locomotionController.reachRightAxisUsed && !locomotionController.reachLeftAxisUsed && !locomotionController.balanced && - jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).Rigidbody.velocity.magnitude < 1f && + jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT).velocity.magnitude < 1f && autoGetUpWhenPossible; } @@ -298,7 +299,7 @@ private void MoveInCameraDirection() Direction = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).transform.rotation * new Vector3(inputHandler.MovementAxis.x, 0.0f, inputHandler.MovementAxis.y); Direction.y = 0f; - Rigidbody rootRigidbody = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).Rigidbody; + Rigidbody rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); var velocity = rootRigidbody.velocity; rootRigidbody.velocity = Vector3.Lerp(velocity, (Direction * moveSpeed) + new Vector3(0, velocity.y, 0), 0.8f); @@ -337,7 +338,7 @@ private void MoveInOwnDirection() { if (inputHandler.MovementAxis.y != 0) { - var rootRigidbody = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).Rigidbody; + var rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); var v3 = rootRigidbody.transform.forward * (inputHandler.MovementAxis.y * moveSpeed); v3.y = rootRigidbody.velocity.y; rootRigidbody.velocity = v3; @@ -526,12 +527,12 @@ private IEnumerator PunchDelayCoroutine(bool isRightArm, ConfigurableJoint upper private void HandleLeftPunch() => HandlePunch(ref punchingLeft, inputHandler.PunchLeftValue, false, RagdollParts.UPPER_LEFT_ARM, - RagdollParts.LOWER_LEFT_ARM, leftHand, + RagdollParts.LOWER_LEFT_ARM, LeftHandRigidBody, () => defaultTargetState.UpperLeftArmTarget, () => defaultTargetState.LowerLeftArmTarget); private void HandleRightPunch() => HandlePunch(ref punchingRight, inputHandler.PunchRightValue, true, RagdollParts.UPPER_RIGHT_ARM, - RagdollParts.LOWER_RIGHT_ARM, rightHand, () => defaultTargetState.UpperRightArmTarget, + RagdollParts.LOWER_RIGHT_ARM, RightHandRigidBody, () => defaultTargetState.UpperRightArmTarget, () => defaultTargetState.LowerLeftArmTarget); private void PerformWalking() @@ -591,8 +592,8 @@ private void ResetStep(string upperLegLabel, lowerLegLerpMultiplier * Time.fixedDeltaTime); Vector3 feetForce = -Vector3.up * (FeetMountForce * Time.deltaTime); - jointHandler.GetRagdollJointWithID(RagdollParts.RIGHT_FOOT).Rigidbody.AddForce(feetForce, ForceMode.Impulse); - jointHandler.GetRagdollJointWithID(RagdollParts.LEFT_FOOT).Rigidbody.AddForce(feetForce, ForceMode.Impulse); + jointHandler.GetRigidBodyFromJoint(RagdollParts.RIGHT_FOOT).AddForce(feetForce, ForceMode.Impulse); + jointHandler.GetRigidBodyFromJoint(RagdollParts.LEFT_FOOT).AddForce(feetForce, ForceMode.Impulse); } private void TakeStepLeft() => TakeStep(ref locomotionController.stepLTimer, RagdollParts.LEFT_FOOT, @@ -614,7 +615,7 @@ private void TakeStep(ref float stepTimer, string upperOppositeJointLabel) { stepTimer += Time.fixedDeltaTime; - jointHandler.GetRagdollJointWithID(footLabel).Rigidbody + jointHandler.GetRigidBodyFromJoint(footLabel) .AddForce(-Vector3.up * (FeetMountForce * Time.deltaTime), ForceMode.Impulse); var upperLegJoint = jointHandler.GetConfigurableJointWithID(upperJointLabel); From 6681d5a95a93f7a0e16c86d344e6adf09747c8fd Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 22:19:18 -0300 Subject: [PATCH 13/19] Add ragdoll state --- .../ActiveRagdoll/RagdollController.cs | 94 ++++++++++--------- .../RagdollDefaultTargetState.cs | 1 + .../ActiveRagdoll/RagdollFeetContact.cs | 17 +++- .../ActiveRagdoll/RagdollHandContact.cs | 9 +- Assets/Scripts/ActiveRagdoll/RagdollState.cs | 11 +++ .../ActiveRagdoll/RagdollState.cs.meta | 3 + 6 files changed, 83 insertions(+), 52 deletions(-) create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollState.cs create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollState.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index 441af0a..8c6dfc6 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -39,14 +39,10 @@ public class RagdollController : MonoBehaviour private readonly RagdollInputHandler inputHandler = new(); private RagdollLocomotionController locomotionController; - [HideInInspector] public bool jumping; - [HideInInspector] public bool isJumping; - [HideInInspector] public bool inAir; - [HideInInspector] public bool punchingRight; - [HideInInspector] public bool punchingLeft; + private RagdollState ragdollState = new(); + [SerializeField] private Camera cam; - private Vector3 Direction; private Vector3 CenterOfMassPoint; //TODO: Check usage private RagdollDefaultTargetState defaultTargetState; @@ -55,13 +51,14 @@ public class RagdollController : MonoBehaviour private Rigidbody RightHandRigidBody => jointHandler.GetRigidBodyFromJoint(RagdollParts.RIGHT_HAND); private Rigidbody LeftHandRigidBody => jointHandler.GetRigidBodyFromJoint(RagdollParts.LEFT_HAND); - void Awake() + private void Awake() { groundLayer = LayerMask.NameToLayer("Ground"); inputHandler.Init(); locomotionController = new RagdollLocomotionController(jointHandler); defaultTargetState = new RagdollDefaultTargetState(jointHandler); SetupHandContacts(); + SetupFeetContacts(); } private void SetupHandContacts() @@ -69,13 +66,22 @@ private void SetupHandContacts() var handContacts = GetComponentsInChildren(); foreach (var handContact in handContacts) { - handContact.Init(inputHandler); + handContact.Init(inputHandler, ragdollState); + } + } + + private void SetupFeetContacts() + { + var feetContacts = GetComponentsInChildren(); + foreach (var feetContact in feetContacts) + { + feetContact.Init(ragdollState); } } private void Update() { - if (!inAir) + if (!ragdollState.inAir) { PlayerMovement(); @@ -153,35 +159,35 @@ private void PerformPlayerRotation() private void ResetPlayerPose() { - if (locomotionController.resetPose && !jumping) - { - jointHandler.GetConfigurableJointWithID(RagdollParts.BODY).targetRotation = defaultTargetState.BodyTarget; - jointHandler.GetConfigurableJointWithID(RagdollParts.UPPER_RIGHT_ARM).targetRotation = - defaultTargetState.UpperRightArmTarget; - jointHandler.GetConfigurableJointWithID(RagdollParts.LOWER_RIGHT_ARM).targetRotation = - defaultTargetState.LowerRightArmTarget; - jointHandler.GetConfigurableJointWithID(RagdollParts.UPPER_LEFT_ARM).targetRotation = - defaultTargetState.UpperLeftArmTarget; - jointHandler.GetConfigurableJointWithID(RagdollParts.LOWER_LEFT_ARM).targetRotation = - defaultTargetState.LowerLeftArmTarget; + if (!locomotionController.resetPose || ragdollState.jumping) + return; - MouseYAxisArms = 0; - locomotionController.resetPose = false; - } + jointHandler.GetConfigurableJointWithID(RagdollParts.BODY).targetRotation = defaultTargetState.BodyTarget; + jointHandler.GetConfigurableJointWithID(RagdollParts.UPPER_RIGHT_ARM).targetRotation = + defaultTargetState.UpperRightArmTarget; + jointHandler.GetConfigurableJointWithID(RagdollParts.LOWER_RIGHT_ARM).targetRotation = + defaultTargetState.LowerRightArmTarget; + jointHandler.GetConfigurableJointWithID(RagdollParts.UPPER_LEFT_ARM).targetRotation = + defaultTargetState.UpperLeftArmTarget; + jointHandler.GetConfigurableJointWithID(RagdollParts.LOWER_LEFT_ARM).targetRotation = + defaultTargetState.LowerLeftArmTarget; + + MouseYAxisArms = 0; + locomotionController.resetPose = false; } public void PlayerLanded() { if (CanResetPoseAfterLanding()) { - inAir = false; + ragdollState.inAir = false; locomotionController.resetPose = true; } } private bool CanResetPoseAfterLanding() { - return inAir && !isJumping && !jumping; + return ragdollState.inAir && !ragdollState.isJumping && !ragdollState.jumping; } private void PerformPlayerGetUpJumping() @@ -190,9 +196,9 @@ private void PerformPlayerGetUpJumping() { if (!locomotionController.jumpAxisUsed) { - if (locomotionController.balanced && !inAir) + if (locomotionController.balanced && !ragdollState.inAir) { - jumping = true; + ragdollState.jumping = true; } else if (!locomotionController.balanced) @@ -210,24 +216,24 @@ private void PerformPlayerGetUpJumping() } - if (jumping) + if (ragdollState.jumping) { - isJumping = true; + ragdollState.isJumping = true; Rigidbody rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); rootRigidbody.velocity = rootRigidbody.velocity.ModifyY(rootRigidbody.transform.up.y * jumpForce); } - if (isJumping) + if (ragdollState.isJumping) { jumpingResetTimer += Time.fixedDeltaTime; if (jumpingResetTimer > 0.2f) { jumpingResetTimer = 0.0f; - jumping = false; - isJumping = false; - inAir = true; + ragdollState.jumping = false; + ragdollState.isJumping = false; + ragdollState.inAir = true; } } } @@ -267,8 +273,8 @@ private void GroundCheck() private bool ShouldSetBalanced() { - return !inAir && - !isJumping && + return !ragdollState.inAir && + !ragdollState.isJumping && !locomotionController.reachRightAxisUsed && !locomotionController.reachLeftAxisUsed && !locomotionController.balanced && @@ -296,13 +302,13 @@ private void PlayerMovement() private void MoveInCameraDirection() { - Direction = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).transform.rotation * - new Vector3(inputHandler.MovementAxis.x, 0.0f, inputHandler.MovementAxis.y); - Direction.y = 0f; + var direction = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).transform.rotation * + new Vector3(inputHandler.MovementAxis.x, 0.0f, inputHandler.MovementAxis.y); + direction.y = 0f; Rigidbody rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); var velocity = rootRigidbody.velocity; rootRigidbody.velocity = Vector3.Lerp(velocity, - (Direction * moveSpeed) + new Vector3(0, velocity.y, 0), 0.8f); + (direction * moveSpeed) + new Vector3(0, velocity.y, 0), 0.8f); if (inputHandler.MovementAxis.x != 0 || inputHandler.MovementAxis.y != 0 && locomotionController.balanced) { @@ -458,10 +464,10 @@ private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reac } - private void HandleRightSideReach() => HandlePlayerReach(punchingRight, inputHandler.GrabRightValue, + private void HandleRightSideReach() => HandlePlayerReach(ragdollState.punchingRight, inputHandler.GrabRightValue, ref locomotionController.reachRightAxisUsed, RagdollParts.UPPER_RIGHT_ARM, RagdollParts.LOWER_RIGHT_ARM, true); - private void HandleLeftSideReach() => HandlePlayerReach(punchingLeft, inputHandler.GrabLeftValue, + private void HandleLeftSideReach() => HandlePlayerReach(ragdollState.punchingLeft, inputHandler.GrabLeftValue, ref locomotionController.reachLeftAxisUsed, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, false); private void PerformPlayerPunch() @@ -526,18 +532,18 @@ private IEnumerator PunchDelayCoroutine(bool isRightArm, ConfigurableJoint upper } private void HandleLeftPunch() => - HandlePunch(ref punchingLeft, inputHandler.PunchLeftValue, false, RagdollParts.UPPER_LEFT_ARM, + HandlePunch(ref ragdollState.punchingLeft, inputHandler.PunchLeftValue, false, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, LeftHandRigidBody, () => defaultTargetState.UpperLeftArmTarget, () => defaultTargetState.LowerLeftArmTarget); - private void HandleRightPunch() => HandlePunch(ref punchingRight, inputHandler.PunchRightValue, true, + private void HandleRightPunch() => HandlePunch(ref ragdollState.punchingRight, inputHandler.PunchRightValue, true, RagdollParts.UPPER_RIGHT_ARM, RagdollParts.LOWER_RIGHT_ARM, RightHandRigidBody, () => defaultTargetState.UpperRightArmTarget, () => defaultTargetState.LowerLeftArmTarget); private void PerformWalking() { - if (inAir) + if (ragdollState.inAir) return; if (locomotionController.walkForward) diff --git a/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs b/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs index 5aba615..2154044 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs @@ -24,6 +24,7 @@ public RagdollDefaultTargetState(RagdollJointHandler jointHandler) private void SetupOriginalPose(RagdollJointHandler jointsHandler) { + //TODO: Consider baking this data into the prefab instead of calculating on startup. BodyTarget = jointsHandler.GetJointTargetRotation(RagdollParts.ROOT); HeadTarget = jointsHandler.GetJointTargetRotation(RagdollParts.HEAD); UpperRightArmTarget = jointsHandler.GetJointTargetRotation(RagdollParts.UPPER_RIGHT_ARM); diff --git a/Assets/Scripts/ActiveRagdoll/RagdollFeetContact.cs b/Assets/Scripts/ActiveRagdoll/RagdollFeetContact.cs index 17b64bf..d870e41 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollFeetContact.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollFeetContact.cs @@ -1,3 +1,4 @@ +using ActiveRagdoll; using UnityEngine; public class RagdollFeetContact : MonoBehaviour @@ -5,6 +6,7 @@ public class RagdollFeetContact : MonoBehaviour [SerializeField] private RagdollController RagdollPlayer; private const string GROUND = "Ground"; private int groundLayer = -1; + private RagdollState ragdollState; private int GroundLayer { @@ -19,14 +21,19 @@ private int GroundLayer } } + public void Init(RagdollState state) + { + ragdollState = state; + } + private void OnCollisionEnter(Collision col) { - if (!RagdollPlayer.isJumping && RagdollPlayer.inAir) + if (ragdollState.isJumping || !ragdollState.inAir) + return; + + if (col.gameObject.layer == GroundLayer) { - if (col.gameObject.layer == GroundLayer) - { - RagdollPlayer.PlayerLanded(); - } + RagdollPlayer.PlayerLanded(); } } } \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs b/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs index 8df9c3e..af7be56 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollHandContact.cs @@ -1,3 +1,4 @@ +using ActiveRagdoll; using UnityEngine; public class RagdollHandContact : MonoBehaviour @@ -9,10 +10,12 @@ public class RagdollHandContact : MonoBehaviour private const string CAN_BE_GRABBED = "CanBeGrabbed"; private IInputListener inputListener; private FixedJoint joint; + private RagdollState ragdollState; - public void Init(IInputListener newInputListener) + public void Init(IInputListener newInputListener, RagdollState state) { inputListener = newInputListener; + ragdollState = state; } private void Update() @@ -58,11 +61,11 @@ private bool CanPerformGrabAction() { if (Left) { - return inputListener.GrabLeftValue != 0 && !ragdollController.punchingLeft; + return inputListener.GrabLeftValue != 0 && !ragdollState.punchingLeft; } else { - return inputListener.GrabRightValue != 0 && !ragdollController.punchingRight; + return inputListener.GrabRightValue != 0 && !ragdollState.punchingRight; } } diff --git a/Assets/Scripts/ActiveRagdoll/RagdollState.cs b/Assets/Scripts/ActiveRagdoll/RagdollState.cs new file mode 100644 index 0000000..95e045c --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollState.cs @@ -0,0 +1,11 @@ +namespace ActiveRagdoll +{ + public class RagdollState + { + public bool jumping; + public bool isJumping; + public bool inAir; + public bool punchingRight; + public bool punchingLeft; + } +} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollState.cs.meta b/Assets/Scripts/ActiveRagdoll/RagdollState.cs.meta new file mode 100644 index 0000000..5f9d80d --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollState.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 52e8a81a2259438ba2f8c5706468892e +timeCreated: 1692666604 \ No newline at end of file From 760cce6dfca0f0d352a8b377f515244f81b93c02 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 22:20:21 -0300 Subject: [PATCH 14/19] Remove jointdrive helper --- Assets/Scripts/ActiveRagdoll/JointDriveHelper.cs | 13 ------------- .../Scripts/ActiveRagdoll/JointDriveHelper.cs.meta | 3 --- 2 files changed, 16 deletions(-) delete mode 100644 Assets/Scripts/ActiveRagdoll/JointDriveHelper.cs delete mode 100644 Assets/Scripts/ActiveRagdoll/JointDriveHelper.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/JointDriveHelper.cs b/Assets/Scripts/ActiveRagdoll/JointDriveHelper.cs deleted file mode 100644 index 7954812..0000000 --- a/Assets/Scripts/ActiveRagdoll/JointDriveHelper.cs +++ /dev/null @@ -1,13 +0,0 @@ -using UnityEngine; - -internal class JointDriveHelper -{ - internal static JointDrive CreateJointDrive(float positionSpring) - { - JointDrive jointDrive = new JointDrive(); - jointDrive.positionSpring = positionSpring; - jointDrive.positionDamper = 0; - jointDrive.maximumForce = Mathf.Infinity; - return jointDrive; - } -} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/JointDriveHelper.cs.meta b/Assets/Scripts/ActiveRagdoll/JointDriveHelper.cs.meta deleted file mode 100644 index c6df86c..0000000 --- a/Assets/Scripts/ActiveRagdoll/JointDriveHelper.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 328fd81864404eb59eafd2f159768096 -timeCreated: 1691903841 \ No newline at end of file From fa20facf1b11b862b04d11be8714dc4b54b8ee0c Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 22:30:33 -0300 Subject: [PATCH 15/19] Implement contact handler class --- .../ActiveRagdoll/RagdollController.cs | 29 ++++++++++--------- .../ActiveRagdoll/RagdollImpactContact.cs | 19 ++++++++---- .../ActiveRagdoll/RagdollImpactHandler.cs | 11 +++++++ .../RagdollImpactHandler.cs.meta | 3 ++ .../RagdollLocomotionController.cs | 9 +++++- 5 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs create mode 100644 Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index 8c6dfc6..0e977a6 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -9,7 +9,7 @@ public class RagdollController : MonoBehaviour { [SerializeField] private RagdollJointHandler jointHandler; [SerializeField] private Transform centerOfMass; - + [SerializeField] private RagdollImpactHandler impactHandler; [Header("Movement Properties")] public bool forwardIsCameraDirection = true; public float moveSpeed = 10f; public float turnSpeed = 6f; @@ -25,9 +25,7 @@ public class RagdollController : MonoBehaviour [Header("Reach Properties")] public float reachSensitivity = 25f; - [Header("Actions")] public bool canBeKnockoutByImpact = true; - public float requiredForceToBeKO = 20f; - public bool canPunch = true; + [Header("Actions")] public bool canPunch = true; public float punchForce = 15f; //Hidden variables @@ -39,7 +37,7 @@ public class RagdollController : MonoBehaviour private readonly RagdollInputHandler inputHandler = new(); private RagdollLocomotionController locomotionController; - private RagdollState ragdollState = new(); + private readonly RagdollState ragdollState = new(); [SerializeField] private Camera cam; @@ -59,6 +57,16 @@ private void Awake() defaultTargetState = new RagdollDefaultTargetState(jointHandler); SetupHandContacts(); SetupFeetContacts(); + SetupRagdollImpactContacts(); + } + + private void SetupRagdollImpactContacts() + { + var impactContacts = GetComponentsInChildren(); + foreach (var impactContact in impactContacts) + { + impactContact.Init(impactHandler, locomotionController); + } } private void SetupHandContacts() @@ -203,7 +211,7 @@ private void PerformPlayerGetUpJumping() else if (!locomotionController.balanced) { - DeactivateRagdoll(); + locomotionController.DeactivateRagdoll(); } } @@ -263,11 +271,11 @@ private void GroundCheck() if (locomotionController.balanced) { - DeactivateRagdoll(); + locomotionController.DeactivateRagdoll(); } else { - ActivateRagdoll(); + locomotionController.ActivateRagdoll(); } } @@ -282,11 +290,6 @@ private bool ShouldSetBalanced() autoGetUpWhenPossible; } - private void DeactivateRagdoll() => - locomotionController.SetRagdollState(false, ref jointHandler.BalanceOn, ref jointHandler.PoseOn, true); - - public void ActivateRagdoll() => - locomotionController.SetRagdollState(true, ref jointHandler.DriveOff, ref jointHandler.DriveOff, false); private void PlayerMovement() { diff --git a/Assets/Scripts/ActiveRagdoll/RagdollImpactContact.cs b/Assets/Scripts/ActiveRagdoll/RagdollImpactContact.cs index 0dab923..e810887 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollImpactContact.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollImpactContact.cs @@ -1,14 +1,23 @@ +using ActiveRagdoll; using UnityEngine; public class RagdollImpactContact : MonoBehaviour { - public RagdollController ragdollController; + private RagdollImpactHandler ragdollImpactHandler; + private RagdollLocomotionController locomotionController; - void OnCollisionEnter(Collision col) + public void Init(RagdollImpactHandler impactHandler, RagdollLocomotionController locomotionController) { - if (ragdollController.canBeKnockoutByImpact && col.relativeVelocity.magnitude > ragdollController.requiredForceToBeKO) + ragdollImpactHandler = impactHandler; + this.locomotionController = locomotionController; + } + + private void OnCollisionEnter(Collision col) + { + if (ragdollImpactHandler.canBeKnockoutByImpact && + col.relativeVelocity.magnitude > ragdollImpactHandler.requiredForceToBeKO) { - ragdollController.ActivateRagdoll(); + locomotionController.ActivateRagdoll(); } } -} +} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs b/Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs new file mode 100644 index 0000000..a529128 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs @@ -0,0 +1,11 @@ +using UnityEngine; + +namespace ActiveRagdoll +{ + [System.Serializable] + public class RagdollImpactHandler + { + [field: SerializeField] public bool canBeKnockoutByImpact { get; private set; } = true; + [field: SerializeField] public float requiredForceToBeKO { get; private set; } = 20f; + } +} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs.meta b/Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs.meta new file mode 100644 index 0000000..e92f516 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fe3e1d157867417cbdf91e7dd90d2e56 +timeCreated: 1692667379 \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs b/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs index 4063896..03ebbdc 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs @@ -29,7 +29,7 @@ public RagdollLocomotionController(RagdollJointHandler jointsHandler) jointHandler = jointsHandler; } - public void SetRagdollState(bool shouldRagdoll, ref JointDrive rootJointDrive, + private void SetRagdollState(bool shouldRagdoll, ref JointDrive rootJointDrive, ref JointDrive poseJointDrive, bool shouldResetPose) { @@ -62,6 +62,13 @@ public void SetRagdollState(bool shouldRagdoll, ref JointDrive rootJointDrive, resetPose = true; } + public void DeactivateRagdoll() => + SetRagdollState(false, ref jointHandler.BalanceOn, ref jointHandler.PoseOn, true); + + public void ActivateRagdoll() => + SetRagdollState(true, ref jointHandler.DriveOff, ref jointHandler.DriveOff, false); + + internal void Walk(string forwardFootLabel, string backFootLabel, ref bool forwardFootState, ref bool backFootState, ref bool forwardAlertLeg, ref bool backAlertLeg) { From caffd706a2cf2f262682b11eeb77864c3e6e98d4 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 22:53:02 -0300 Subject: [PATCH 16/19] Seaparate components into subsystems --- .../ActiveRagdoll/RagdollController.cs | 174 +++----------- .../ActiveRagdoll/RagdollImpactContact.cs | 10 +- .../RagdollLocomotionController.cs | 98 -------- Assets/Scripts/ActiveRagdoll/RagdollState.cs | 11 - .../ActiveRagdoll/SubSystemHandlers.meta | 3 + .../RagdollDefaultTargetState.cs | 0 .../RagdollDefaultTargetState.cs.meta | 0 .../RagdollImpactHandler.cs | 0 .../RagdollImpactHandler.cs.meta | 0 .../RagdollJointHandler.cs | 0 .../RagdollJointHandler.cs.meta | 0 .../RagdollLocomotionController.cs | 219 ++++++++++++++++++ .../RagdollLocomotionController.cs.meta | 0 .../SubSystemHandlers/RagdollState.cs | 20 ++ .../RagdollState.cs.meta | 0 15 files changed, 277 insertions(+), 258 deletions(-) delete mode 100644 Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs delete mode 100644 Assets/Scripts/ActiveRagdoll/RagdollState.cs create mode 100644 Assets/Scripts/ActiveRagdoll/SubSystemHandlers.meta rename Assets/Scripts/ActiveRagdoll/{ => SubSystemHandlers}/RagdollDefaultTargetState.cs (100%) rename Assets/Scripts/ActiveRagdoll/{ => SubSystemHandlers}/RagdollDefaultTargetState.cs.meta (100%) rename Assets/Scripts/ActiveRagdoll/{ => SubSystemHandlers}/RagdollImpactHandler.cs (100%) rename Assets/Scripts/ActiveRagdoll/{ => SubSystemHandlers}/RagdollImpactHandler.cs.meta (100%) rename Assets/Scripts/ActiveRagdoll/{ => SubSystemHandlers}/RagdollJointHandler.cs (100%) rename Assets/Scripts/ActiveRagdoll/{ => SubSystemHandlers}/RagdollJointHandler.cs.meta (100%) create mode 100644 Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollLocomotionController.cs rename Assets/Scripts/ActiveRagdoll/{ => SubSystemHandlers}/RagdollLocomotionController.cs.meta (100%) create mode 100644 Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollState.cs rename Assets/Scripts/ActiveRagdoll/{ => SubSystemHandlers}/RagdollState.cs.meta (100%) diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index 0e977a6..d2dd91a 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -15,10 +15,6 @@ public class RagdollController : MonoBehaviour public float turnSpeed = 6f; public float jumpForce = 18f; - [Header("Balance Properties")] public bool autoGetUpWhenPossible = true; - public bool useStepPrediction = true; - public float balanceHeight = 2.5f; - public float StepDuration = 0.2f; public float StepHeight = 1.7f; public float FeetMountForce = 25f; @@ -29,10 +25,6 @@ public class RagdollController : MonoBehaviour public float punchForce = 15f; //Hidden variables - private float jumpingResetTimer; - private float MouseYAxisArms; - private float MouseXAxisArms; - private float MouseYAxisBody; private readonly RagdollInputHandler inputHandler = new(); @@ -53,7 +45,7 @@ private void Awake() { groundLayer = LayerMask.NameToLayer("Ground"); inputHandler.Init(); - locomotionController = new RagdollLocomotionController(jointHandler); + locomotionController = new RagdollLocomotionController(jointHandler, ragdollState); defaultTargetState = new RagdollDefaultTargetState(jointHandler); SetupHandContacts(); SetupFeetContacts(); @@ -101,17 +93,9 @@ private void Update() PlayerReach(); - if (locomotionController.balanced && useStepPrediction) - { - PerformStepPrediction(); - } - - if (!useStepPrediction) - { - ResetWalkCycle(); - } + locomotionController.HandleStepPrediction(centerOfMass.transform.position.z); - GroundCheck(); + locomotionController.GroundCheck(); UpdateCenterOfMass(); } @@ -180,7 +164,7 @@ private void ResetPlayerPose() jointHandler.GetConfigurableJointWithID(RagdollParts.LOWER_LEFT_ARM).targetRotation = defaultTargetState.LowerLeftArmTarget; - MouseYAxisArms = 0; + ragdollState.mouseYAxisArms = 0; locomotionController.resetPose = false; } @@ -202,7 +186,7 @@ private void PerformPlayerGetUpJumping() { if (inputHandler.JumpValue > 0) { - if (!locomotionController.jumpAxisUsed) + if (!ragdollState.jumpAxisUsed) { if (locomotionController.balanced && !ragdollState.inAir) { @@ -215,12 +199,12 @@ private void PerformPlayerGetUpJumping() } } - locomotionController.jumpAxisUsed = true; + ragdollState.jumpAxisUsed = true; } else { - locomotionController.jumpAxisUsed = false; + ragdollState.jumpAxisUsed = false; } @@ -234,11 +218,11 @@ private void PerformPlayerGetUpJumping() if (ragdollState.isJumping) { - jumpingResetTimer += Time.fixedDeltaTime; + ragdollState.jumpingResetTimer += Time.fixedDeltaTime; - if (jumpingResetTimer > 0.2f) + if (ragdollState.jumpingResetTimer > 0.2f) { - jumpingResetTimer = 0.0f; + ragdollState.jumpingResetTimer = 0.0f; ragdollState.jumping = false; ragdollState.isJumping = false; ragdollState.inAir = true; @@ -246,51 +230,6 @@ private void PerformPlayerGetUpJumping() } } - private void GroundCheck() - { - Transform rootTransform = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).transform; - Ray ray = new Ray(rootTransform.position, Vector3.down); - bool isHittingGround = Physics.Raycast(ray, out _, balanceHeight, 1 << groundLayer); - - if (!isHittingGround) - { - if (locomotionController.balanced) - { - locomotionController.balanced = false; - } - } - else if (ShouldSetBalanced()) - { - locomotionController.balanced = true; - } - - bool needsStateChange = (locomotionController.balanced == locomotionController.isRagdoll); - - if (!needsStateChange) - return; - - if (locomotionController.balanced) - { - locomotionController.DeactivateRagdoll(); - } - else - { - locomotionController.ActivateRagdoll(); - } - } - - private bool ShouldSetBalanced() - { - return !ragdollState.inAir && - !ragdollState.isJumping && - !locomotionController.reachRightAxisUsed && - !locomotionController.reachLeftAxisUsed && - !locomotionController.balanced && - jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT).velocity.magnitude < 1f && - autoGetUpWhenPossible; - } - - private void PlayerMovement() { if (forwardIsCameraDirection) @@ -325,21 +264,21 @@ private void MoveInCameraDirection() private void StartWalkingForward() { - if (!locomotionController.walkForward && !locomotionController.moveAxisUsed) + if (!locomotionController.walkForward && !ragdollState.moveAxisUsed) { locomotionController.walkForward = true; - locomotionController.moveAxisUsed = true; - locomotionController.isKeyDown = true; + ragdollState.moveAxisUsed = true; + ragdollState.isKeyDown = true; } } private void StopWalkingForward() { - if (locomotionController.walkForward && locomotionController.moveAxisUsed) + if (locomotionController.walkForward && ragdollState.moveAxisUsed) { locomotionController.walkForward = false; - locomotionController.moveAxisUsed = false; - locomotionController.isKeyDown = false; + ragdollState.moveAxisUsed = false; + ragdollState.isKeyDown = false; } } @@ -368,18 +307,18 @@ private void MoveInOwnDirection() } private void StartWalkingForwardInOwnDirection() => - SetWalkMovingState(() => (!locomotionController.walkForward && !locomotionController.moveAxisUsed), true, false, + SetWalkMovingState(() => (!locomotionController.walkForward && !ragdollState.moveAxisUsed), true, false, true, true, jointHandler.PoseOn); private void StartWalkingBackward() => - SetWalkMovingState(() => !locomotionController.walkBackward && !locomotionController.moveAxisUsed, false, true, + SetWalkMovingState(() => !locomotionController.walkBackward && !ragdollState.moveAxisUsed, false, true, true, true, jointHandler.PoseOn); private void StopWalking() => SetWalkMovingState( () => locomotionController.walkForward || - locomotionController.walkBackward && locomotionController.moveAxisUsed, false, false, + locomotionController.walkBackward && ragdollState.moveAxisUsed, false, false, false, false, jointHandler.DriveOff); private void SetWalkMovingState(Func activationCondition, bool walkForwardSetState, bool walkBackwardSetState, @@ -398,8 +337,8 @@ private void InternalChangeWalkState(bool walkForward, bool walkBackward, bool i { locomotionController.walkForward = walkForward; locomotionController.walkBackward = walkBackward; - locomotionController.moveAxisUsed = isMoveAxisUsed; - locomotionController.isKeyDown = isKeyCurrentlyDown; + ragdollState.moveAxisUsed = isMoveAxisUsed; + ragdollState.isKeyDown = isKeyCurrentlyDown; if (locomotionController.isRagdoll) SetJointAngularDrivesForLegs(in legsJointDrive); } @@ -416,9 +355,10 @@ private void SetJointAngularDrivesForLegs(in JointDrive jointDrive) private void PlayerReach() { - MouseYAxisBody = Mathf.Clamp(MouseYAxisBody += (inputHandler.AimAxis.y / reachSensitivity), -0.2f, 0.1f); + ragdollState.mouseYAxisBody = + Mathf.Clamp(ragdollState.mouseYAxisBody += (inputHandler.AimAxis.y / reachSensitivity), -0.2f, 0.1f); jointHandler.GetConfigurableJointWithID(RagdollParts.BODY).targetRotation = - new Quaternion(MouseYAxisBody, 0, 0, 1); + new Quaternion(ragdollState.mouseYAxisBody, 0, 0, 1); HandleLeftSideReach(); HandleRightSideReach(); @@ -440,9 +380,11 @@ private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reac } int multiplier = isRightArm ? 1 : -1; - MouseYAxisArms = Mathf.Clamp(MouseYAxisArms += (inputHandler.AimAxis.y / reachSensitivity), -1.2f, 1.2f); + ragdollState.mouseYAxisArms = + Mathf.Clamp(ragdollState.mouseYAxisArms += (inputHandler.AimAxis.y / reachSensitivity), -1.2f, 1.2f); jointHandler.GetConfigurableJointWithID(upperArmJoint).targetRotation = - new Quaternion((0.58f + (MouseYAxisArms)) * multiplier, -0.88f - (MouseYAxisArms), 0.8f * multiplier, + new Quaternion((0.58f + (ragdollState.mouseYAxisArms)) * multiplier, + -0.88f - (ragdollState.mouseYAxisArms), 0.8f * multiplier, 1); } else @@ -468,10 +410,10 @@ private void HandlePlayerReach(bool punchingSide, float grabValue, ref bool reac private void HandleRightSideReach() => HandlePlayerReach(ragdollState.punchingRight, inputHandler.GrabRightValue, - ref locomotionController.reachRightAxisUsed, RagdollParts.UPPER_RIGHT_ARM, RagdollParts.LOWER_RIGHT_ARM, true); + ref ragdollState.reachRightAxisUsed, RagdollParts.UPPER_RIGHT_ARM, RagdollParts.LOWER_RIGHT_ARM, true); private void HandleLeftSideReach() => HandlePlayerReach(ragdollState.punchingLeft, inputHandler.GrabLeftValue, - ref locomotionController.reachLeftAxisUsed, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, false); + ref ragdollState.reachLeftAxisUsed, RagdollParts.UPPER_LEFT_ARM, RagdollParts.LOWER_LEFT_ARM, false); private void PerformPlayerPunch() { @@ -684,65 +626,9 @@ private void WalkForwards() => locomotionController.Walk(RagdollParts.RIGHT_FOOT ref locomotionController.alertLegRight, ref locomotionController.alertLegLeft); - private void PerformStepPrediction() - { - if (!locomotionController.walkForward && !locomotionController.walkBackward) - { - locomotionController.stepRight = false; - locomotionController.stepLeft = false; - locomotionController.stepRTimer = 0; - locomotionController.stepLTimer = 0; - locomotionController.alertLegRight = false; - locomotionController.alertLegLeft = false; - } - - float rightFootZPosition = - jointHandler.GetConfigurableJointWithID(RagdollParts.RIGHT_FOOT).transform.position.z; - float leftFootZPosition = - jointHandler.GetConfigurableJointWithID(RagdollParts.LEFT_FOOT).transform.position.z; - - //TODO: Refactor to reduce amount of code here - if (centerOfMass.position.z < rightFootZPosition && centerOfMass.position.z < leftFootZPosition) - { - locomotionController.walkBackward = true; - } - else - { - if (!locomotionController.isKeyDown) - { - locomotionController.walkBackward = false; - } - } - - if (centerOfMass.position.z > rightFootZPosition && centerOfMass.position.z > leftFootZPosition) - { - locomotionController.walkForward = true; - } - else - { - if (!locomotionController.isKeyDown) - { - locomotionController.walkForward = false; - } - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] private void UpdateCenterOfMass() { jointHandler.GetCenterOfMass(out CenterOfMassPoint, centerOfMass); } - - private void ResetWalkCycle() - { - if (!locomotionController.walkForward && !locomotionController.walkBackward) - { - locomotionController.stepRight = false; - locomotionController.stepLeft = false; - locomotionController.stepRTimer = 0; - locomotionController.stepLTimer = 0; - locomotionController.alertLegRight = false; - locomotionController.alertLegLeft = false; - } - } } \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollImpactContact.cs b/Assets/Scripts/ActiveRagdoll/RagdollImpactContact.cs index e810887..86d6a39 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollImpactContact.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollImpactContact.cs @@ -14,10 +14,10 @@ public void Init(RagdollImpactHandler impactHandler, RagdollLocomotionController private void OnCollisionEnter(Collision col) { - if (ragdollImpactHandler.canBeKnockoutByImpact && - col.relativeVelocity.magnitude > ragdollImpactHandler.requiredForceToBeKO) - { - locomotionController.ActivateRagdoll(); - } + if (!ragdollImpactHandler.canBeKnockoutByImpact || + col.relativeVelocity.magnitude < ragdollImpactHandler.requiredForceToBeKO) + return; + + locomotionController.ActivateRagdoll(); } } \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs b/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs deleted file mode 100644 index 03ebbdc..0000000 --- a/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs +++ /dev/null @@ -1,98 +0,0 @@ -using UnityEngine; - -namespace ActiveRagdoll -{ - public class RagdollLocomotionController - { - internal float stepRTimer; - internal float stepLTimer; - internal bool walkForward; - internal bool walkBackward; - internal bool stepRight; - internal bool stepLeft; - internal bool alertLegRight; - internal bool alertLegLeft; - internal bool balanced = true; - internal bool gettingUp; - internal bool resetPose; - internal bool isRagdoll; - internal bool isKeyDown; - internal bool moveAxisUsed; - internal bool jumpAxisUsed; - internal bool reachLeftAxisUsed; - internal bool reachRightAxisUsed; - - private RagdollJointHandler jointHandler; - - public RagdollLocomotionController(RagdollJointHandler jointsHandler) - { - jointHandler = jointsHandler; - } - - private void SetRagdollState(bool shouldRagdoll, ref JointDrive rootJointDrive, - ref JointDrive poseJointDrive, - bool shouldResetPose) - { - isRagdoll = shouldRagdoll; - balanced = !shouldRagdoll; - - jointHandler.SetJointAngularDrives(RagdollParts.ROOT, in rootJointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.HEAD, in poseJointDrive); - - if (!reachRightAxisUsed) - { - jointHandler.SetJointAngularDrives(RagdollParts.UPPER_RIGHT_ARM, in poseJointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.LOWER_RIGHT_ARM, in poseJointDrive); - } - - if (!reachLeftAxisUsed) - { - jointHandler.SetJointAngularDrives(RagdollParts.UPPER_LEFT_ARM, in poseJointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.LOWER_LEFT_ARM, in poseJointDrive); - } - - jointHandler.SetJointAngularDrives(RagdollParts.UPPER_RIGHT_LEG, in poseJointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.LOWER_RIGHT_LEG, in poseJointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.UPPER_LEFT_LEG, in poseJointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.LOWER_LEFT_LEG, in poseJointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.RIGHT_FOOT, in poseJointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.LEFT_FOOT, in poseJointDrive); - - if (shouldResetPose) - resetPose = true; - } - - public void DeactivateRagdoll() => - SetRagdollState(false, ref jointHandler.BalanceOn, ref jointHandler.PoseOn, true); - - public void ActivateRagdoll() => - SetRagdollState(true, ref jointHandler.DriveOff, ref jointHandler.DriveOff, false); - - - internal void Walk(string forwardFootLabel, string backFootLabel, ref bool forwardFootState, - ref bool backFootState, ref bool forwardAlertLeg, ref bool backAlertLeg) - { - float forwardFootTransformZ = - jointHandler.GetConfigurableJointWithID(forwardFootLabel).transform.position.z; - float backFootTransformZ = jointHandler.GetConfigurableJointWithID(backFootLabel).transform.position.z; - - bool forwardFootIsBehind = forwardFootTransformZ < backFootTransformZ; - bool forwardFootIsAhead = forwardFootTransformZ > backFootTransformZ; - - //TODO: Displace logic to not use alternating conditions - if (forwardFootIsBehind && !backFootState && !forwardAlertLeg) - { - forwardFootState = true; - forwardAlertLeg = true; - backAlertLeg = true; - } - - if (forwardFootIsAhead && !forwardFootState && !backAlertLeg) - { - backFootState = true; - backAlertLeg = true; - forwardAlertLeg = true; - } - } - } -} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollState.cs b/Assets/Scripts/ActiveRagdoll/RagdollState.cs deleted file mode 100644 index 95e045c..0000000 --- a/Assets/Scripts/ActiveRagdoll/RagdollState.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace ActiveRagdoll -{ - public class RagdollState - { - public bool jumping; - public bool isJumping; - public bool inAir; - public bool punchingRight; - public bool punchingLeft; - } -} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/SubSystemHandlers.meta b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers.meta new file mode 100644 index 0000000..e0b368e --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1c6e60544abe4af98a30664cac3d963e +timeCreated: 1692669145 \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollDefaultTargetState.cs similarity index 100% rename from Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs rename to Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollDefaultTargetState.cs diff --git a/Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs.meta b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollDefaultTargetState.cs.meta similarity index 100% rename from Assets/Scripts/ActiveRagdoll/RagdollDefaultTargetState.cs.meta rename to Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollDefaultTargetState.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollImpactHandler.cs similarity index 100% rename from Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs rename to Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollImpactHandler.cs diff --git a/Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs.meta b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollImpactHandler.cs.meta similarity index 100% rename from Assets/Scripts/ActiveRagdoll/RagdollImpactHandler.cs.meta rename to Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollImpactHandler.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollJointHandler.cs similarity index 100% rename from Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs rename to Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollJointHandler.cs diff --git a/Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs.meta b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollJointHandler.cs.meta similarity index 100% rename from Assets/Scripts/ActiveRagdoll/RagdollJointHandler.cs.meta rename to Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollJointHandler.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollLocomotionController.cs b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollLocomotionController.cs new file mode 100644 index 0000000..3516132 --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollLocomotionController.cs @@ -0,0 +1,219 @@ +using UnityEngine; + +namespace ActiveRagdoll +{ + public class RagdollLocomotionController + { + internal float stepRTimer; + internal float stepLTimer; + internal bool walkForward; + internal bool walkBackward; + internal bool stepRight; + internal bool stepLeft; + internal bool alertLegRight; + internal bool alertLegLeft; + internal bool balanced = true; + internal bool gettingUp; + internal bool resetPose; + internal bool isRagdoll; + + + [Header("Balance Properties")] public bool autoGetUpWhenPossible = true; + public bool useStepPrediction = true; + public float balanceHeight = 2.5f; + + private RagdollJointHandler jointHandler; + private static int groundLayer; + private const string GROUND_LAYER_NAME = "Ground"; + private RagdollState ragdollState; + + public RagdollLocomotionController(RagdollJointHandler jointsHandler, RagdollState state) + { + jointHandler = jointsHandler; + groundLayer = LayerMask.NameToLayer(GROUND_LAYER_NAME); + ragdollState = state; + } + + private void SetRagdollState(bool shouldRagdoll, ref JointDrive rootJointDrive, + ref JointDrive poseJointDrive, + bool shouldResetPose) + { + isRagdoll = shouldRagdoll; + balanced = !shouldRagdoll; + + jointHandler.SetJointAngularDrives(RagdollParts.ROOT, in rootJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.HEAD, in poseJointDrive); + + if (!ragdollState.reachRightAxisUsed) + { + jointHandler.SetJointAngularDrives(RagdollParts.UPPER_RIGHT_ARM, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LOWER_RIGHT_ARM, in poseJointDrive); + } + + if (!ragdollState.reachLeftAxisUsed) + { + jointHandler.SetJointAngularDrives(RagdollParts.UPPER_LEFT_ARM, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LOWER_LEFT_ARM, in poseJointDrive); + } + + jointHandler.SetJointAngularDrives(RagdollParts.UPPER_RIGHT_LEG, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LOWER_RIGHT_LEG, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.UPPER_LEFT_LEG, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LOWER_LEFT_LEG, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.RIGHT_FOOT, in poseJointDrive); + jointHandler.SetJointAngularDrives(RagdollParts.LEFT_FOOT, in poseJointDrive); + + if (shouldResetPose) + resetPose = true; + } + + public void DeactivateRagdoll() => + SetRagdollState(false, ref jointHandler.BalanceOn, ref jointHandler.PoseOn, true); + + public void ActivateRagdoll() => + SetRagdollState(true, ref jointHandler.DriveOff, ref jointHandler.DriveOff, false); + + + internal void Walk(string forwardFootLabel, string backFootLabel, ref bool forwardFootState, + ref bool backFootState, ref bool forwardAlertLeg, ref bool backAlertLeg) + { + float forwardFootTransformZ = + jointHandler.GetConfigurableJointWithID(forwardFootLabel).transform.position.z; + float backFootTransformZ = jointHandler.GetConfigurableJointWithID(backFootLabel).transform.position.z; + + bool forwardFootIsBehind = forwardFootTransformZ < backFootTransformZ; + bool forwardFootIsAhead = forwardFootTransformZ > backFootTransformZ; + + //TODO: Displace logic to not use alternating conditions + if (forwardFootIsBehind && !backFootState && !forwardAlertLeg) + { + forwardFootState = true; + forwardAlertLeg = true; + backAlertLeg = true; + } + + if (forwardFootIsAhead && !forwardFootState && !backAlertLeg) + { + backFootState = true; + backAlertLeg = true; + forwardAlertLeg = true; + } + } + + internal void GroundCheck() + { + Transform rootTransform = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).transform; + Ray ray = new Ray(rootTransform.position, Vector3.down); + bool isHittingGround = Physics.Raycast(ray, out _, balanceHeight, 1 << groundLayer); + + if (!isHittingGround) + { + if (balanced) + { + balanced = false; + } + } + else if (ShouldSetBalanced()) + { + balanced = true; + } + + bool needsStateChange = (balanced == isRagdoll); + + if (!needsStateChange) + return; + + if (balanced) + { + DeactivateRagdoll(); + } + else + { + ActivateRagdoll(); + } + } + + private bool ShouldSetBalanced() + { + return !ragdollState.inAir && + !ragdollState.isJumping && + !ragdollState.reachRightAxisUsed && + !ragdollState.reachLeftAxisUsed && + !balanced && + jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT).velocity.magnitude < 1f && + autoGetUpWhenPossible; + } + + internal void HandleStepPrediction(float centerOfMassZPosition) + { + if (useStepPrediction) + { + PerformStepPrediction(centerOfMassZPosition); + } + else + { + ResetWalkCycle(); + } + } + + private void PerformStepPrediction(float centerOfMassZPosition) + { + if (!balanced) + return; + + if (!walkForward && !walkBackward) + { + stepRight = false; + stepLeft = false; + stepRTimer = 0; + stepLTimer = 0; + alertLegRight = false; + alertLegLeft = false; + } + + float rightFootZPosition = + jointHandler.GetConfigurableJointWithID(RagdollParts.RIGHT_FOOT).transform.position.z; + float leftFootZPosition = + jointHandler.GetConfigurableJointWithID(RagdollParts.LEFT_FOOT).transform.position.z; + + //TODO: Refactor to reduce amount of code here + if (centerOfMassZPosition < rightFootZPosition && centerOfMassZPosition < leftFootZPosition) + { + walkBackward = true; + } + else + { + if (!ragdollState.isKeyDown) + { + walkBackward = false; + } + } + + if (centerOfMassZPosition > rightFootZPosition && centerOfMassZPosition > leftFootZPosition) + { + walkForward = true; + } + else + { + if (!ragdollState.isKeyDown) + { + walkForward = false; + } + } + } + + private void ResetWalkCycle() + { + if (!walkForward && !walkBackward) + { + stepRight = false; + stepLeft = false; + stepRTimer = 0; + stepLTimer = 0; + alertLegRight = false; + alertLegLeft = false; + } + } + + } +} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs.meta b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollLocomotionController.cs.meta similarity index 100% rename from Assets/Scripts/ActiveRagdoll/RagdollLocomotionController.cs.meta rename to Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollLocomotionController.cs.meta diff --git a/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollState.cs b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollState.cs new file mode 100644 index 0000000..919978f --- /dev/null +++ b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollState.cs @@ -0,0 +1,20 @@ +namespace ActiveRagdoll +{ + public class RagdollState + { + public bool jumping; + public bool isJumping; + public bool inAir; + public bool punchingRight; + public bool punchingLeft; + internal float jumpingResetTimer; + internal float mouseYAxisArms; + internal float mouseXAxisArms; + internal float mouseYAxisBody; + internal bool isKeyDown; + internal bool moveAxisUsed; + internal bool jumpAxisUsed; + internal bool reachLeftAxisUsed; + internal bool reachRightAxisUsed; + } +} \ No newline at end of file diff --git a/Assets/Scripts/ActiveRagdoll/RagdollState.cs.meta b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollState.cs.meta similarity index 100% rename from Assets/Scripts/ActiveRagdoll/RagdollState.cs.meta rename to Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollState.cs.meta From 58081dc033626a3f545f7b589364fc8142d01bb3 Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 22:53:32 -0300 Subject: [PATCH 17/19] Cleanup unused data --- Assets/Scripts/ActiveRagdoll/RagdollController.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index d2dd91a..fab54b6 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -24,26 +24,18 @@ public class RagdollController : MonoBehaviour [Header("Actions")] public bool canPunch = true; public float punchForce = 15f; - //Hidden variables - - private readonly RagdollInputHandler inputHandler = new(); private RagdollLocomotionController locomotionController; private readonly RagdollState ragdollState = new(); - - [SerializeField] private Camera cam; private Vector3 CenterOfMassPoint; //TODO: Check usage - private RagdollDefaultTargetState defaultTargetState; - private static int groundLayer; private readonly WaitForSeconds punchDelayWaitTime = new(0.3f); private Rigidbody RightHandRigidBody => jointHandler.GetRigidBodyFromJoint(RagdollParts.RIGHT_HAND); private Rigidbody LeftHandRigidBody => jointHandler.GetRigidBodyFromJoint(RagdollParts.LEFT_HAND); private void Awake() { - groundLayer = LayerMask.NameToLayer("Ground"); inputHandler.Init(); locomotionController = new RagdollLocomotionController(jointHandler, ragdollState); defaultTargetState = new RagdollDefaultTargetState(jointHandler); From 46206ef36134a73652823620d6e445bfeb2de6cb Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 23:19:22 -0300 Subject: [PATCH 18/19] Add leg jointdrive setting for joint handler --- .../SubSystemHandlers/RagdollJointHandler.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollJointHandler.cs b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollJointHandler.cs index 8eff526..efee5e7 100644 --- a/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollJointHandler.cs +++ b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollJointHandler.cs @@ -126,5 +126,15 @@ public void GetCenterOfMass(out Vector3 CenterOfMassPoint, Transform centerOfMas CenterOfMassPoint = (massPositionDisplacement / totalMass); centerOfMass.position = CenterOfMassPoint; } + + public void SetJointAngularDrivesForLegs(in JointDrive jointDrive) + { + SetJointAngularDrives(RagdollParts.UPPER_RIGHT_LEG, in jointDrive); + SetJointAngularDrives(RagdollParts.LOWER_RIGHT_LEG, in jointDrive); + SetJointAngularDrives(RagdollParts.UPPER_LEFT_LEG, in jointDrive); + SetJointAngularDrives(RagdollParts.LOWER_LEFT_LEG, in jointDrive); + SetJointAngularDrives(RagdollParts.RIGHT_FOOT, in jointDrive); + SetJointAngularDrives(RagdollParts.LEFT_FOOT, in jointDrive); + } } } \ No newline at end of file From 779ffb3d0030c3738c590e351fd308858e1ebd0b Mon Sep 17 00:00:00 2001 From: Eruandou Date: Mon, 21 Aug 2023 23:19:34 -0300 Subject: [PATCH 19/19] Expand locomotion controller --- .../ActiveRagdoll/RagdollController.cs | 336 +----------------- .../RagdollLocomotionController.cs | 322 ++++++++++++++++- 2 files changed, 328 insertions(+), 330 deletions(-) diff --git a/Assets/Scripts/ActiveRagdoll/RagdollController.cs b/Assets/Scripts/ActiveRagdoll/RagdollController.cs index fab54b6..7b014e2 100644 --- a/Assets/Scripts/ActiveRagdoll/RagdollController.cs +++ b/Assets/Scripts/ActiveRagdoll/RagdollController.cs @@ -8,24 +8,15 @@ public class RagdollController : MonoBehaviour { [SerializeField] private RagdollJointHandler jointHandler; - [SerializeField] private Transform centerOfMass; [SerializeField] private RagdollImpactHandler impactHandler; - [Header("Movement Properties")] public bool forwardIsCameraDirection = true; - public float moveSpeed = 10f; - public float turnSpeed = 6f; - public float jumpForce = 18f; - - public float StepDuration = 0.2f; - public float StepHeight = 1.7f; - public float FeetMountForce = 25f; - + [SerializeField] private RagdollLocomotionController locomotionController; + [SerializeField] private bool forwardIsCameraDirection = true; + [SerializeField] private Transform centerOfMass; + [SerializeField] private float turnSpeed = 6f; [Header("Reach Properties")] public float reachSensitivity = 25f; - [Header("Actions")] public bool canPunch = true; public float punchForce = 15f; - private readonly RagdollInputHandler inputHandler = new(); - private RagdollLocomotionController locomotionController; private readonly RagdollState ragdollState = new(); [SerializeField] private Camera cam; private Vector3 CenterOfMassPoint; //TODO: Check usage @@ -37,8 +28,8 @@ public class RagdollController : MonoBehaviour private void Awake() { inputHandler.Init(); - locomotionController = new RagdollLocomotionController(jointHandler, ragdollState); defaultTargetState = new RagdollDefaultTargetState(jointHandler); + locomotionController.Init(jointHandler, ragdollState, defaultTargetState); SetupHandContacts(); SetupFeetContacts(); SetupRagdollImpactContacts(); @@ -75,7 +66,7 @@ private void Update() { if (!ragdollState.inAir) { - PlayerMovement(); + locomotionController.PlayerMovement(forwardIsCameraDirection, inputHandler.MovementAxis); if (canPunch) { @@ -93,10 +84,10 @@ private void Update() private void FixedUpdate() { - PerformWalking(); + locomotionController.PerformWalking(); PerformPlayerRotation(); ResetPlayerPose(); - PerformPlayerGetUpJumping(); + locomotionController.PerformPlayerGetUpJumping(inputHandler.JumpValue); } private void PerformPlayerRotation() @@ -174,177 +165,6 @@ private bool CanResetPoseAfterLanding() return ragdollState.inAir && !ragdollState.isJumping && !ragdollState.jumping; } - private void PerformPlayerGetUpJumping() - { - if (inputHandler.JumpValue > 0) - { - if (!ragdollState.jumpAxisUsed) - { - if (locomotionController.balanced && !ragdollState.inAir) - { - ragdollState.jumping = true; - } - - else if (!locomotionController.balanced) - { - locomotionController.DeactivateRagdoll(); - } - } - - ragdollState.jumpAxisUsed = true; - } - - else - { - ragdollState.jumpAxisUsed = false; - } - - - if (ragdollState.jumping) - { - ragdollState.isJumping = true; - - Rigidbody rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); - rootRigidbody.velocity = rootRigidbody.velocity.ModifyY(rootRigidbody.transform.up.y * jumpForce); - } - - if (ragdollState.isJumping) - { - ragdollState.jumpingResetTimer += Time.fixedDeltaTime; - - if (ragdollState.jumpingResetTimer > 0.2f) - { - ragdollState.jumpingResetTimer = 0.0f; - ragdollState.jumping = false; - ragdollState.isJumping = false; - ragdollState.inAir = true; - } - } - } - - private void PlayerMovement() - { - if (forwardIsCameraDirection) - { - MoveInCameraDirection(); - } - else - { - MoveInOwnDirection(); - } - } - - private void MoveInCameraDirection() - { - var direction = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).transform.rotation * - new Vector3(inputHandler.MovementAxis.x, 0.0f, inputHandler.MovementAxis.y); - direction.y = 0f; - Rigidbody rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); - var velocity = rootRigidbody.velocity; - rootRigidbody.velocity = Vector3.Lerp(velocity, - (direction * moveSpeed) + new Vector3(0, velocity.y, 0), 0.8f); - - if (inputHandler.MovementAxis.x != 0 || inputHandler.MovementAxis.y != 0 && locomotionController.balanced) - { - StartWalkingForward(); - } - else if (inputHandler.MovementAxis is { x: 0, y: 0 }) - { - StopWalkingForward(); - } - } - - private void StartWalkingForward() - { - if (!locomotionController.walkForward && !ragdollState.moveAxisUsed) - { - locomotionController.walkForward = true; - ragdollState.moveAxisUsed = true; - ragdollState.isKeyDown = true; - } - } - - private void StopWalkingForward() - { - if (locomotionController.walkForward && ragdollState.moveAxisUsed) - { - locomotionController.walkForward = false; - ragdollState.moveAxisUsed = false; - ragdollState.isKeyDown = false; - } - } - - private void MoveInOwnDirection() - { - if (inputHandler.MovementAxis.y != 0) - { - var rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); - var v3 = rootRigidbody.transform.forward * (inputHandler.MovementAxis.y * moveSpeed); - v3.y = rootRigidbody.velocity.y; - rootRigidbody.velocity = v3; - } - - if (inputHandler.MovementAxis.y > 0) - { - StartWalkingForwardInOwnDirection(); - } - else if (inputHandler.MovementAxis.y < 0) - { - StartWalkingBackward(); - } - else - { - StopWalking(); - } - } - - private void StartWalkingForwardInOwnDirection() => - SetWalkMovingState(() => (!locomotionController.walkForward && !ragdollState.moveAxisUsed), true, false, - true, true, - jointHandler.PoseOn); - - private void StartWalkingBackward() => - SetWalkMovingState(() => !locomotionController.walkBackward && !ragdollState.moveAxisUsed, false, true, - true, true, - jointHandler.PoseOn); - - private void StopWalking() => SetWalkMovingState( - () => locomotionController.walkForward || - locomotionController.walkBackward && ragdollState.moveAxisUsed, false, false, - false, false, jointHandler.DriveOff); - - private void SetWalkMovingState(Func activationCondition, bool walkForwardSetState, bool walkBackwardSetState, - bool isMoveAxisUsed, bool isKeyCurrentlyDown, in JointDrive legsJointDrive) - { - if (activationCondition.Invoke()) - { - InternalChangeWalkState(walkForwardSetState, walkBackwardSetState, isMoveAxisUsed, isKeyCurrentlyDown, - in legsJointDrive); - } - } - - private void InternalChangeWalkState(bool walkForward, bool walkBackward, bool isMoveAxisUsed, - bool isKeyCurrentlyDown, - in JointDrive legsJointDrive) - { - locomotionController.walkForward = walkForward; - locomotionController.walkBackward = walkBackward; - ragdollState.moveAxisUsed = isMoveAxisUsed; - ragdollState.isKeyDown = isKeyCurrentlyDown; - if (locomotionController.isRagdoll) - SetJointAngularDrivesForLegs(in legsJointDrive); - } - - private void SetJointAngularDrivesForLegs(in JointDrive jointDrive) - { - jointHandler.SetJointAngularDrives(RagdollParts.UPPER_RIGHT_LEG, in jointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.LOWER_RIGHT_LEG, in jointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.UPPER_LEFT_LEG, in jointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.LOWER_LEFT_LEG, in jointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.RIGHT_FOOT, in jointDrive); - jointHandler.SetJointAngularDrives(RagdollParts.LEFT_FOOT, in jointDrive); - } - private void PlayerReach() { ragdollState.mouseYAxisBody = @@ -478,146 +298,6 @@ private void HandleRightPunch() => HandlePunch(ref ragdollState.punchingRight, i RagdollParts.LOWER_RIGHT_ARM, RightHandRigidBody, () => defaultTargetState.UpperRightArmTarget, () => defaultTargetState.LowerLeftArmTarget); - private void PerformWalking() - { - if (ragdollState.inAir) - return; - - if (locomotionController.walkForward) - { - WalkForwards(); - } - - if (locomotionController.walkBackward) - { - WalkBackwards(); - } - - if (locomotionController.stepRight) - { - TakeStepRight(); - } - else - { - ResetStepRight(); - } - - if (locomotionController.stepLeft) - { - TakeStepLeft(); - } - else - { - ResetStepLeft(); - } - } - - private void ResetStepLeft() => - ResetStep(RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, defaultTargetState.UpperLeftLegTarget, - defaultTargetState.LowerLeftLegTarget, 7f, 18f); - - private void ResetStepRight() => ResetStep(RagdollParts.UPPER_RIGHT_LEG, RagdollParts.LOWER_RIGHT_LEG, - defaultTargetState.UpperRightLegTarget, - defaultTargetState.LowerRightLegTarget, 8f, 17f); - - private void ResetStep(string upperLegLabel, - string lowerLegLabel, - Quaternion upperLegTarget, - Quaternion lowerLegTarget, - float upperLegLerpMultiplier, - float lowerLegLerpMultiplier) - { - jointHandler.GetConfigurableJointWithID(upperLegLabel).targetRotation = Quaternion.Lerp( - jointHandler.GetJointTargetRotation(upperLegLabel), upperLegTarget, - upperLegLerpMultiplier * Time.fixedDeltaTime); - jointHandler.GetConfigurableJointWithID(lowerLegLabel).targetRotation = Quaternion.Lerp( - jointHandler.GetJointTargetRotation(lowerLegLabel), lowerLegTarget, - lowerLegLerpMultiplier * Time.fixedDeltaTime); - - Vector3 feetForce = -Vector3.up * (FeetMountForce * Time.deltaTime); - jointHandler.GetRigidBodyFromJoint(RagdollParts.RIGHT_FOOT).AddForce(feetForce, ForceMode.Impulse); - jointHandler.GetRigidBodyFromJoint(RagdollParts.LEFT_FOOT).AddForce(feetForce, ForceMode.Impulse); - } - - private void TakeStepLeft() => TakeStep(ref locomotionController.stepLTimer, RagdollParts.LEFT_FOOT, - ref locomotionController.stepLeft, ref locomotionController.stepRight, - RagdollParts.UPPER_LEFT_LEG, - RagdollParts.LOWER_LEFT_LEG, RagdollParts.UPPER_RIGHT_LEG); - - private void TakeStepRight() => TakeStep(ref locomotionController.stepRTimer, RagdollParts.RIGHT_FOOT, - ref locomotionController.stepRight, ref locomotionController.stepLeft, - RagdollParts.UPPER_RIGHT_LEG, - RagdollParts.LOWER_RIGHT_LEG, RagdollParts.UPPER_LEFT_LEG); - - private void TakeStep(ref float stepTimer, - string footLabel, - ref bool stepFootState, - ref bool oppositeStepFootState, - string upperJointLabel, - string lowerJointLabel, - string upperOppositeJointLabel) - { - stepTimer += Time.fixedDeltaTime; - jointHandler.GetRigidBodyFromJoint(footLabel) - .AddForce(-Vector3.up * (FeetMountForce * Time.deltaTime), ForceMode.Impulse); - - var upperLegJoint = jointHandler.GetConfigurableJointWithID(upperJointLabel); - var upperLegJointTargetRotation = upperLegJoint.targetRotation; - - var lowerLegJoint = jointHandler.GetConfigurableJointWithID(lowerJointLabel); - var lowerLegJointTargetRotation = lowerLegJoint.targetRotation; - - var upperOppositeLegJoint = jointHandler.GetConfigurableJointWithID(upperOppositeJointLabel); - var upperOppositeLegJointTargetRotation = upperOppositeLegJoint.targetRotation; - - bool isWalking = locomotionController.walkForward || locomotionController.walkBackward; - - if (locomotionController.walkForward) - { - upperLegJointTargetRotation = upperLegJointTargetRotation.DisplaceX(0.09f * StepHeight); - lowerLegJointTargetRotation = lowerLegJointTargetRotation.DisplaceX(-0.09f * StepHeight * 2); - upperOppositeLegJointTargetRotation = - upperOppositeLegJointTargetRotation.DisplaceX(-0.12f * StepHeight / 2); - } - - if (locomotionController.walkBackward) - { - //TODO: Is this necessary for something? It's multiplying by 0. - upperLegJointTargetRotation = upperLegJointTargetRotation.DisplaceX(-0.00f * StepHeight); - lowerLegJointTargetRotation = lowerLegJointTargetRotation.DisplaceX(-0.07f * StepHeight * 2); - upperOppositeLegJointTargetRotation = upperOppositeLegJointTargetRotation.DisplaceX(0.02f * StepHeight / 2); - } - - if (isWalking) - { - upperLegJoint.targetRotation = upperLegJointTargetRotation; - lowerLegJoint.targetRotation = lowerLegJointTargetRotation; - upperOppositeLegJoint.targetRotation = upperOppositeLegJointTargetRotation; - } - - - if (stepTimer <= StepDuration) - return; - - stepTimer = 0; - stepFootState = false; - - if (isWalking) - { - oppositeStepFootState = true; - } - } - - private void WalkBackwards() => locomotionController.Walk(RagdollParts.LEFT_FOOT, RagdollParts.RIGHT_FOOT, - ref locomotionController.stepLeft, ref locomotionController.stepRight, - ref locomotionController.alertLegLeft, - ref locomotionController.alertLegRight); - - private void WalkForwards() => locomotionController.Walk(RagdollParts.RIGHT_FOOT, RagdollParts.LEFT_FOOT, - ref locomotionController.stepRight, ref locomotionController.stepLeft, - ref locomotionController.alertLegRight, - ref locomotionController.alertLegLeft); - [MethodImpl(MethodImplOptions.AggressiveInlining)] private void UpdateCenterOfMass() { diff --git a/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollLocomotionController.cs b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollLocomotionController.cs index 3516132..4df3650 100644 --- a/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollLocomotionController.cs +++ b/Assets/Scripts/ActiveRagdoll/SubSystemHandlers/RagdollLocomotionController.cs @@ -1,9 +1,20 @@ +using System; using UnityEngine; +using Utils; namespace ActiveRagdoll { + [Serializable] public class RagdollLocomotionController { + [Header("Movement Properties")] [SerializeField] + private float moveSpeed = 10f; + + [SerializeField] private float jumpForce = 18f; + [SerializeField] private float StepDuration = 0.2f; + [SerializeField] private float StepHeight = 1.7f; + [SerializeField] private float FeetMountForce = 25f; + internal float stepRTimer; internal float stepLTimer; internal bool walkForward; @@ -26,12 +37,15 @@ public class RagdollLocomotionController private static int groundLayer; private const string GROUND_LAYER_NAME = "Ground"; private RagdollState ragdollState; + private RagdollDefaultTargetState defaultTargetState; - public RagdollLocomotionController(RagdollJointHandler jointsHandler, RagdollState state) + public void Init(RagdollJointHandler jointsHandler, RagdollState state, + RagdollDefaultTargetState defaultTargetState) { jointHandler = jointsHandler; groundLayer = LayerMask.NameToLayer(GROUND_LAYER_NAME); ragdollState = state; + this.defaultTargetState = defaultTargetState; } private void SetRagdollState(bool shouldRagdoll, ref JointDrive rootJointDrive, @@ -214,6 +228,310 @@ private void ResetWalkCycle() alertLegLeft = false; } } - + + internal void PerformPlayerGetUpJumping(float jumpValue) + { + if (jumpValue > 0) + { + if (!ragdollState.jumpAxisUsed) + { + if (balanced && !ragdollState.inAir) + { + ragdollState.jumping = true; + } + + else if (!balanced) + { + DeactivateRagdoll(); + } + } + + ragdollState.jumpAxisUsed = true; + } + + else + { + ragdollState.jumpAxisUsed = false; + } + + + if (ragdollState.jumping) + { + ragdollState.isJumping = true; + + Rigidbody rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); + rootRigidbody.velocity = rootRigidbody.velocity.ModifyY(rootRigidbody.transform.up.y * jumpForce); + } + + if (ragdollState.isJumping) + { + ragdollState.jumpingResetTimer += Time.fixedDeltaTime; + + if (ragdollState.jumpingResetTimer > 0.2f) + { + ragdollState.jumpingResetTimer = 0.0f; + ragdollState.jumping = false; + ragdollState.isJumping = false; + ragdollState.inAir = true; + } + } + } + + internal void PerformWalking() + { + if (ragdollState.inAir) + return; + + if (walkForward) + { + WalkForwards(); + } + + if (walkBackward) + { + WalkBackwards(); + } + + if (stepRight) + { + TakeStepRight(); + } + else + { + ResetStepRight(); + } + + if (stepLeft) + { + TakeStepLeft(); + } + else + { + ResetStepLeft(); + } + } + + private void WalkBackwards() => Walk(RagdollParts.LEFT_FOOT, RagdollParts.RIGHT_FOOT, + ref stepLeft, ref stepRight, + ref alertLegLeft, + ref alertLegRight); + + private void WalkForwards() => Walk(RagdollParts.RIGHT_FOOT, RagdollParts.LEFT_FOOT, + ref stepRight, ref stepLeft, + ref alertLegRight, + ref alertLegLeft); + + private void TakeStepLeft() => TakeStep(ref stepLTimer, RagdollParts.LEFT_FOOT, + ref stepLeft, ref stepRight, + RagdollParts.UPPER_LEFT_LEG, + RagdollParts.LOWER_LEFT_LEG, RagdollParts.UPPER_RIGHT_LEG); + + private void TakeStepRight() => TakeStep(ref stepRTimer, RagdollParts.RIGHT_FOOT, + ref stepRight, ref stepLeft, + RagdollParts.UPPER_RIGHT_LEG, + RagdollParts.LOWER_RIGHT_LEG, RagdollParts.UPPER_LEFT_LEG); + + private void TakeStep(ref float stepTimer, + string footLabel, + ref bool stepFootState, + ref bool oppositeStepFootState, + string upperJointLabel, + string lowerJointLabel, + string upperOppositeJointLabel) + { + stepTimer += Time.fixedDeltaTime; + jointHandler.GetRigidBodyFromJoint(footLabel) + .AddForce(-Vector3.up * (FeetMountForce * Time.deltaTime), ForceMode.Impulse); + + var upperLegJoint = jointHandler.GetConfigurableJointWithID(upperJointLabel); + var upperLegJointTargetRotation = upperLegJoint.targetRotation; + + var lowerLegJoint = jointHandler.GetConfigurableJointWithID(lowerJointLabel); + var lowerLegJointTargetRotation = lowerLegJoint.targetRotation; + + var upperOppositeLegJoint = jointHandler.GetConfigurableJointWithID(upperOppositeJointLabel); + var upperOppositeLegJointTargetRotation = upperOppositeLegJoint.targetRotation; + + bool isWalking = walkForward || walkBackward; + + if (walkForward) + { + upperLegJointTargetRotation = upperLegJointTargetRotation.DisplaceX(0.09f * StepHeight); + lowerLegJointTargetRotation = lowerLegJointTargetRotation.DisplaceX(-0.09f * StepHeight * 2); + upperOppositeLegJointTargetRotation = + upperOppositeLegJointTargetRotation.DisplaceX(-0.12f * StepHeight / 2); + } + + if (walkBackward) + { + //TODO: Is this necessary for something? It's multiplying by 0. + upperLegJointTargetRotation = upperLegJointTargetRotation.DisplaceX(-0.00f * StepHeight); + lowerLegJointTargetRotation = lowerLegJointTargetRotation.DisplaceX(-0.07f * StepHeight * 2); + upperOppositeLegJointTargetRotation = + upperOppositeLegJointTargetRotation.DisplaceX(0.02f * StepHeight / 2); + } + + if (isWalking) + { + upperLegJoint.targetRotation = upperLegJointTargetRotation; + lowerLegJoint.targetRotation = lowerLegJointTargetRotation; + upperOppositeLegJoint.targetRotation = upperOppositeLegJointTargetRotation; + } + + + if (stepTimer <= StepDuration) + return; + + stepTimer = 0; + stepFootState = false; + + if (isWalking) + { + oppositeStepFootState = true; + } + } + + private void ResetStepLeft() => + ResetStep(RagdollParts.UPPER_LEFT_LEG, RagdollParts.LOWER_LEFT_LEG, + defaultTargetState.UpperLeftLegTarget, defaultTargetState.LowerLeftLegTarget, + 7f, 18f); + + private void ResetStepRight() => ResetStep( + RagdollParts.UPPER_RIGHT_LEG, RagdollParts.LOWER_RIGHT_LEG, + defaultTargetState.UpperRightLegTarget, defaultTargetState.LowerRightLegTarget, + 8f, 17f); + + private void ResetStep(string upperLegLabel, + string lowerLegLabel, + Quaternion upperLegTarget, + Quaternion lowerLegTarget, + float upperLegLerpMultiplier, + float lowerLegLerpMultiplier) + { + jointHandler.GetConfigurableJointWithID(upperLegLabel).targetRotation = Quaternion.Lerp( + jointHandler.GetJointTargetRotation(upperLegLabel), upperLegTarget, + upperLegLerpMultiplier * Time.fixedDeltaTime); + jointHandler.GetConfigurableJointWithID(lowerLegLabel).targetRotation = Quaternion.Lerp( + jointHandler.GetJointTargetRotation(lowerLegLabel), lowerLegTarget, + lowerLegLerpMultiplier * Time.fixedDeltaTime); + + Vector3 feetForce = -Vector3.up * (FeetMountForce * Time.deltaTime); + jointHandler.GetRigidBodyFromJoint(RagdollParts.RIGHT_FOOT).AddForce(feetForce, ForceMode.Impulse); + jointHandler.GetRigidBodyFromJoint(RagdollParts.LEFT_FOOT).AddForce(feetForce, ForceMode.Impulse); + } + + internal void PlayerMovement(bool forwardIsCameraDirection, Vector2 movementVector) + { + if (forwardIsCameraDirection) + { + MoveInCameraDirection(movementVector); + } + else + { + MoveInOwnDirection(movementVector); + } + } + + private void MoveInCameraDirection(Vector2 movementVector) + { + var direction = jointHandler.GetRagdollJointWithID(RagdollParts.ROOT).transform.rotation * + new Vector3(movementVector.x, 0.0f, movementVector.y); + direction.y = 0f; + Rigidbody rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); + var velocity = rootRigidbody.velocity; + rootRigidbody.velocity = Vector3.Lerp(velocity, + (direction * moveSpeed) + new Vector3(0, velocity.y, 0), 0.8f); + + if (movementVector.x != 0 || movementVector.y != 0 && balanced) + { + StartWalkingForward(); + } + else if (movementVector is { x: 0, y: 0 }) + { + StopWalkingForward(); + } + } + + private void StartWalkingForward() + { + if (walkForward || ragdollState.moveAxisUsed) + return; + + walkForward = true; + ragdollState.moveAxisUsed = true; + ragdollState.isKeyDown = true; + } + + private void StopWalkingForward() + { + if (!walkForward || !ragdollState.moveAxisUsed) + return; + + walkForward = false; + ragdollState.moveAxisUsed = false; + ragdollState.isKeyDown = false; + } + + private void MoveInOwnDirection(Vector2 movementVector) + { + if (movementVector.y != 0) + { + var rootRigidbody = jointHandler.GetRigidBodyFromJoint(RagdollParts.ROOT); + var v3 = rootRigidbody.transform.forward * (movementVector.y * moveSpeed); + v3.y = rootRigidbody.velocity.y; + rootRigidbody.velocity = v3; + } + + if (movementVector.y > 0) + { + StartWalkingForwardInOwnDirection(); + } + else if (movementVector.y < 0) + { + StartWalkingBackward(); + } + else + { + StopWalking(); + } + } + + private void StartWalkingForwardInOwnDirection() => + SetWalkMovingState(() => (!walkForward && !ragdollState.moveAxisUsed), true, false, + true, true, + jointHandler.PoseOn); + + private void StartWalkingBackward() => + SetWalkMovingState(() => !walkBackward && !ragdollState.moveAxisUsed, false, true, + true, true, + jointHandler.PoseOn); + + private void StopWalking() => SetWalkMovingState( + () => walkForward || + walkBackward && ragdollState.moveAxisUsed, false, false, + false, false, jointHandler.DriveOff); + + private void SetWalkMovingState(Func activationCondition, bool walkForwardSetState, + bool walkBackwardSetState, + bool isMoveAxisUsed, bool isKeyCurrentlyDown, in JointDrive legsJointDrive) + { + if (activationCondition.Invoke()) + { + InternalChangeWalkState(walkForwardSetState, walkBackwardSetState, isMoveAxisUsed, isKeyCurrentlyDown, + in legsJointDrive); + } + } + + private void InternalChangeWalkState(bool walkForwardState, bool walkBackwardState, bool isMoveAxisUsed, + bool isKeyCurrentlyDown, + in JointDrive legsJointDrive) + { + this.walkForward = walkForwardState; + this.walkBackward = walkBackwardState; + ragdollState.moveAxisUsed = isMoveAxisUsed; + ragdollState.isKeyDown = isKeyCurrentlyDown; + if (isRagdoll) + jointHandler.SetJointAngularDrivesForLegs(in legsJointDrive); + } } } \ No newline at end of file