change: gravity settings functions and deprecation of jump/gravity split
This commit is contained in:
@@ -2,6 +2,7 @@ using System;
|
||||
using UnityEngine;
|
||||
using ParadoxNotion.Design;
|
||||
using PlasticPipe.PlasticProtocol.Messages;
|
||||
using Reset.Core.Tools;
|
||||
using Sirenix.OdinInspector;
|
||||
|
||||
public enum PlayerFacingDirection{
|
||||
@@ -22,6 +23,7 @@ namespace Reset.Units{
|
||||
[Serializable]
|
||||
public class UnitMovementData : ICloneable{
|
||||
// Movement Direction
|
||||
public SettingValue<Vector2> moveSmoothing = new SettingValue<Vector2>(new Vector2(.5f, .5f));
|
||||
public SettingValue<float> acceleration = new SettingValue<float>(5f);
|
||||
public SettingValue<float> deaccerlation = new SettingValue<float>(5f);
|
||||
|
||||
@@ -52,7 +54,7 @@ namespace Reset.Units{
|
||||
}
|
||||
|
||||
public class ResolvedMovement{
|
||||
public UnitMovementHandler.MoveDirection moveDirection;
|
||||
[ShowInInspector] public UnitMovementHandler.MoveDirection moveDirection;
|
||||
public float moveSpeed;
|
||||
public Quaternion rotation;
|
||||
public float rotationSpeed;
|
||||
@@ -63,23 +65,26 @@ namespace Reset.Units{
|
||||
public struct MoveDirection{
|
||||
private Transform owner;
|
||||
|
||||
private Vector2 moveDir; // Always local
|
||||
private Vector2 _moveDir; // Always local
|
||||
|
||||
public Vector2 World{
|
||||
get => owner.TransformDirection(Local);
|
||||
get => owner.TransformDirection(_moveDir);
|
||||
set{
|
||||
moveDir = owner.InverseTransformDirection(value);
|
||||
Local = moveDir;
|
||||
_moveDir = owner.InverseTransformDirection(value);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 Local{
|
||||
get => owner.InverseTransformDirection(World);
|
||||
get => _moveDir;
|
||||
set {
|
||||
moveDir = value;
|
||||
World = owner.TransformDirection(value);
|
||||
_moveDir = value;
|
||||
}
|
||||
}
|
||||
|
||||
public MoveDirection(Transform ownerTransform){
|
||||
owner = ownerTransform;
|
||||
_moveDir = Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
[ShowInInspector]
|
||||
@@ -130,6 +135,7 @@ namespace Reset.Units{
|
||||
defaultEasing = (UnitMovementData)easing.Clone();
|
||||
|
||||
resolvedMovement = new ResolvedMovement();
|
||||
resolvedMovement.moveDirection = new MoveDirection(transform);
|
||||
}
|
||||
|
||||
void Update(){
|
||||
@@ -198,57 +204,69 @@ namespace Reset.Units{
|
||||
// Update the direction, called every frame
|
||||
private void UpdateCurrentDirection(){
|
||||
// Get input value
|
||||
Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y);
|
||||
|
||||
// Construct move direction
|
||||
Vector3 targetDirection = inputMovement;
|
||||
Vector2 targetDirection = new Vector2(controls.rawMoveInput.x, controls.rawMoveInput.y);
|
||||
|
||||
// Deadzone
|
||||
if (inputMovement.magnitude < .05f) {
|
||||
targetDirection = Vector3.zero;
|
||||
if (targetDirection.magnitude < .05f) {
|
||||
targetDirection = Vector2.zero;
|
||||
}
|
||||
|
||||
// Remove Y from variables
|
||||
Vector3 targetNoY = new Vector3(targetDirection.x, 0f, targetDirection.z);
|
||||
Vector3 currentNoY = new Vector3(outputMoveDirection.x, 0f, outputMoveDirection.z);
|
||||
|
||||
// Get current direction
|
||||
Vector2 currentDirection = resolvedMovement.moveDirection.Local;
|
||||
|
||||
// Also need to find the dot value of the current input versus the current move direction
|
||||
Vector3 slerpedValue;
|
||||
Vector3 lerpedValue;
|
||||
Vector2 lerpedValue;
|
||||
Vector2 newDirection;
|
||||
|
||||
float switchedDirection = Vector3.Dot(inputMovement, outputMoveDirection);
|
||||
float switchedDirection = Vector3.Dot(targetDirection, currentDirection);
|
||||
float switchedDirectionRemapped = Mathf.Lerp(0, 1, switchedDirection);
|
||||
|
||||
directionChangeDotLerp = Mathf.Lerp(switchedDirection, switchedDirectionRemapped, 5f * Time.deltaTime) ; // turn that .5f into a variable
|
||||
|
||||
DebugOverlayDrawer.ChangeValue("Movement", "Direction Change Dot", directionChangeDotLerp);
|
||||
|
||||
// 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) {
|
||||
if (targetDirection.magnitude > currentDirection.magnitude) {
|
||||
if (controller.isGrounded){
|
||||
slerpedValue = Vector3.Slerp(currentNoY, targetNoY, data.acceleration.value * Time.deltaTime);
|
||||
lerpedValue = Vector3.Lerp(currentNoY, targetNoY, data.acceleration.value * Time.deltaTime);
|
||||
slerpedValue = Vector3.Slerp(currentDirection, targetDirection, data.acceleration.value * Time.deltaTime); // This used to be a slerp. If rotational movement broke this is why
|
||||
|
||||
currentNoY = Vector3.Lerp(lerpedValue, slerpedValue, directionChangeDotLerp);
|
||||
// slerpedValue = Quaternion.AngleAxis(30, Vector3.up) * slerpedValue;
|
||||
lerpedValue = Vector2.Lerp(currentDirection, targetDirection,data.acceleration.value * Time.deltaTime);
|
||||
|
||||
newDirection = Vector2.Lerp(slerpedValue, slerpedValue, directionChangeDotLerp);
|
||||
// newDirection = slerpedValue;
|
||||
} else {
|
||||
currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.acceleration.value * Time.deltaTime);
|
||||
newDirection = Vector2.Lerp(currentDirection, targetDirection, data.acceleration.value * Time.deltaTime);
|
||||
}
|
||||
} else {
|
||||
if (controller.isGrounded){
|
||||
slerpedValue = Vector3.Slerp(currentNoY, targetNoY, data.deaccerlation.value * Time.deltaTime);
|
||||
lerpedValue = Vector3.Lerp(currentNoY, targetNoY, data.deaccerlation.value * Time.deltaTime);
|
||||
slerpedValue = Vector3.Slerp(currentDirection, targetDirection, data.deaccerlation.value * Time.deltaTime); // This used to be a slerp. If rotational movement broke this is why
|
||||
lerpedValue = Vector2.Lerp(currentDirection, targetDirection, data.deaccerlation.value * Time.deltaTime);
|
||||
|
||||
currentNoY = Vector3.Lerp(lerpedValue, slerpedValue, directionChangeDotLerp);
|
||||
newDirection = Vector2.Lerp(lerpedValue, slerpedValue, directionChangeDotLerp);
|
||||
} else {
|
||||
currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.deaccerlation.value * data.airDirectionDecay.value * Time.deltaTime);
|
||||
newDirection = Vector2.Lerp(currentDirection, targetDirection, data.deaccerlation.value * data.airDirectionDecay.value * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
// newDirection = Vector2.MoveTowards(currentDirection, targetDirection.Rotate(-Vector2.Angle(currentDirection, targetDirection)), 2f * Time.deltaTime);
|
||||
|
||||
// Commit move direction
|
||||
outputMoveDirection = Vector3.SmoothDamp(outputMoveDirection, new Vector3(currentNoY.x, outputMoveDirection.y, currentNoY.z),ref moveSmoothVelocityRef , .5f *Time.deltaTime); // TODO: Check this smoothing
|
||||
resolvedMovement.moveDirection.Local = Vector2.SmoothDamp(
|
||||
resolvedMovement.moveDirection.Local,
|
||||
newDirection,
|
||||
ref smoothing.moveSmoothing.refVel,
|
||||
smoothing.moveSmoothing.smoothing *Time.deltaTime); // TODO: Check this smoothing
|
||||
|
||||
resolvedMovement.moveDirection.Local = newDirection;
|
||||
}
|
||||
|
||||
// Update the speed, called every frame
|
||||
private void UpdateCurrentSpeed(){
|
||||
//TODO: Is accel/deaccel ever used in this file????????
|
||||
|
||||
resolvedMovement.moveSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, data.moveSpeed.value, data.moveSpeed.smoothing * Time.deltaTime);
|
||||
}
|
||||
|
||||
@@ -326,16 +344,16 @@ namespace Reset.Units{
|
||||
|
||||
// Move with default settings
|
||||
public void DoMovement(){
|
||||
DoMovement(outputMoveDirection, outputSpeed, data.gravityScale.value); // TODO: Gets multiplied a second time in DoMovement by gravity scale????
|
||||
DoMovement(resolvedMovement.moveDirection.Local, resolvedMovement.moveSpeed, data.gravityScale.value); // TODO: Gets multiplied a second time in DoMovement by gravity scale????
|
||||
}
|
||||
|
||||
// Custom move from input
|
||||
public void DoMovement(Vector3 moveDir, float speed, float gravityScale){
|
||||
public void DoMovement(Vector2 moveDir, float speed, float gravityScale){
|
||||
// Debug.Log($"moveDir: {moveDir}, agent velocity: {transform.InverseTransformDirection(controller.velocity.normalized)}");
|
||||
|
||||
// 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, resolvedMovement.gravity, 0f);
|
||||
Vector2 moveXZDir = new Vector3(moveDir.x, moveDir.y);
|
||||
float moveYDir = resolvedMovement.gravity;
|
||||
Vector3 addDir = additionalMoveDirection;
|
||||
|
||||
// Add their related speeds
|
||||
@@ -344,7 +362,7 @@ namespace Reset.Units{
|
||||
addDir *= additionalSpeed * Time.deltaTime;
|
||||
|
||||
// Construct the direction and move
|
||||
Vector3 finalDir = moveXZDir + moveYDir;
|
||||
Vector3 finalDir = new Vector3(moveXZDir.x, moveYDir,moveXZDir.y) ;
|
||||
controller.Move((Camera.main.transform.rotation.Flatten(0, null, 0) * finalDir) + addDir);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user