added: initial seperation of movement from gravity and rotation
This commit is contained in:
102
Assets/Scripts/Core/Graph Tasks/ProcessRotation.cs
Normal file
102
Assets/Scripts/Core/Graph Tasks/ProcessRotation.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using NodeCanvas.Framework;
|
||||
using ParadoxNotion.Design;
|
||||
using Reset.Movement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TextCore.Text;
|
||||
|
||||
|
||||
namespace Reset.Player.Movement {
|
||||
[Category("Reset/Movement")]
|
||||
[Description("Finalizes the character rotation and commits it to the Transform")]
|
||||
public class ProcessRotation : ActionTask<CharacterController> {
|
||||
// Rotation
|
||||
public BBParameter<Vector3> moveDirection;
|
||||
|
||||
public BBParameter<PlayerFacingDirection> playerFacingDirection;
|
||||
public BBParameter<float> rotationSpeed = 5f;
|
||||
public BBParameter<float> rotationSmoothing;
|
||||
|
||||
private Vector3 currentMoveDir;
|
||||
private PlayerControls controls;
|
||||
|
||||
private Quaternion targetRotation;
|
||||
private float currentRotSpeed;
|
||||
|
||||
[ShowIf("playerFacingDirection", 0)]
|
||||
public Vector3 rotationTargetPosition;
|
||||
|
||||
|
||||
//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() {
|
||||
|
||||
|
||||
controls = agent.GetComponent<PlayerControls>();
|
||||
|
||||
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() {
|
||||
// Calculate rotation speed
|
||||
currentRotSpeed = Mathf.Lerp(currentRotSpeed, rotationSpeed.value, rotationSmoothing.value * Time.deltaTime);
|
||||
|
||||
// Set ingitial rotation power
|
||||
currentRotSpeed = rotationSpeed.value;
|
||||
|
||||
switch (playerFacingDirection.value) {
|
||||
case PlayerFacingDirection.Target:
|
||||
// Set rotation to just the direction of the target
|
||||
targetRotation = Quaternion.LookRotation((agent.transform.position.DirectionTo(rotationTargetPosition)).Flatten(null, 0));
|
||||
|
||||
break;
|
||||
case PlayerFacingDirection.Movement:
|
||||
// Check magnitude to avoid the "Look rotation viewing vector is zero" debug
|
||||
|
||||
// Set desired rotation to input direction, with respect to camera rotation
|
||||
if (agent.isGrounded){
|
||||
if (controls.rawMoveInput.magnitude == 0) { break; }
|
||||
|
||||
targetRotation = Quaternion.LookRotation(currentMoveDir) *
|
||||
Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0f, null, 0f));
|
||||
} else {
|
||||
if (moveDirection.value.magnitude == 0) { break; }
|
||||
|
||||
targetRotation = Quaternion.LookRotation(moveDirection.value);
|
||||
}
|
||||
break;
|
||||
case PlayerFacingDirection.MatchCamera:
|
||||
// Craft a new rotation that flattens the camera's rotation to the ground
|
||||
// Needed to keep the character from inheriting the camera's height-rotation
|
||||
targetRotation = Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0, null, 0));
|
||||
break;
|
||||
case PlayerFacingDirection.Static:
|
||||
targetRotation = agent.transform.rotation;
|
||||
break;
|
||||
}
|
||||
|
||||
// Set final rotation
|
||||
agent.transform.rotation = Quaternion.Lerp(agent.transform.rotation, targetRotation, 10f * Time.deltaTime).Flatten(0, null, 0);
|
||||
|
||||
EndAction(true);
|
||||
}
|
||||
|
||||
|
||||
//Called when the task is disabled.
|
||||
protected override void OnStop() {
|
||||
|
||||
}
|
||||
|
||||
//Called when the task is paused.
|
||||
protected override void OnPause() {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user