Files
project-reset/Assets/Scripts/Core/Graph Tasks/CalculateGroundedLocomotion.cs
2025-03-12 18:27:51 -04:00

83 lines
2.4 KiB
C#

using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
namespace NodeCanvas.Tasks.Actions {
[Category("Reset/Movement")]
[Description("Unit flat ground movement")]
public class CalculateGroundedLocomotion : ActionTask<CharacterController>{
public BBParameter<float> baseMoveSpeed;
public BBParameter<float> sprintAdditionalSpeed;
public BBParameter<Vector3> moveDirection;
private float sprintPower;
private bool sprinting;
//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() {
// Get current input direction
Vector3 flatSurfaceDirection = Vector3.zero;
if (agent.isGrounded) {
Vector2 rawInput = agent.transform.GetComponent<PlayerControls>().rawMoveInput;
flatSurfaceDirection = new(rawInput.x, 0, rawInput.y);
}
float CalculateSpeed(){
// Calculate sprinting speed
float outputSpeed = 0f;
// Add base speed
outputSpeed += baseMoveSpeed.value;
// Add sprinting speed
// TODO: Take this sprinting section out and move it into it's own task. Sprinting should be a state in the ground locomotion graph.
if (sprinting) {
sprintPower = Mathf.Lerp(sprintPower, 1, sprintAdditionalSpeed.value * Time.deltaTime);
outputSpeed += (sprintAdditionalSpeed.value * sprintPower);
} else {
outputSpeed += (sprintAdditionalSpeed.value * sprintPower);
sprintPower = Mathf.Lerp(sprintPower, 0, sprintAdditionalSpeed.value * Time.deltaTime);
}
return outputSpeed;
}
// Rotate input to forward direction for flat surface
// flatSurfaceDirection = agent.transform.rotation * flatSurfaceDirection;
// Finalize flat surface direction by adding speed
flatSurfaceDirection *= CalculateSpeed();
moveDirection.value = flatSurfaceDirection;
EndAction(true);
}
//Called when the task is disabled.
protected override void OnStop() {
}
//Called when the task is paused.
protected override void OnPause() {
}
}
}