From 0b3bf59c5c1b56ea0f193b87d6d4680dfc75318c Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 14 Aug 2025 18:39:36 -0400 Subject: [PATCH] maint: cleaned up some files and tweaked some movement --- Assets/Player/Graphs/GroundedCheckBT.asset | 2 +- .../Graph Tasks/ChangeMovementSettings.cs | 70 ++++++---- Assets/Scripts/Core/LockOnManager.cs | 4 +- Assets/Scripts/Core/ValueGroup.cs | 3 +- Assets/Scripts/Player/UnitMovementHandler.cs | 127 ++++++++++++------ 5 files changed, 132 insertions(+), 74 deletions(-) diff --git a/Assets/Player/Graphs/GroundedCheckBT.asset b/Assets/Player/Graphs/GroundedCheckBT.asset index bee460a..48f8634 100644 --- a/Assets/Player/Graphs/GroundedCheckBT.asset +++ b/Assets/Player/Graphs/GroundedCheckBT.asset @@ -12,7 +12,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 7a686a47eee2fa44cb0a34b5d86e4d5e, type: 3} m_Name: GroundedCheckBT m_EditorClassIdentifier: - _serializedGraph: '{"type":"NodeCanvas.BehaviourTrees.BehaviourTree","nodes":[{"repeaterMode":2,"repeatTimes":{"_value":1},"_position":{"x":476.0,"y":327.0},"$type":"NodeCanvas.BehaviourTrees.Repeater","$id":"0"},{"_position":{"x":550.0,"y":433.0},"$type":"NodeCanvas.BehaviourTrees.Sequencer","$id":"1"},{"_condition":{"valueA":{"_name":"isGrounded","_targetVariableID":"09b3259f-8b50-439a-ab86-ea3d8a4916f1"},"valueB":{},"$type":"NodeCanvas.Tasks.Conditions.CheckBoolean"},"_position":{"x":458.0,"y":521.0},"$type":"NodeCanvas.BehaviourTrees.WaitUntil","$id":"2"},{"_position":{"x":522.0,"y":632.0},"$type":"NodeCanvas.BehaviourTrees.Sequencer","$id":"3"},{"_action":{"airMoveDirection":{"_name":"airMoveDirection","_targetVariableID":"2fc91db6-09dd-4a1f-9195-426a0c44de31"},"inputVector3":{"_name":"groundMoveDirection","_targetVariableID":"a2c4fe25-9549-4bdd-8cec-2dbfc4a8856f"},"$type":"NodeCanvas.Tasks.Actions.SetAirMovement"},"_position":{"x":399.0,"y":775.0},"$type":"NodeCanvas.BehaviourTrees.ActionNode","$id":"4"},{"_action":{"airMoveDirection":{"_name":"groundMoveDirection","_targetVariableID":"a2c4fe25-9549-4bdd-8cec-2dbfc4a8856f"},"inputVector3":{},"$type":"NodeCanvas.Tasks.Actions.SetAirMovement"},"_position":{"x":576.0,"y":776.0},"$type":"NodeCanvas.BehaviourTrees.ActionNode","$id":"5"},{"_condition":{"valueA":{"_name":"isGrounded","_targetVariableID":"09b3259f-8b50-439a-ab86-ea3d8a4916f1"},"valueB":{"_value":true},"$type":"NodeCanvas.Tasks.Conditions.CheckBoolean"},"_position":{"x":715.0,"y":523.0},"$type":"NodeCanvas.BehaviourTrees.WaitUntil","$id":"6"}],"connections":[{"_sourceNode":{"$ref":"0"},"_targetNode":{"$ref":"1"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"1"},"_targetNode":{"$ref":"2"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"1"},"_targetNode":{"$ref":"6"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"2"},"_targetNode":{"$ref":"3"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"3"},"_targetNode":{"$ref":"4"},"_isDisabled":true,"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"3"},"_targetNode":{"$ref":"5"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"}],"canvasGroups":[],"localBlackboard":{"_variables":{}},"derivedData":{"repeat":true,"$type":"NodeCanvas.BehaviourTrees.BehaviourTree+DerivedSerializationData"}}' + _serializedGraph: '{"type":"NodeCanvas.BehaviourTrees.BehaviourTree","nodes":[{"repeaterMode":2,"repeatTimes":{"_value":1},"_position":{"x":476.0,"y":327.0},"$type":"NodeCanvas.BehaviourTrees.Repeater","$id":"0"},{"_position":{"x":550.0,"y":433.0},"$type":"NodeCanvas.BehaviourTrees.Sequencer","$id":"1"},{"_condition":{"valueA":{"_name":"isGrounded","_targetVariableID":"09b3259f-8b50-439a-ab86-ea3d8a4916f1"},"valueB":{},"$type":"NodeCanvas.Tasks.Conditions.CheckBoolean"},"_position":{"x":458.0,"y":521.0},"$type":"NodeCanvas.BehaviourTrees.WaitUntil","$id":"2"},{"_position":{"x":478.0,"y":709.0},"$type":"NodeCanvas.BehaviourTrees.Sequencer","$id":"3"},{"_action":{"airMoveDirection":{"_name":"airMoveDirection","_targetVariableID":"2fc91db6-09dd-4a1f-9195-426a0c44de31"},"inputVector3":{"_name":"groundMoveDirection","_targetVariableID":"a2c4fe25-9549-4bdd-8cec-2dbfc4a8856f"},"$type":"NodeCanvas.Tasks.Actions.SetAirMovement"},"_position":{"x":294.0,"y":816.0},"$type":"NodeCanvas.BehaviourTrees.ActionNode","$id":"4"},{"_action":{"airMoveDirection":{"_name":"groundMoveDirection","_targetVariableID":"a2c4fe25-9549-4bdd-8cec-2dbfc4a8856f"},"inputVector3":{},"$type":"NodeCanvas.Tasks.Actions.SetAirMovement"},"_position":{"x":471.0,"y":817.0},"$type":"NodeCanvas.BehaviourTrees.ActionNode","$id":"5"},{"_condition":{"valueA":{"_name":"isGrounded","_targetVariableID":"09b3259f-8b50-439a-ab86-ea3d8a4916f1"},"valueB":{"_value":true},"$type":"NodeCanvas.Tasks.Conditions.CheckBoolean"},"_position":{"x":715.0,"y":523.0},"$type":"NodeCanvas.BehaviourTrees.WaitUntil","$id":"6"}],"connections":[{"_sourceNode":{"$ref":"0"},"_targetNode":{"$ref":"1"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"1"},"_targetNode":{"$ref":"2"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"1"},"_targetNode":{"$ref":"6"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"2"},"_targetNode":{"$ref":"3"},"_isDisabled":true,"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"3"},"_targetNode":{"$ref":"4"},"_isDisabled":true,"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"3"},"_targetNode":{"$ref":"5"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"}],"canvasGroups":[],"localBlackboard":{"_variables":{}},"derivedData":{"repeat":true,"$type":"NodeCanvas.BehaviourTrees.BehaviourTree+DerivedSerializationData"}}' _objectReferences: [] _graphSource: _version: 3.31 diff --git a/Assets/Scripts/Core/Graph Tasks/ChangeMovementSettings.cs b/Assets/Scripts/Core/Graph Tasks/ChangeMovementSettings.cs index 3e5a969..d391594 100644 --- a/Assets/Scripts/Core/Graph Tasks/ChangeMovementSettings.cs +++ b/Assets/Scripts/Core/Graph Tasks/ChangeMovementSettings.cs @@ -8,19 +8,27 @@ namespace Reset.Core { [Category("Reset")] [Description("Commits movement unit changes to the handler.")] public class ChangeMovementSettings : ActionTask { - //Use for initialization. This is called only once in the lifetime of the task. // Move Speed [ParadoxNotion.Design.Header("Speed")] - public FloatValueGroup moveSpeedTarget = new (newLabel: "Move Speed"); + public FloatValueGroup moveSpeed = new (newLabel: "Move Speed"); public FloatValueGroup moveSpeedSoothing = new (newLabel: "Move Speed Smoothing"); - [ParadoxNotion.Design.Header("Speed")] - public Vector3ValueGroup feedNewDirection = new Vector3ValueGroup("Feed New Direction"); - public float newDirectionPower; - + [ParadoxNotion.Design.Header("Direction")] + public FloatValueGroup airDirectionDecay = new FloatValueGroup("Air Direction Decay"); public FloatValueGroup accelerationSmoothing = new (newLabel: "Acceleration Smoothing"); public FloatValueGroup deaccelerationSmoothing = new (newLabel: "Deacceleration Smoothing"); - public CurveValueGroup deaccelerationCurve = new (newLabel: "Deacceleration Curve"); + // public CurveValueGroup deaccelerationCurve = new (newLabel: "Deacceleration Curve"); // Currently unused, may return + + // Direction + [Space(5)] + public Vector3ValueGroup feedNewDirection = new Vector3ValueGroup("Feed New Direction"); + public float newDirectionStrength; + [Space(5)] + public Vector2 addDirectionFromInput; + public float addInputStrength; + [SliderField(0,1)] + public float addInputPriorty; + // Jumping [ParadoxNotion.Design.Header("Jumping")] @@ -37,16 +45,16 @@ namespace Reset.Core { // Rotation [ParadoxNotion.Design.Header("Rotation")] public EnumValueGroup rotateFacing = new EnumValueGroup("Facing Direction", PlayerFacingDirection.TowardsTarget); - public FloatValueGroup rotationSpeedTarget = new (newLabel: "Rotation Speed"); + public FloatValueGroup rotationSpeed = new (newLabel: "Rotation Speed"); public FloatValueGroup rotationSmoothing = new (newLabel: "Rotation Smoothing"); + public FloatValueGroup rotationInputBlending = new("Rotation Input Blending"); + [Space(5)] public BBParameter feedNewRotation; public BBParameter feedRelativeTo; public Space rotationRelativeSpace; private Vector3 feedDir; - - //Return null if init was successfull. Return an error string otherwise protected override string OnInit() { return null; } @@ -55,35 +63,49 @@ namespace Reset.Core { //Call EndAction() to mark the action as finished, either in success or failure. //EndAction can be called from anywhere. protected override void OnExecute() { + // Direction + UpdateFloatValue(airDirectionDecay, ref agent.data.airDirectionDecay, ref agent.data.airDirectionDecay); UpdateFloatValue(accelerationSmoothing, ref agent.data.accelerationSmoothing, ref agent.defaultData.accelerationSmoothing); UpdateFloatValue(deaccelerationSmoothing, ref agent.data.deaccelerationSmoothing, ref agent.defaultData.deaccelerationSmoothing); - UpdateCurveProperty(deaccelerationCurve, ref agent.data.deaccelerationCurve, ref agent.defaultData.deaccelerationCurve); + // UpdateCurveProperty(deaccelerationCurve, ref agent.data.deaccelerationCurve, ref agent.defaultData.deaccelerationCurve); // Currently unused, may return - UpdateFloatValue(moveSpeedTarget, ref agent.data.moveSpeedTarget, ref agent.defaultData.moveSpeedTarget); + // Direction from value + // Check that feedDir is not changed + UpdateVector3Value(feedNewDirection, ref feedDir, ref feedDir); + + // If there's a direciton add it to the player for a frame + if (feedDir != Vector3.zero) { + agent.AddToCurrentDirection(agent.transform.rotation * feedDir.normalized, newDirectionStrength); + // Reset the fed direction after it's added so future runs don't have + feedDir = Vector3.zero; + } + + // Direction from controller input + if (addDirectionFromInput != Vector2.zero){ + agent.OverwriteDirectionFromInput(new Vector3(addDirectionFromInput.x, addDirectionFromInput.y), addInputPriorty, addInputStrength); + } + + // Move Speed + UpdateFloatValue(moveSpeed, ref agent.data.moveSpeed, ref agent.defaultData.moveSpeed); UpdateFloatValue(moveSpeedSoothing, ref agent.data.moveSpeedSoothing, ref agent.defaultData.moveSpeedSoothing); + // Jump UpdateFloatValue(jumpPower, ref agent.data.jumpPower, ref agent.defaultData.jumpPower); UpdateFloatValue(jumpPowerDecay, ref agent.data.jumpPowerDecay, ref agent.defaultData.jumpPowerDecay); + // Gravity UpdateFloatValue(gravityPower, ref agent.data.gravityPower, ref agent.defaultData.gravityPower); UpdateFloatValue(gravityMax, ref agent.data.gravityMax, ref agent.defaultData.gravityMax); UpdateFloatValue(gravityAcceleration, ref agent.data.gravityAcceleration, ref agent.defaultData.gravityAcceleration); UpdateFloatValue(gravityScale, ref agent.data.gravityScale, ref agent.defaultData.gravityScale); + // Rotation UpdateEnumValue(rotateFacing, ref agent.data.rotateFacing, ref agent.defaultData.rotateFacing); - - UpdateFloatValue(rotationSpeedTarget, ref agent.data.rotationSpeedTarget, ref agent.defaultData.rotationSpeedTarget); + UpdateFloatValue(rotationSpeed, ref agent.data.rotationSpeed, ref agent.defaultData.rotationSpeed); UpdateFloatValue(rotationSmoothing, ref agent.data.rotationSmoothing, ref agent.defaultData.rotationSmoothing); - - UpdateVector3Value(feedNewDirection, ref feedDir, ref feedDir); - - // Debug.Log(feedDir); + UpdateFloatValue(rotationInputBlending, ref agent.data.rotationInputBlending, ref agent.defaultData.rotationInputBlending); - if (feedDir != Vector3.zero) { - agent.AddToCurrentDirection(agent.transform.rotation * feedDir.normalized, newDirectionPower); - feedDir = Vector3.zero; - } - + // Rotation from value if (feedNewRotation.value != Vector3.zero) { if (rotationRelativeSpace == Space.World) { Debug.Log(Quaternion.LookRotation(feedRelativeTo.value)); @@ -91,9 +113,7 @@ namespace Reset.Core { } else { agent.SetSpecifiedRotation(agent.transform.rotation * Quaternion.Euler(feedNewRotation.value) * Quaternion.LookRotation(feedRelativeTo.value)); } - } - EndAction(true); } diff --git a/Assets/Scripts/Core/LockOnManager.cs b/Assets/Scripts/Core/LockOnManager.cs index fb6c5ee..e97acc9 100644 --- a/Assets/Scripts/Core/LockOnManager.cs +++ b/Assets/Scripts/Core/LockOnManager.cs @@ -29,8 +29,7 @@ public class LockOnManager : MonoBehaviour{ // Lock On Tracking [Space(10)] - [ShowInInspector] - ActiveLockOnTarget mainTarget; + public ActiveLockOnTarget mainTarget; public List activeTargets = new List(); @@ -54,7 +53,6 @@ public class LockOnManager : MonoBehaviour{ return; } - // Quick check for things in lock-on target that aren't lock-onable if (mainTarget != null && mainTarget.gameObject.GetComponent() == null) { mainTarget.gameObject.AddComponent(); diff --git a/Assets/Scripts/Core/ValueGroup.cs b/Assets/Scripts/Core/ValueGroup.cs index aa76e14..77228c3 100644 --- a/Assets/Scripts/Core/ValueGroup.cs +++ b/Assets/Scripts/Core/ValueGroup.cs @@ -1,6 +1,5 @@ using System; using ParadoxNotion.Design; -using Reset.Core; using UnityEditor; using UnityEngine; @@ -483,6 +482,6 @@ namespace Reset.Core{ return _instance; } } - #endif +#endif } diff --git a/Assets/Scripts/Player/UnitMovementHandler.cs b/Assets/Scripts/Player/UnitMovementHandler.cs index 05d380f..b41c05b 100644 --- a/Assets/Scripts/Player/UnitMovementHandler.cs +++ b/Assets/Scripts/Player/UnitMovementHandler.cs @@ -1,9 +1,8 @@ using System; -using System.Collections.Generic; using UnityEngine; -using Drawing; -using Reset.Movement; +using ParadoxNotion.Design; using Sirenix.OdinInspector; +using UnityEngine.Serialization; public enum PlayerFacingDirection{ TowardsTarget = 0, @@ -25,10 +24,13 @@ namespace Reset.Units{ // Movement Direction public float accelerationSmoothing = 5f; public float deaccelerationSmoothing = 5f; - public AnimationCurve deaccelerationCurve; + // public AnimationCurve deaccelerationCurve; // Currently unused, may return + [SliderField(0,1)] + public float airDirectionDecay; + // Move Speed - public float moveSpeedTarget = 15f; + public float moveSpeed = 15f; public float moveSpeedSoothing = 10f; // Jumping @@ -44,11 +46,12 @@ namespace Reset.Units{ // Rotation [ShowInInspector, SerializeReference] public Enum rotateFacing; - public float rotationSpeedTarget = 5f; + [FormerlySerializedAs("rotationSpeedTarget")] public float rotationSpeed = 5f; public float rotationSmoothing = 1f; + public float rotationInputBlending = .3f; public object Clone(){ - return this.MemberwiseClone(); + return MemberwiseClone(); } } @@ -59,32 +62,28 @@ namespace Reset.Units{ // } // Debug viewing - // Smoothed Values [ShowInInspector, ReadOnly] private float outputSpeed; [ShowInInspector, ReadOnly] private float additionalSpeed; - [ShowInInspector, ReadOnly] private Vector3 outputMoveDirection; + [ShowInInspector, ReadOnly] public Vector3 outputMoveDirection; [ShowInInspector, ReadOnly] private Vector3 additionalMoveDirection; [ShowInInspector, ReadOnly] private Quaternion outputRotation; [ShowInInspector, ReadOnly] private Quaternion specifiedRotation; [ShowInInspector, ReadOnly] private float outputRotationSpeed; - private CharacterController controller; - public PlayerControls controls; + private PlayerControls controls; + private LockOnManager lockOnManager; [ShowInInspector, PropertyOrder(2)] - public UnitMovementData data = new UnitMovementData(); + public UnitMovementData data = new(); [HideInInspector] public UnitMovementData defaultData; - - [Button, PropertyOrder(1)] - void ResetMovementData(){ - data = new UnitMovementData(); - } - + void Awake(){ controller = GetComponent(); + controls = GetComponent(); + lockOnManager = GetComponent(); } private void Start(){ @@ -100,15 +99,40 @@ namespace Reset.Units{ DoMovement(); } + // Add directly to the direction public void AddToCurrentDirection(Vector3 inputDirection, float power){ additionalMoveDirection += inputDirection.normalized; additionalSpeed = power; } + // Hold a new rotation to be moved to during PlayerFacingDirection.SpecifiedRotation public void SetSpecifiedRotation(Quaternion inputRotation){ specifiedRotation = inputRotation; } + + // Blend between the current direction and an input direction, based on the current controller input + public void OverwriteDirectionFromInput(Vector2 value, float priority, float speed = Mathf.Infinity){ + // Create a new direction that is the current controller input * the amount of power they should have + Vector3 dirToAdd = new Vector3(controls.rawMoveInput.x * value.x, 0f, controls.rawMoveInput.y * value.y); + + // Multiply it by the current magnitude (why? i forgor) + dirToAdd *= controls.rawMoveInput.magnitude; + + // Blend the existing direction into it + dirToAdd = Vector3.Lerp(outputMoveDirection, dirToAdd, priority * controls.rawMoveInput.magnitude); + + // Set the new direction + outputMoveDirection = new Vector3(dirToAdd.x, outputMoveDirection.y, dirToAdd.z); + + // Everthing under here is for the speed now. If it's not set do...nothing. Otherwise set it to the max value between move speed and the new speed + if (float.IsPositiveInfinity(speed)) { + return; + } + + outputSpeed = Mathf.Lerp(outputSpeed, Mathf.Max(data.moveSpeed, speed), priority); + } + // Update the direction, called every frame private void UpdateCurrentDirection(){ // Get input value Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y); @@ -116,7 +140,8 @@ namespace Reset.Units{ // Construct move direction Vector3 targetDirection = inputMovement; - if (inputMovement.magnitude < .1f) { + // Deadzone + if (inputMovement.magnitude < .05f) { targetDirection = Vector3.zero; } @@ -124,24 +149,28 @@ namespace Reset.Units{ Vector3 targetNoY = new Vector3(targetDirection.x, 0f, targetDirection.z); Vector3 currentNoY = new Vector3(outputMoveDirection.x, 0f, outputMoveDirection.z); - // Smooth movement + // Smooth movement. Use deaccel smoothing if the input magnitude is lower, and accel smoothing if it's higher + // Also checks when grounded to only use Slerp on the ground if (targetNoY.magnitude > currentNoY.magnitude) { - currentNoY = Vector3.Slerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime); - + currentNoY = controller.isGrounded ? + Vector3.Slerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime) : + Vector3.Lerp(currentNoY, targetNoY,data.accelerationSmoothing * Time.deltaTime); } else { - // float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude); - - currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime); + currentNoY = controller.isGrounded ? + Vector3.Slerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime) : + Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * data.airDirectionDecay * Time.deltaTime); } // Commit move direction outputMoveDirection = new Vector3(currentNoY.x, outputMoveDirection.y, currentNoY.z); } + // Update the speed, called every frame private void UpdateCurrentSpeed(){ - outputSpeed = Mathf.Lerp(outputSpeed, data.moveSpeedTarget, data.moveSpeedSoothing * Time.deltaTime); + outputSpeed = Mathf.Lerp(outputSpeed, data.moveSpeed, data.moveSpeedSoothing * Time.deltaTime); } + // Update the gravity, called every frame private void UpdateCurrentGravity(){ // Accelerate gravity data.gravityPower += data.gravityAcceleration * Time.deltaTime; @@ -159,58 +188,74 @@ namespace Reset.Units{ outputMoveDirection.y = gravityMoveDirection; } + // Update the rotation, called every frame private void UpdateCurrentRotation(){ // Get input value Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y); + // Switch the desired rotation based on current movement setting switch (data.rotateFacing) { - // TODO: Recomment + // Just look at target case PlayerFacingDirection.TowardsTarget: - Debug.LogError("Not implemented..."); + // Look directly at the target + outputRotation = Quaternion.LookRotation(transform.position.DirectionTo(lockOnManager.mainTarget.gameObject.transform.position)); break; case PlayerFacingDirection.Momentum: - if (inputMovement.magnitude > .03f){ + // Look towards the current direction the agent is moving + if (inputMovement.magnitude > .05f){ outputRotation = Camera.main.transform.rotation * Quaternion.LookRotation(outputMoveDirection); } break; case PlayerFacingDirection.MatchForward: - if (controls.rawMoveInput.magnitude < 0.01f) { break; } + // Look towards the input direction....why??? I guess cause move direction is PlayerFacingDirection.Momentum. + if (controls.rawMoveInput.magnitude < 0.05f) { break; } + outputRotation = Camera.main.transform.rotation * Quaternion.LookRotation(inputMovement); - break; case PlayerFacingDirection.MatchCamera: - + // Look the same direction as the camera outputRotation = Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0, null, 0)); break; case PlayerFacingDirection.Static: + // Don't change outputRotation = transform.rotation; break; case PlayerFacingDirection.SpecifiedDirection: + // Look at an inputed rotation outputRotation = specifiedRotation; break; - } + } - // Calculate rotation speed - outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeedTarget, data.rotationSmoothing * Time.deltaTime); + // Add the current input into the created rotation + if (inputMovement.magnitude > .05){ + outputRotation = Quaternion.Lerp(outputRotation, Camera.main.transform.rotation * Quaternion.LookRotation(inputMovement), data.rotationInputBlending); + } + + // Calculate rotation speed, as in how fast the fella rotates. This value has it's own smoothing to allow for gradual increasing/decreasing of the speed till it reaches the target + outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeed, data.rotationSmoothing * Time.deltaTime); // Set final rotation - transform.rotation = Quaternion.Lerp(transform.rotation, outputRotation, outputRotationSpeed * Time.deltaTime).Flatten(0, null, 0); + transform.rotation = Quaternion.Slerp(transform.rotation, outputRotation, outputRotationSpeed * Time.deltaTime).Flatten(0, null, 0); } + // Move with default settings public void DoMovement(){ DoMovement(outputMoveDirection, outputSpeed, data.gravityScale); } + // Custom move from input public void DoMovement(Vector3 moveDir, float speed, float gravity){ - // Commit the move, with respect to the camera's rotation + // Seperate the different move directions. Additonal becomes it's own because it needs to be added independently of the other two Vector3 moveXZDir = new Vector3(moveDir.x, 0f, moveDir.z); Vector3 moveYDir = new Vector3(0f, moveDir.y, 0f); Vector3 addDir = additionalMoveDirection; + // Add their related speeds moveXZDir *= speed * Time.deltaTime; moveYDir *= gravity * Time.deltaTime; addDir *= additionalSpeed * Time.deltaTime; + // Construct the direction and move Vector3 finalDir = moveXZDir + moveYDir; controller.Move((Camera.main.transform.rotation.Flatten(0, null, 0) * finalDir) + addDir); @@ -233,12 +278,12 @@ namespace Reset.Units{ // Remove Y from variables Vector3 currentNoY = new Vector3(additionalMoveDirection.x, 0f, additionalMoveDirection.z); + // Decay the direction if (inputMovement.magnitude < currentNoY.magnitude) { additionalMoveDirection = Vector3.Slerp(additionalMoveDirection, Vector3.zero,data.accelerationSmoothing * Time.deltaTime); } else { - // float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude); - + // float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude); additionalMoveDirection = Vector3.Lerp(additionalMoveDirection, Vector3.zero, data.deaccelerationSmoothing * Time.deltaTime); } } @@ -248,10 +293,6 @@ namespace Reset.Units{ data.jumpPower -= data.jumpPowerDecay * Time.deltaTime; data.jumpPower = Mathf.Max(0f, data.jumpPower); } - - public void AssignNewData(UnitMovementData movementData){ - data = (UnitMovementData)movementData.Clone(); - } } }