129 lines
4.6 KiB
C#
129 lines
4.6 KiB
C#
using NodeCanvas.Framework;
|
|
using ParadoxNotion.Design;
|
|
using ParadoxNotion.Services;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
|
|
namespace NodeCanvas.Tasks.Actions {
|
|
|
|
[Category("Reset/Movement")]
|
|
[Description("Finalizes movement and sends the final move commands to the controller")]
|
|
public class ProcessMovement : ActionTask<CharacterController>{
|
|
public BBParameter<Vector3> groundMoveDirection;
|
|
public BBParameter<Vector3> airMoveDirection;
|
|
[Space(5)]
|
|
public BBParameter<float> gravityAcceleration = 1f;
|
|
public BBParameter<float> gravityMax = 8f;
|
|
|
|
public BBParameter<float> jumpPower;
|
|
public BBParameter<float> jumpPowerDecay;
|
|
|
|
public BBParameter<float> rotationSpeed = 5f;
|
|
public BBParameter<float> rotationSmoothing;
|
|
|
|
private float currentRotSpeed;
|
|
private Vector3 rotationNoY;
|
|
|
|
public BBParameter<float> gravityPower;
|
|
|
|
//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() {
|
|
MonoManager.current.onLateUpdate += LateUpdate;
|
|
currentRotSpeed = rotationSpeed.value;
|
|
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 input values
|
|
Vector2 rawLookInputVector3 = agent.GetComponent<PlayerControls>().rawLookInput;
|
|
Vector2 rawMoveInputVector3 = agent.GetComponent<PlayerControls>().rawMoveInput;
|
|
|
|
gravityPower.value += gravityAcceleration.value * Time.deltaTime;
|
|
gravityPower.value = Mathf.Min(gravityPower.value, gravityMax.value);
|
|
|
|
// Calculate rotation speed
|
|
currentRotSpeed = Mathf.Lerp(currentRotSpeed, rotationSpeed.value, rotationSmoothing.value * Time.deltaTime);
|
|
|
|
// Process input rotation
|
|
agent.transform.Rotate(new Vector3(0, agent.GetComponent<PlayerControls>().rawLookInput.x * rotationSpeed.value * 360f * Time.deltaTime)); // *360 to account for a full circle taking a long ass time
|
|
|
|
// Process movement rotation
|
|
if (agent.isGrounded){
|
|
// Quaternion cameraRotNoY = Quaternion.Euler(0f, Camera.main.transform.rotation.eulerAngles.y , 0f);
|
|
// agent.transform.rotation = Quaternion.Lerp(agent.transform.rotation, Quaternion.LookRotation(agent.transform.position + groundMoveDirection.value), 10f * Time.deltaTime);
|
|
} else {
|
|
// rotationNoY = airMoveDirection.value;
|
|
// rotationNoY.x = 0f;
|
|
// rotationNoY.z = 0f;
|
|
// agent.transform.rotation = Quaternion.Lerp(agent.transform.rotation, Quaternion.Euler(rotationNoY), 5f * Time.deltaTime);
|
|
}
|
|
|
|
// Construct move direction
|
|
Vector3 finalMoveDir = Vector3.zero;
|
|
Vector3 gravityMoveDirection;
|
|
|
|
// Create movement direction for ground movement based on move direction, facing direction, and input
|
|
// Start by lerping facing direction towards move direction based on input power
|
|
rotationNoY = airMoveDirection.value;
|
|
rotationNoY.x = 0f;
|
|
rotationNoY.z = 0f;
|
|
|
|
// agent.transform.rotation = Quaternion.RotateTowards(agent.transform.rotation, Quaternion.Euler(rotationNoY), )
|
|
|
|
// Vector3 groundMoveDirModified = Vector3.Lerp();
|
|
|
|
// Add input movement
|
|
if (agent.isGrounded){
|
|
Quaternion cameraRotNoY = Quaternion.Euler(0f, Camera.main.transform.rotation.eulerAngles.y , 0f);
|
|
|
|
finalMoveDir += groundMoveDirection.value + Vector3.down * .03f;
|
|
gravityMoveDirection = new(0f, jumpPower.value + (Physics.gravity.y *.08f), 0f);
|
|
} else {
|
|
finalMoveDir += airMoveDirection.value;
|
|
gravityMoveDirection = new(0f, jumpPower.value + Physics.gravity.y * gravityPower.value, 0f);
|
|
}
|
|
|
|
// Do final movement
|
|
if (agent.isGrounded) {
|
|
agent.Move((Camera.main.transform.rotation * finalMoveDir + gravityMoveDirection) * Time.deltaTime);
|
|
} else {
|
|
agent.Move((finalMoveDir + gravityMoveDirection) * Time.deltaTime);
|
|
}
|
|
|
|
// Reset gravity power when grounded
|
|
if (agent.isGrounded) {
|
|
gravityPower.value = 0f;
|
|
}
|
|
|
|
if (agent.isGrounded) {
|
|
jumpPower.value = 0f;
|
|
}
|
|
}
|
|
|
|
// Gravity is processed in LateUpdate (added to MonoManager's onLateUpdate in OnInit())
|
|
public void LateUpdate(){
|
|
|
|
// Decay jump power
|
|
jumpPower.value = Mathf.Lerp(jumpPower.value, 0f, jumpPowerDecay.value * Time.deltaTime);
|
|
}
|
|
|
|
//Called when the task is disabled.
|
|
protected override void OnStop() {
|
|
EndAction(true);
|
|
}
|
|
|
|
//Called when the task is paused.
|
|
protected override void OnPause() {
|
|
|
|
}
|
|
}
|
|
} |