From 103ffce69bf773fe8cf46e34054d663a606e04a5 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 15 Jul 2025 17:00:45 -0400 Subject: [PATCH] fixed: changed how rotation is handled in transition from grounded to air to account for becoming grounded without jumping --- Assets/Player/Graphs/GroundedCheckBT.asset | 24 ++++++++ .../Player/Graphs/GroundedCheckBT.asset.meta | 8 +++ Assets/Scripts/Core/Graph Tasks/AddJump.cs | 6 +- .../Core/Graph Tasks/CalculateAirMovement.cs | 7 ++- .../Core/Graph Tasks/ProcessMovement.cs | 6 +- .../Core/Graph Tasks/SetAirMovement.cs | 56 +++++++++++++++++++ .../Core/Graph Tasks/SetAirMovement.cs.meta | 2 + 7 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 Assets/Player/Graphs/GroundedCheckBT.asset create mode 100644 Assets/Player/Graphs/GroundedCheckBT.asset.meta create mode 100644 Assets/Scripts/Core/Graph Tasks/SetAirMovement.cs create mode 100644 Assets/Scripts/Core/Graph Tasks/SetAirMovement.cs.meta diff --git a/Assets/Player/Graphs/GroundedCheckBT.asset b/Assets/Player/Graphs/GroundedCheckBT.asset new file mode 100644 index 0000000..5ef36a4 --- /dev/null +++ b/Assets/Player/Graphs/GroundedCheckBT.asset @@ -0,0 +1,24 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + 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":522.0},"$type":"NodeCanvas.BehaviourTrees.WaitUntil","$id":"2"},{"_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":396.0,"y":655.0},"$type":"NodeCanvas.BehaviourTrees.ActionNode","$id":"3"},{"_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":"4"}],"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":"4"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"},{"_sourceNode":{"$ref":"2"},"_targetNode":{"$ref":"3"},"$type":"NodeCanvas.BehaviourTrees.BTConnection"}],"canvasGroups":[],"localBlackboard":{"_variables":{}},"derivedData":{"repeat":true,"$type":"NodeCanvas.BehaviourTrees.BehaviourTree+DerivedSerializationData"}}' + _objectReferences: [] + _graphSource: + _version: 3.31 + _category: + _comments: + _translation: {x: 150, y: -43} + _zoomFactor: 1 + _haltSerialization: 0 + _externalSerializationFile: {fileID: 0} diff --git a/Assets/Player/Graphs/GroundedCheckBT.asset.meta b/Assets/Player/Graphs/GroundedCheckBT.asset.meta new file mode 100644 index 0000000..f458e15 --- /dev/null +++ b/Assets/Player/Graphs/GroundedCheckBT.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: f9aba4b029eacea44966829321f0124a +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Core/Graph Tasks/AddJump.cs b/Assets/Scripts/Core/Graph Tasks/AddJump.cs index ffacbef..6f16a3d 100644 --- a/Assets/Scripts/Core/Graph Tasks/AddJump.cs +++ b/Assets/Scripts/Core/Graph Tasks/AddJump.cs @@ -51,7 +51,7 @@ namespace NodeCanvas.Tasks.Actions { // Save current velocity and get current input direction Vector3 currentVelocityVector3 = new Vector3(agent.velocity.x, 0f, agent.velocity.z); Vector2 currentInput = agent.GetComponent().rawMoveInput; - Vector3 currentInputVector3 = new(currentInput.x, 0f, currentInput.y); + Vector3 currentInputVector3 = Camera.main.transform.rotation.Flatten(0, null, 0) * new Vector3(currentInput.x, 0f, currentInput.y); // Ignore rotation for the current velocity Vector3 currentVelocityWorld = agent.transform.InverseTransformDirection(currentVelocityVector3.normalized); @@ -93,10 +93,6 @@ namespace NodeCanvas.Tasks.Actions { airMoveDirection.value += outputDirection * outputVelocity; } - // Transform and apply rotatio - - agent.transform.rotation = Quaternion.LookRotation(airMoveDirection.value); - EndAction(true); } diff --git a/Assets/Scripts/Core/Graph Tasks/CalculateAirMovement.cs b/Assets/Scripts/Core/Graph Tasks/CalculateAirMovement.cs index 76d34c5..0cf9d87 100644 --- a/Assets/Scripts/Core/Graph Tasks/CalculateAirMovement.cs +++ b/Assets/Scripts/Core/Graph Tasks/CalculateAirMovement.cs @@ -22,6 +22,8 @@ namespace NodeCanvas.Tasks.Actions { //EndAction can be called from anywhere. protected override void OnExecute() { airControlPower = 1f; + protected override void OnExecute(){ + } //Called once per frame while the action is active. @@ -34,8 +36,9 @@ namespace NodeCanvas.Tasks.Actions { // Add air control Vector3 inputVector3 = new(agent.GetComponent().rawMoveInput.x, 0f, agent.GetComponent().rawMoveInput.y); - float originalMagnitude = airMoveDirection.value.magnitude; - airMoveDirection.value += Camera.main.transform.rotation * inputVector3 * airControlPower; + float originalMagnitude = airMoveDirection.value.magnitude; + airMoveDirection.value += Camera.main.transform.rotation.Flatten(0, null, 0) * + inputVector3 * airControlPower; airMoveDirection.value = Vector3.ClampMagnitude(airMoveDirection.value, originalMagnitude); } diff --git a/Assets/Scripts/Core/Graph Tasks/ProcessMovement.cs b/Assets/Scripts/Core/Graph Tasks/ProcessMovement.cs index a9d5552..2d61d83 100644 --- a/Assets/Scripts/Core/Graph Tasks/ProcessMovement.cs +++ b/Assets/Scripts/Core/Graph Tasks/ProcessMovement.cs @@ -96,9 +96,6 @@ namespace NodeCanvas.Tasks.Actions { targetRotation = Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0, null, 0)); break; } - - // Set final rotation - agent.transform.rotation = Quaternion.Lerp(agent.transform.rotation, targetRotation, 10f * Time.deltaTime); // Construct move direction Vector3 finalMoveDir = Vector3.zero; @@ -125,6 +122,9 @@ namespace NodeCanvas.Tasks.Actions { agent.Move((finalMoveDir + gravityMoveDirection) * Time.deltaTime); } + // Set final rotation + agent.transform.rotation = Quaternion.Lerp(agent.transform.rotation, targetRotation, 10f * Time.deltaTime); + // ???? Moved this above but don't remember if this needs to be here still if (agent.isGrounded) { jumpPower.value = 0f; diff --git a/Assets/Scripts/Core/Graph Tasks/SetAirMovement.cs b/Assets/Scripts/Core/Graph Tasks/SetAirMovement.cs new file mode 100644 index 0000000..796a5fc --- /dev/null +++ b/Assets/Scripts/Core/Graph Tasks/SetAirMovement.cs @@ -0,0 +1,56 @@ +using NodeCanvas.Framework; +using ParadoxNotion.Design; +using UnityEngine; + + +namespace NodeCanvas.Tasks.Actions { + + [Category("Reset")] + [Description("Hard set the air direction to a certain direction")] + public class SetAirMovement : ActionTask{ + public BBParameter airMoveDirection; + + public BBParameter inputVector3; + public Space fromSpace; + public Space toSpace; + + //Use for initialization. This is called only once in the lifetime of the task. + //Return null if init was successfull. Return an error string otherwise + protected override string OnInit() { + return null; + } + + //This is called once each time the task is enabled. + //Call EndAction() to mark the action as finished, either in success or failure. + //EndAction can be called from anywhere. + protected override void OnExecute(){ + + } + + //Called once per frame while the action is active. + protected override void OnUpdate() { + if (fromSpace == toSpace) { + airMoveDirection.value = inputVector3.value; + } + + // NOTE: I swear to God these absolutely do not work and nobody could convince me otherwise + if (fromSpace == Space.World){ + airMoveDirection.value = Camera.main.transform.rotation.Flatten(0, null, 0) * inputVector3.value; + } else { + airMoveDirection.value = Camera.main.transform.rotation.Flatten(0, null, 0) * agent.InverseTransformVector(inputVector3.value); + } + + EndAction(true); + } + + //Called when the task is disabled. + protected override void OnStop() { + + } + + //Called when the task is paused. + protected override void OnPause() { + + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Core/Graph Tasks/SetAirMovement.cs.meta b/Assets/Scripts/Core/Graph Tasks/SetAirMovement.cs.meta new file mode 100644 index 0000000..71d42c5 --- /dev/null +++ b/Assets/Scripts/Core/Graph Tasks/SetAirMovement.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: f1db555b92ed94543beab7a2106bc2a3 \ No newline at end of file