change: more alterations to the new movement and settingvalue system

This commit is contained in:
Chris
2025-09-22 14:24:50 -04:00
parent d4231d4f38
commit b21adf93e2
18 changed files with 399 additions and 333 deletions

View File

@@ -1,101 +1,12 @@
using System;
using UnityEngine;
using ParadoxNotion.Design;
using PlasticPipe.PlasticProtocol.Messages;
using Reset.Core.Tools;
using Sirenix.OdinInspector;
public enum PlayerFacingDirection{
TowardsTarget = 0,
MatchForward,
MatchCamera,
Static,
Momentum,
SpecifiedDirection
}
namespace Reset.Units{
// public interface IBuffSource{ - Unused good idea?
// public Object sourceObject();
// public
// }
[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);
[SliderField(0,1)]
public SettingValue<float> airDirectionDecay = new SettingValue<float>(1f); // TODO: Check default value
// Move Speed
public SettingValue<float> moveSpeed = new SettingValue<float>(15f, defaultSmoothing: 10f);
// Jumping
[ShowInInspector] public SettingValue<float> jumpPower = new SettingValue<float>(0f);
public SettingValue<float> jumpPowerDecay = new SettingValue<float>(3f); // TODO: Check default value
// Gravity
[ShowInInspector] public SettingValue<float> gravityPower = new SettingValue<float>(1f);
public SettingValue<float> gravityMax = new SettingValue<float>(8f);
public SettingValue<float> gravityAcceleration = new SettingValue<float>(1f);
public SettingValue<float> gravityScale = new SettingValue<float>(1f);
// Rotation
[ShowInInspector, SerializeReference] public Enum rotateFacing;
public SettingValue<float> rotationSpeed = new SettingValue<float>(5f);
public SettingValue<float> rotationInputBlending = new SettingValue<float>(.3f);
public object Clone(){
return MemberwiseClone();
}
}
public class ResolvedMovement{
[ShowInInspector] public UnitMovementHandler.MoveDirection moveDirection;
public float moveSpeed;
public Quaternion rotation;
public float rotationSpeed;
public float gravity;
}
public class UnitMovementHandler : MonoBehaviour{
public struct MoveDirection{
private Transform owner;
private Vector2 _moveDir; // Always local
public Vector2 World{
get => owner.TransformDirection(_moveDir);
set{
_moveDir = owner.InverseTransformDirection(value);
}
}
public Vector2 Local{
get => _moveDir;
set {
_moveDir = value;
}
}
public MoveDirection(Transform ownerTransform){
owner = ownerTransform;
_moveDir = Vector2.zero;
}
}
[ShowInInspector]
public ResolvedMovement resolvedMovement;
// class MovementFloatModifier{
// // IBuffSource source
// public float value;
// }
//
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private float outputSpeed;
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private float additionalSpeed;
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] public Vector3 outputMoveDirection;
@@ -104,7 +15,7 @@ namespace Reset.Units{
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private Quaternion specifiedRotation;
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private float outputRotationSpeed;
// Lerps
// Smoothing Values
private float directionChangeDotLerp;
private Vector3 moveSmoothVelocityRef;
private float gravitySmoothVelocityRef;
@@ -116,12 +27,7 @@ namespace Reset.Units{
// Movement Data
[ShowInInspector, PropertyOrder(2)] public UnitMovementData data = new();
[ShowInInspector, PropertyOrder(2)] public UnitMovementData smoothing = new();
[ShowInInspector, PropertyOrder(2)] public UnitMovementData easing = new();
[HideInInspector] public UnitMovementData defaultData;
[HideInInspector] public UnitMovementData defaultSmoothing;
[HideInInspector] public UnitMovementData defaultEasing;
void Awake(){
controller = GetComponent<CharacterController>();
@@ -130,12 +36,8 @@ namespace Reset.Units{
}
void Start(){
defaultData = (UnitMovementData)data.Clone();
defaultSmoothing = (UnitMovementData)smoothing.Clone();
defaultEasing = (UnitMovementData)easing.Clone();
resolvedMovement = new ResolvedMovement();
resolvedMovement.moveDirection = new MoveDirection(transform);
resolvedMovement.moveDirection = new ResolvedMovement.MoveDirection(transform);
}
void Update(){
@@ -188,7 +90,7 @@ namespace Reset.Units{
return;
}
outputSpeed = Mathf.Lerp(outputSpeed, Mathf.Max(data.moveSpeed.value, speed), priority);
outputSpeed = Mathf.Lerp(outputSpeed, Mathf.Max(data.moveSpeed.Value, speed), priority);
}
// Setting absolute to true will cause the current gravity to snap to the new gravity value.
@@ -228,53 +130,47 @@ namespace Reset.Units{
// 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 (targetDirection.magnitude > currentDirection.magnitude) {
if (controller.isGrounded){
slerpedValue = Vector3.Slerp(currentDirection, targetDirection, data.acceleration.value * Time.deltaTime); // This used to be a slerp. If rotational movement broke this is why
// 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 {
newDirection = Vector2.Lerp(currentDirection, targetDirection, data.acceleration.value * Time.deltaTime);
}
if (controller.isGrounded){
slerpedValue = Vector3.Slerp(currentDirection, targetDirection, data.softening.Value * Time.deltaTime); // This used to be a slerp. If rotational movement broke this is why
lerpedValue = Vector2.Lerp(currentDirection, targetDirection, data.softening.Value * Time.deltaTime);
newDirection = Vector2.Lerp(slerpedValue, lerpedValue, directionChangeDotLerp);
} else {
if (controller.isGrounded){
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);
newDirection = Vector2.Lerp(lerpedValue, slerpedValue, directionChangeDotLerp);
} else {
newDirection = Vector2.Lerp(currentDirection, targetDirection, data.deaccerlation.value * data.airDirectionDecay.value * Time.deltaTime);
}
}
newDirection = Vector2.Lerp(currentDirection, targetDirection, data.softening.Value * data.airDirectionDecay.Value * Time.deltaTime);
}
// newDirection = Vector2.MoveTowards(currentDirection, targetDirection.Rotate(-Vector2.Angle(currentDirection, targetDirection)), 2f * Time.deltaTime);
// Commit move direction
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 = 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????????
float speed;
resolvedMovement.moveSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, data.moveSpeed.value, data.moveSpeed.smoothing * Time.deltaTime);
if (resolvedMovement.moveDirection.Local.magnitude < controls.rawMoveInput.magnitude) {
speed = data.moveSpeed.Value * Time.deltaTime * data.acceleration.Value;
} else {
speed = data.moveSpeed.Value * Time.deltaTime * data.deacceleration.Value;
}
resolvedMovement.moveSpeed = speed;
DebugOverlayDrawer.ChangeValue("Movement", "Resolved Speed", resolvedMovement.moveSpeed);
}
// Update the gravity, called every frame
private void UpdateCurrentGravity(){
// Accelerate gravity
if (!controller.isGrounded){
resolvedMovement.gravity -= data.gravityAcceleration.value * Time.deltaTime;
resolvedMovement.gravity -= data.gravityAcceleration.Value * Time.deltaTime;
}
// resolvedMovement.gravity = Mathf.Clamp(resolvedMovement.gravity, Mathf.NegativeInfinity, data.gravityMax.value);
@@ -285,7 +181,7 @@ namespace Reset.Units{
}
// Create the final gravity value
float gravityMoveDirection = data.jumpPower.value + (Physics.gravity.y * resolvedMovement.gravity);
float gravityMoveDirection = data.jumpPower.Value + (Physics.gravity.y * resolvedMovement.gravity);
// resolvedMovement.gravity = data.jumpPower.value + (Physics.gravity.y * data.gravityPower.currentValue);
// Commit gravity to move direction, ignoring XZ since those are done in UpdateMovementDirection
@@ -332,11 +228,12 @@ namespace Reset.Units{
// Add the current input into the created rotation
if (inputMovement.magnitude > .05){
outputRotation = Quaternion.Lerp(outputRotation, Camera.main.transform.rotation * Quaternion.LookRotation(inputMovement), data.rotationInputBlending.value);
outputRotation = Quaternion.Lerp(outputRotation, Camera.main.transform.rotation * Quaternion.LookRotation(inputMovement), data.rotationInputBlending.Value);
}
// Calculate rotation speed, as in how fast the fella rotates. This value has it's own smoothing to allow for gradual increasing/decreasing of the speed till it reaches the target
outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeed.value, data.rotationSpeed.smoothing * Time.deltaTime);
outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeed.Value, data.softening.Value * Time.deltaTime);
// Set final rotation
transform.rotation = Quaternion.Slerp(transform.rotation, outputRotation, outputRotationSpeed * Time.deltaTime).Flatten(0, null, 0);
@@ -344,7 +241,7 @@ namespace Reset.Units{
// Move with default settings
public void DoMovement(){
DoMovement(resolvedMovement.moveDirection.Local, resolvedMovement.moveSpeed, 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
@@ -358,7 +255,7 @@ namespace Reset.Units{
// Add their related speeds
moveXZDir *= speed * Time.deltaTime;
moveYDir *= data.gravityScale.value * Time.deltaTime;
moveYDir *= data.gravityScale.Value * Time.deltaTime;
addDir *= additionalSpeed * Time.deltaTime;
// Construct the direction and move
@@ -385,21 +282,30 @@ namespace Reset.Units{
// Decay the direction
if (inputMovement.magnitude < currentNoY.magnitude) {
additionalMoveDirection = Vector3.Slerp(additionalMoveDirection, Vector3.zero,data.acceleration.value * Time.deltaTime);
additionalMoveDirection = Vector3.Slerp(additionalMoveDirection, Vector3.zero,data.acceleration.Value * Time.deltaTime);
} else {
// float deaccelValue = data.deaccelerationCurve.Evaluate(inputMovement.magnitude);
additionalMoveDirection = Vector3.Lerp(additionalMoveDirection, Vector3.zero, data.deaccerlation.value * Time.deltaTime);
additionalMoveDirection = Vector3.Lerp(additionalMoveDirection, Vector3.zero, data.deacceleration.Value * Time.deltaTime);
}
// Decay the gravity
additionalMoveDirection.y -= data.gravityPower.value;
additionalMoveDirection.y -= data.gravityPower.Value;
additionalMoveDirection.y = Mathf.Max(0f, additionalMoveDirection.y);
}
private void UpdateGravityLate(){
// Decay jump power
data.jumpPower.value -= data.jumpPowerDecay.value * Time.deltaTime;
data.jumpPower.value = Mathf.Max(0f, data.jumpPower.value);
data.jumpPower.Value -= data.jumpPowerDecay.Value * Time.deltaTime;
data.jumpPower.Value = Mathf.Max(0f, data.jumpPower.Value);
}
[Button("Initialize Settings")]
void InitAllSettings(){
var newthing = data.GetAllSettings();
}
void SmoothAllSettings(){
}
}
}