change: more alterations to movement, settingvalues, valuegroup, etc.
This commit is contained in:
@@ -22,32 +22,29 @@ namespace Reset.Units{
|
||||
[Serializable]
|
||||
public class UnitMovementData : ICloneable{
|
||||
// Movement Direction
|
||||
public float accelerationSmoothing = 5f;
|
||||
public float deaccelerationSmoothing = 5f;
|
||||
public SettingValue<float> acceleration = new SettingValue<float>(5f);
|
||||
public SettingValue<float> deaccerlation = new SettingValue<float>(5f);
|
||||
|
||||
[SliderField(0,1)]
|
||||
public float airDirectionDecay;
|
||||
public SettingValue<float> airDirectionDecay = new SettingValue<float>(1f); // TODO: Check default value
|
||||
|
||||
// Move Speed
|
||||
public float moveSpeed = 15f;
|
||||
public float moveSpeedSoothing = 10f;
|
||||
public SettingValue<float> moveSpeed = new SettingValue<float>(15f, defaultSmoothing: 10f);
|
||||
|
||||
// Jumping
|
||||
[ShowInInspector, ReadOnly] public float jumpPower;
|
||||
public float jumpPowerDecay = 3f;
|
||||
[ShowInInspector] public SettingValue<float> jumpPower = new SettingValue<float>(0f);
|
||||
public SettingValue<float> jumpPowerDecay = new SettingValue<float>(3f); // TODO: Check default value
|
||||
|
||||
// Gravity
|
||||
[ShowInInspector, ReadOnly] public float gravityPower;
|
||||
public float gravityMax = 8f;
|
||||
public float gravityAcceleration = 1f;
|
||||
public float gravityScale = 1f;
|
||||
public float settingsChangeSmoothing = 6f;
|
||||
[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 float rotationSpeed = 5f;
|
||||
public float rotationSmoothing = 1f;
|
||||
public float rotationInputBlending = .3f;
|
||||
public SettingValue<float> rotationSpeed = new SettingValue<float>(5f);
|
||||
public SettingValue<float> rotationInputBlending = new SettingValue<float>(.3f);
|
||||
|
||||
public object Clone(){
|
||||
return MemberwiseClone();
|
||||
@@ -85,6 +82,9 @@ namespace Reset.Units{
|
||||
}
|
||||
}
|
||||
|
||||
[ShowInInspector]
|
||||
public ResolvedMovement resolvedMovement;
|
||||
|
||||
// class MovementFloatModifier{
|
||||
// // IBuffSource source
|
||||
// public float value;
|
||||
@@ -99,12 +99,6 @@ namespace Reset.Units{
|
||||
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private Quaternion specifiedRotation;
|
||||
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private float outputRotationSpeed;
|
||||
|
||||
// Used by graph to gradually shift into new values rather than dump them. Even if they're smoothed values they need to be eased into
|
||||
[FoldoutGroup("Smoothed Values"), ShowInInspector, ReadOnly] private float smoothedJumpDecay;
|
||||
[FoldoutGroup("Smoothed Values"), ShowInInspector, ReadOnly] private float smoothedGravityAccel;
|
||||
[FoldoutGroup("Smoothed Values"), ShowInInspector, ReadOnly] private float smoothedGravityScale;
|
||||
[FoldoutGroup("Smoothed Values"), ShowInInspector, ReadOnly] private float settingsChangeRotationSpeed;
|
||||
|
||||
// Lerps
|
||||
private float directionChangeDotLerp;
|
||||
private Vector3 moveSmoothVelocityRef;
|
||||
@@ -134,28 +128,29 @@ namespace Reset.Units{
|
||||
defaultData = (UnitMovementData)data.Clone();
|
||||
defaultSmoothing = (UnitMovementData)smoothing.Clone();
|
||||
defaultEasing = (UnitMovementData)easing.Clone();
|
||||
|
||||
resolvedMovement = new ResolvedMovement();
|
||||
}
|
||||
|
||||
void Update(){
|
||||
UpdateCurrentDirection();
|
||||
UpdateCurrentGravity();
|
||||
UpdateCurrentSpeed();
|
||||
UpdateCurrentRotation();
|
||||
|
||||
// UpdateCurrentRotation();
|
||||
DoMovement();
|
||||
}
|
||||
|
||||
// Add directly to the direction
|
||||
public void AddToCurrentDirection(Vector3 inputDirection, float power){
|
||||
public void AddToCurrentDirection(Vector3 inputDirection, float power){ // Old
|
||||
additionalMoveDirection += inputDirection.normalized;
|
||||
additionalSpeed = power;
|
||||
}
|
||||
|
||||
public void SmoothToSpeed(float desiredSpeed, float smoothing){
|
||||
public void SmoothToSpeed(float desiredSpeed, float smoothing){ // Old
|
||||
additionalSpeed = Mathf.Lerp(additionalSpeed, desiredSpeed, smoothing * Time.deltaTime);
|
||||
}
|
||||
|
||||
public void SetNewDirection(Vector3 inputDirection){ // NOTE: If smoothing desired add a default bool for smoothing maybe?
|
||||
public void SetNewDirection(Vector3 inputDirection){ // NOTE: If smoothing desired add a default bool for smoothing maybe? // Old
|
||||
additionalMoveDirection = inputDirection.Flatten(null, 0f, null);
|
||||
}
|
||||
|
||||
@@ -163,13 +158,13 @@ namespace Reset.Units{
|
||||
additionalMoveDirection.y = value;
|
||||
}
|
||||
|
||||
// Hold a new rotation to be moved to during PlayerFacingDirection.SpecifiedRotation
|
||||
public void SetSpecifiedRotation(Quaternion inputRotation){
|
||||
// Hold a new rotation to be moved to during PlayerFacingDirection.SpecifiedRotation
|
||||
public void SetSpecifiedRotation(Quaternion inputRotation){ // Old
|
||||
specifiedRotation = inputRotation;
|
||||
}
|
||||
|
||||
// Blend between the current direction and an input direction, based on the current controller input
|
||||
public void OverwriteDirectionFromInput(Vector2 value, float priority, float speed = Mathf.Infinity){
|
||||
public void OverwriteDirectionFromInput(Vector2 value, float priority, float speed = Mathf.Infinity){ // Old
|
||||
// Create a new direction that is the current controller input * the amount of power they should have
|
||||
Vector3 dirToAdd = new Vector3(controls.rawMoveInput.x * value.x, 0f, controls.rawMoveInput.y * value.y);
|
||||
|
||||
@@ -187,7 +182,17 @@ namespace Reset.Units{
|
||||
return;
|
||||
}
|
||||
|
||||
outputSpeed = Mathf.Lerp(outputSpeed, Mathf.Max(data.moveSpeed, 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.
|
||||
// Keeping it false will make it apply additively to the current gravity. Both options use relativty for linear interpolation.
|
||||
public void SetNewGravity(float value, float relativity, bool absolute){ // new
|
||||
if (absolute){
|
||||
resolvedMovement.gravity = Mathf.Lerp(resolvedMovement.gravity, value, relativity);
|
||||
} else {
|
||||
resolvedMovement.gravity = Mathf.Lerp(resolvedMovement.gravity, resolvedMovement.gravity + value, relativity);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the direction, called every frame
|
||||
@@ -220,47 +225,50 @@ namespace Reset.Units{
|
||||
// Also checks when grounded to only use Slerp on the ground
|
||||
if (targetNoY.magnitude > currentNoY.magnitude) {
|
||||
if (controller.isGrounded){
|
||||
slerpedValue = Vector3.Slerp(currentNoY, targetNoY, data.accelerationSmoothing * Time.deltaTime);
|
||||
lerpedValue = Vector3.Lerp(currentNoY, targetNoY, data.accelerationSmoothing * Time.deltaTime);
|
||||
slerpedValue = Vector3.Slerp(currentNoY, targetNoY, data.acceleration.value * Time.deltaTime);
|
||||
lerpedValue = Vector3.Lerp(currentNoY, targetNoY, data.acceleration.value * Time.deltaTime);
|
||||
|
||||
currentNoY = Vector3.Lerp(lerpedValue, slerpedValue, directionChangeDotLerp);
|
||||
} else {
|
||||
currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.accelerationSmoothing * Time.deltaTime);
|
||||
currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.acceleration.value * Time.deltaTime);
|
||||
}
|
||||
} else {
|
||||
if (controller.isGrounded){
|
||||
slerpedValue = Vector3.Slerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime);
|
||||
lerpedValue = Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * Time.deltaTime);
|
||||
slerpedValue = Vector3.Slerp(currentNoY, targetNoY, data.deaccerlation.value * Time.deltaTime);
|
||||
lerpedValue = Vector3.Lerp(currentNoY, targetNoY, data.deaccerlation.value * Time.deltaTime);
|
||||
|
||||
currentNoY = Vector3.Lerp(lerpedValue, slerpedValue, directionChangeDotLerp);
|
||||
} else {
|
||||
currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.deaccelerationSmoothing * data.airDirectionDecay * Time.deltaTime);
|
||||
currentNoY = Vector3.Lerp(currentNoY, targetNoY, data.deaccerlation.value * data.airDirectionDecay.value * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
// Commit move direction
|
||||
outputMoveDirection = Vector3.SmoothDamp(outputMoveDirection, new Vector3(currentNoY.x, outputMoveDirection.y, currentNoY.z),ref moveSmoothVelocityRef , .5f *Time.deltaTime);
|
||||
outputMoveDirection = Vector3.SmoothDamp(outputMoveDirection, new Vector3(currentNoY.x, outputMoveDirection.y, currentNoY.z),ref moveSmoothVelocityRef , .5f *Time.deltaTime); // TODO: Check this smoothing
|
||||
}
|
||||
|
||||
// Update the speed, called every frame
|
||||
private void UpdateCurrentSpeed(){
|
||||
Debug.Log(data.moveSpeed);
|
||||
outputSpeed = Mathf.Lerp(outputSpeed, data.moveSpeed, data.moveSpeedSoothing * Time.deltaTime);
|
||||
resolvedMovement.moveSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, data.moveSpeed.value, data.moveSpeed.smoothing * Time.deltaTime);
|
||||
}
|
||||
|
||||
// Update the gravity, called every frame
|
||||
private void UpdateCurrentGravity(){
|
||||
// Accelerate gravity
|
||||
data.gravityPower += smoothedGravityAccel * Time.deltaTime;
|
||||
data.gravityPower = Mathf.Clamp(data.gravityPower, Mathf.NegativeInfinity, data.gravityMax);
|
||||
if (!controller.isGrounded){
|
||||
resolvedMovement.gravity -= data.gravityAcceleration.value * Time.deltaTime;
|
||||
}
|
||||
|
||||
// resolvedMovement.gravity = Mathf.Clamp(resolvedMovement.gravity, Mathf.NegativeInfinity, data.gravityMax.value);
|
||||
|
||||
// Apply a constant gravity if the player is grounded
|
||||
if (controller.isGrounded) {
|
||||
data.gravityPower = .1f;
|
||||
// resolvedMovement.gravity = -.3f;
|
||||
}
|
||||
|
||||
// Create the final gravity value
|
||||
float gravityMoveDirection = data.jumpPower + (Physics.gravity.y * data.gravityPower);
|
||||
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
|
||||
outputMoveDirection.y = Mathf.SmoothDamp(outputMoveDirection.y, gravityMoveDirection, ref gravitySmoothVelocityRef, .1f * Time.deltaTime);
|
||||
@@ -306,11 +314,11 @@ 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);
|
||||
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, settingsChangeRotationSpeed * Time.deltaTime);
|
||||
outputRotationSpeed = Mathf.Lerp(outputRotationSpeed, data.rotationSpeed.value, data.rotationSpeed.smoothing * Time.deltaTime);
|
||||
|
||||
// Set final rotation
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, outputRotation, outputRotationSpeed * Time.deltaTime).Flatten(0, null, 0);
|
||||
@@ -318,7 +326,7 @@ namespace Reset.Units{
|
||||
|
||||
// Move with default settings
|
||||
public void DoMovement(){
|
||||
DoMovement(outputMoveDirection, outputSpeed, data.gravityScale);
|
||||
DoMovement(outputMoveDirection, outputSpeed, data.gravityScale.value); // TODO: Gets multiplied a second time in DoMovement by gravity scale????
|
||||
}
|
||||
|
||||
// Custom move from input
|
||||
@@ -327,12 +335,12 @@ namespace Reset.Units{
|
||||
|
||||
// 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, moveDir.y, 0f);
|
||||
Vector3 moveYDir = new Vector3(0f, resolvedMovement.gravity, 0f);
|
||||
Vector3 addDir = additionalMoveDirection;
|
||||
|
||||
// Add their related speeds
|
||||
moveXZDir *= speed * Time.deltaTime;
|
||||
moveYDir *= smoothedGravityScale * Time.deltaTime;
|
||||
moveYDir *= data.gravityScale.value * Time.deltaTime;
|
||||
addDir *= additionalSpeed * Time.deltaTime;
|
||||
|
||||
// Construct the direction and move
|
||||
@@ -343,14 +351,6 @@ namespace Reset.Units{
|
||||
void LateUpdate(){
|
||||
UpdateGravityLate();
|
||||
DecayAdditionalDirection();
|
||||
SmoothingSettingsChanges();
|
||||
}
|
||||
|
||||
private void SmoothingSettingsChanges(){
|
||||
smoothedJumpDecay = Mathf.Lerp(smoothedJumpDecay, data.jumpPowerDecay, data.settingsChangeSmoothing * Time.deltaTime);
|
||||
smoothedGravityAccel = Mathf.Lerp(smoothedGravityAccel, data.gravityAcceleration, data.settingsChangeSmoothing * Time.deltaTime);
|
||||
smoothedGravityScale = Mathf.Lerp(smoothedGravityScale, data.gravityScale, data.settingsChangeSmoothing * Time.deltaTime);
|
||||
settingsChangeRotationSpeed = Mathf.Lerp(settingsChangeRotationSpeed, data.rotationSpeed, data.settingsChangeSmoothing * Time.deltaTime);
|
||||
}
|
||||
|
||||
void DecayAdditionalDirection(){
|
||||
@@ -367,21 +367,21 @@ namespace Reset.Units{
|
||||
|
||||
// Decay the direction
|
||||
if (inputMovement.magnitude < currentNoY.magnitude) {
|
||||
additionalMoveDirection = Vector3.Slerp(additionalMoveDirection, Vector3.zero,data.accelerationSmoothing * 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.deaccelerationSmoothing * Time.deltaTime);
|
||||
additionalMoveDirection = Vector3.Lerp(additionalMoveDirection, Vector3.zero, data.deaccerlation.value * Time.deltaTime);
|
||||
}
|
||||
|
||||
// Decay the gravity
|
||||
additionalMoveDirection.y -= data.gravityPower;
|
||||
additionalMoveDirection.y -= data.gravityPower.value;
|
||||
additionalMoveDirection.y = Mathf.Max(0f, additionalMoveDirection.y);
|
||||
}
|
||||
|
||||
private void UpdateGravityLate(){
|
||||
// Decay jump power
|
||||
data.jumpPower -= smoothedJumpDecay * Time.deltaTime;
|
||||
data.jumpPower = Mathf.Max(0f, data.jumpPower);
|
||||
data.jumpPower.value -= data.jumpPowerDecay.value * Time.deltaTime;
|
||||
data.jumpPower.value = Mathf.Max(0f, data.jumpPower.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user