change: fixing of many issues in handler, deprecation/deletion of variables, cleanup of UnitMovementHandler, cleaning up of inspector

This commit is contained in:
Chris
2025-09-23 14:14:47 -04:00
parent b21adf93e2
commit bd2903a0b2
10 changed files with 445 additions and 838 deletions

View File

@@ -212,615 +212,109 @@ MonoBehaviour:
additionalMoveDirection: {x: 0, y: 0, z: 0} additionalMoveDirection: {x: 0, y: 0, z: 0}
data: data:
moveSmoothing: moveSmoothing:
value: {x: 0, y: 0} targetValue: {x: 0.5, y: 0.5}
smoothing: 1 currentValue: {x: 0.5, y: 0.5}
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: {x: 0.5, y: 0.5}
refVel: {x: 0, y: 0} defaultSmoothing: 1
defaultEasing: 2
acceleration: acceleration:
value: 2.3 targetValue: 10
smoothing: 1 currentValue: 1.5
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 5
refVel: 0 defaultSmoothing: 1
defaultEasing: 2
deacceleration: deacceleration:
value: 1 targetValue: 20
smoothing: 7.46 currentValue: 2
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 5
refVel: 0 defaultSmoothing: 1
defaultEasing: 2
airDirectionDecay: airDirectionDecay:
value: 1 targetValue: 1
smoothing: 1 currentValue: 1
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 1
refVel: 0 defaultSmoothing: 1
defaultEasing: 2
softening: softening:
value: 5 targetValue: 4
smoothing: 1 currentValue: 1
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 1
refVel: 0 defaultSmoothing: 1
defaultEasing: 2
moveSpeed: moveSpeed:
value: 10 targetValue: 15
smoothing: 10 currentValue: 15
easing: 2 targetSmoothing: 10
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 15
refVel: 0 defaultSmoothing: 10
jumpPower: defaultEasing: 2
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPowerDecay:
value: 4
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityPower: gravityPower:
value: 0.1 targetValue: 1
smoothing: 1 currentValue: 1
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 1
refVel: 0 defaultSmoothing: 1
defaultEasing: 2
gravityMax: gravityMax:
value: 8 targetValue: 8
smoothing: 1 currentValue: 8
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 8
refVel: 0 defaultSmoothing: 1
defaultEasing: 2
gravityAcceleration: gravityAcceleration:
value: 3 targetValue: 5
smoothing: 1 currentValue: 5
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 1
refVel: 0 defaultSmoothing: 1
defaultEasing: 2
gravityScale: gravityScale:
value: 1 targetValue: 1
smoothing: 1 currentValue: 1
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 1
refVel: 0 defaultSmoothing: 1
defaultEasing: 2
rotateFacing: rotateFacing:
rid: -2 targetValue: 4
currentValue: 4
targetSmoothing: 1
targetEasing: 2
defaultValue: 4
defaultSmoothing: 1
defaultEasing: 2
rotationSpeed: rotationSpeed:
value: 0 targetValue: 5
smoothing: 1 currentValue: 5
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 5
refVel: 0 defaultSmoothing: 1
defaultEasing: 2
rotationInputBlending: rotationInputBlending:
value: 0 targetValue: 0.3
smoothing: 1 currentValue: 0.3
easing: 2 targetSmoothing: 1
currentSmoothing: 0 targetEasing: 2
currentValue: 0 defaultValue: 0.3
refVel: 0 defaultSmoothing: 1
smoothing: defaultEasing: 2
moveSmoothing:
value: {x: 0, y: 0}
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: {x: 0, y: 0}
acceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
deacceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
airDirectionDecay:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
softening:
value: 0
smoothing: 10
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
moveSpeed:
value: 0
smoothing: 10
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPower:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPowerDecay:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityPower:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityMax:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityAcceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityScale:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
rotateFacing:
rid: -2
rotationSpeed:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
rotationInputBlending:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
easing:
moveSmoothing:
value: {x: 0, y: 0}
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: {x: 0, y: 0}
acceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
deacceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
airDirectionDecay:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
softening:
value: 0
smoothing: 10
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
moveSpeed:
value: 0
smoothing: 10
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPower:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPowerDecay:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityPower:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityMax:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityAcceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityScale:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
rotateFacing:
rid: -2
rotationSpeed:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
rotationInputBlending:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
defaultData:
moveSmoothing:
value: {x: 0, y: 0}
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: {x: 0, y: 0}
acceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
deacceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
airDirectionDecay:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
softening:
value: 0
smoothing: 10
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
moveSpeed:
value: 0
smoothing: 10
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPower:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPowerDecay:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityPower:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityMax:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityAcceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityScale:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
rotateFacing:
rid: -2
rotationSpeed:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
rotationInputBlending:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
defaultSmoothing:
moveSmoothing:
value: {x: 0, y: 0}
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: {x: 0, y: 0}
acceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
deacceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
airDirectionDecay:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
softening:
value: 0
smoothing: 10
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
moveSpeed:
value: 0
smoothing: 10
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPower:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPowerDecay:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityPower:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityMax:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityAcceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityScale:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
rotateFacing:
rid: -2
rotationSpeed:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
rotationInputBlending:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
defaultEasing:
moveSmoothing:
value: {x: 0, y: 0}
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: {x: 0, y: 0}
acceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
deacceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
airDirectionDecay:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
softening:
value: 0
smoothing: 10
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
moveSpeed:
value: 0
smoothing: 10
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPower:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
jumpPowerDecay:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityPower:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityMax:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityAcceleration:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
gravityScale:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
rotateFacing:
rid: -2
rotationSpeed:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
rotationInputBlending:
value: 0
smoothing: 1
easing: 2
currentSmoothing: 0
currentValue: 0
refVel: 0
references:
version: 2
RefIds:
- rid: -2
type: {class: , ns: , asm: }
--- !u!114 &679199079481597207 --- !u!114 &679199079481597207
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@@ -33,7 +33,7 @@ namespace Reset.Units {
// ValueGroup.ChangeSmoothingEasing(jumpPower, ref agent.data.jumpPower.currentSmoothing, // ValueGroup.ChangeSmoothingEasing(jumpPower, ref agent.data.jumpPower.currentSmoothing,
// ref agent.data.jumpPower.easing, ref agent.defaultData.jumpPower.smoothing, ref agent.defaultData.jumpPower.easing); // ref agent.data.jumpPower.easing, ref agent.defaultData.jumpPower.smoothing, ref agent.defaultData.jumpPower.easing);
FloatValueGroup.UpdateValue(jumpPowerDecay, agent.data.jumpPowerDecay); // FloatValueGroup.UpdateValue(jumpPowerDecay, agent.data.jumpPowerDecay);
// Deprecated by SetNewGravity // Deprecated by SetNewGravity
// FloatValueGroup.UpdateValue(gravityPower, ref agent.data.gravityPower.value, ref agent.defaultData.gravityPower.value); // FloatValueGroup.UpdateValue(gravityPower, ref agent.data.gravityPower.value, ref agent.defaultData.gravityPower.value);

View File

@@ -13,10 +13,45 @@ public static class MathExtensions{
return (to - origin).normalized; return (to - origin).normalized;
} }
public static Vector3 ToVector3(this Vector2 vector2){
return new Vector3(vector2.x, 0f, vector2.y);
}
public static Vector2 ToVector2(this Vector3 vector3){
return new Vector2(vector3.x, vector3.z);
}
public static Color Alpha(this Color input, float newAlpha){ public static Color Alpha(this Color input, float newAlpha){
return new Color(input.r, input.g, input.b, newAlpha); return new Color(input.r, input.g, input.b, newAlpha);
} }
public static Quaternion SmoothDamp(Quaternion rot, Quaternion target, ref Quaternion deriv, float time) {
if (Time.deltaTime < Mathf.Epsilon) return rot;
// account for double-cover
var Dot = Quaternion.Dot(rot, target);
var Multi = Dot > 0f ? 1f : -1f;
target.x *= Multi;
target.y *= Multi;
target.z *= Multi;
target.w *= Multi;
// smooth damp (nlerp approx)
var Result = new Vector4(
Mathf.SmoothDamp(rot.x, target.x, ref deriv.x, time),
Mathf.SmoothDamp(rot.y, target.y, ref deriv.y, time),
Mathf.SmoothDamp(rot.z, target.z, ref deriv.z, time),
Mathf.SmoothDamp(rot.w, target.w, ref deriv.w, time)
).normalized;
// ensure deriv is tangent
var derivError = Vector4.Project(new Vector4(deriv.x, deriv.y, deriv.z, deriv.w), Result);
deriv.x -= derivError.x;
deriv.y -= derivError.y;
deriv.z -= derivError.z;
deriv.w -= derivError.w;
return new Quaternion(Result.x, Result.y, Result.z, Result.w);
}
public static Vector2 Rotate(this Vector2 v, float delta) { public static Vector2 Rotate(this Vector2 v, float delta) {
return new Vector2( return new Vector2(
v.x * Mathf.Cos(delta) - v.y * Mathf.Sin(delta), v.x * Mathf.Cos(delta) - v.y * Mathf.Sin(delta),

View File

@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using UnityEngine;
public struct CameraSettingData : ICloneable{
public SettingValue<float> mainFieldOfView;
public SettingValue<Vector3> orbitPositionDamping;
public SettingValue<Vector3> orbitTargetOffset;
public SettingValue<bool> axisLookEnabledX;
public SettingValue<bool> axisLookEnabledY;
public SettingValue<float> axisLookGainX;
public SettingValue<float> axisLookGainY;
public SettingValue<float> orbitFollowTopHeight;
public SettingValue<float> orbitFollowTopRadius;
public SettingValue<float> orbitFollowCenterHeight;
public SettingValue<float> orbitFollowCenterRadius;
public SettingValue<float> orbitFollowBottomHeight;
public SettingValue<float> orbitFollowBottomRadius;
public SettingValue<Vector2> rotationComposerScreenPos;
public SettingValue<Vector3> cameraOffsetOffset;
public object Clone(){
return MemberwiseClone();
}
public List<IResettableSettingValue> GetAllSettings(){
var outputList = new List<IResettableSettingValue>();
IResettableSettingValue[] settings = new[]{
mainFieldOfView as IResettableSettingValue,
orbitPositionDamping,
orbitTargetOffset,
axisLookEnabledX,
axisLookEnabledY,
axisLookGainX,
axisLookGainY,
orbitFollowTopHeight,
orbitFollowTopRadius,
orbitFollowCenterHeight,
orbitFollowCenterRadius,
orbitFollowBottomHeight,
orbitFollowBottomRadius,
rotationComposerScreenPos,
cameraOffsetOffset,
};
outputList.AddRange(settings);
return outputList;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 14393bffe6324344b5f972412e37a339
timeCreated: 1758568723

View File

@@ -1,37 +1,8 @@
using System;
using Reset.Core; using Reset.Core;
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
using Unity.Cinemachine; using Unity.Cinemachine;
using UnityEngine; using UnityEngine;
public struct CameraSettingData : ICloneable{
public SettingValue<float> mainFieldOfView;
public SettingValue<Vector3> orbitPositionDamping;
public SettingValue<Vector3> orbitTargetOffset;
public SettingValue<bool> axisLookEnabledX;
public SettingValue<bool> axisLookEnabledY;
public SettingValue<float> axisLookGainX;
public SettingValue<float> axisLookGainY;
public SettingValue<float> orbitFollowTopHeight;
public SettingValue<float> orbitFollowTopRadius;
public SettingValue<float> orbitFollowCenterHeight;
public SettingValue<float> orbitFollowCenterRadius;
public SettingValue<float> orbitFollowBottomHeight;
public SettingValue<float> orbitFollowBottomRadius;
public SettingValue<Vector2> rotationComposerScreenPos;
public SettingValue<Vector3> cameraOffsetOffset;
public object Clone(){
return MemberwiseClone();
}
}
public class CameraSettingsProcessor : MonoBehaviour{ public class CameraSettingsProcessor : MonoBehaviour{
public static CameraSettingsProcessor Instance{ get; private set; } public static CameraSettingsProcessor Instance{ get; private set; }
@@ -89,7 +60,7 @@ public class CameraSettingsProcessor : MonoBehaviour{
data.rotationComposerScreenPos.targetValue = rotComp.Composition.ScreenPosition; data.rotationComposerScreenPos.targetValue = rotComp.Composition.ScreenPosition;
data.cameraOffsetOffset.targetValue = offset.Offset; data.cameraOffsetOffset.targetValue = offset.Offset;
// And copy to the original // And copy to the original
original = (CameraSettingData)data.Clone(); original = (CameraSettingData)data.Clone();
} }
@@ -142,4 +113,13 @@ public class CameraSettingsProcessor : MonoBehaviour{
offset.Offset = data.cameraOffsetOffset.Value; offset.Offset = data.cameraOffsetOffset.Value;
} }
[Button]
void InitializeAllSettings(){
var allSettings = data.GetAllSettings();
for (int i = 0; i < allSettings.Count; i++) {
allSettings[i].Initialize();
allSettings[i].Verify();
}
}
} }

View File

@@ -1,4 +1,5 @@
using Sirenix.OdinInspector; using Reset.Core.Tools;
using Sirenix.OdinInspector;
using UnityEngine; using UnityEngine;
namespace Reset.Units{ namespace Reset.Units{
@@ -6,25 +7,50 @@ namespace Reset.Units{
public struct MoveDirection{ public struct MoveDirection{
private Transform owner; private Transform owner;
private Vector2 _moveDir; // Always local private Vector2 _moveRaw;
private Vector2 _moveDir; // Always world??
[ShowInInspector]
public Vector2 World{ public Vector2 World{
get => owner.TransformDirection(_moveDir); get{
set{ DebugOverlayDrawer.ChangeValue("Movement", "_moveDir", _moveDir);
_moveDir = owner.InverseTransformDirection(value); // return owner.TransformDirection(_moveDir.ToVector3()).ToVector2();
return _moveDir;
} }
set{
_moveDir = value;
}
} }
[ShowInInspector]
public Vector2 Local{ public Vector2 Local{
get => _moveDir; get => owner.InverseTransformDirection(_moveDir.ToVector3()).ToVector2();
set {
_moveDir = value; private set {
// _moveDir = value;
} }
} }
public Vector2 RawWorld{
get{
DebugOverlayDrawer.ChangeValue("Movement", "_moveRaw", _moveRaw);
// return owner.TransformDirection(_moveDir.ToVector3()).ToVector2();
return _moveRaw;
}
set{
_moveRaw = value;
}
}
public Vector2 RawLocal{
get => owner.InverseTransformDirection(_moveRaw.ToVector3()).ToVector2();
}
public MoveDirection(Transform ownerTransform){ public MoveDirection(Transform ownerTransform){
owner = ownerTransform; owner = ownerTransform;
_moveDir = Vector2.zero; _moveDir = Vector2.zero;
_moveRaw = Vector2.zero;
} }
} }

View File

@@ -1,39 +1,53 @@
using System; using System;
using Sirenix.OdinInspector;
using UnityEngine; using UnityEngine;
using UnityEngine.Serialization; using UnityEngine.Serialization;
public interface IResettableSettingValue{ public interface IResettableSettingValue{
public abstract void SmoothAndEase(); public void Verify();
public void SmoothAndEase();
public void Initialize();
} }
[Serializable] [Serializable]
public struct SettingValue<T> : IResettableSettingValue{ public class SettingValue<T> : IResettableSettingValue{
public T targetValue; [HorizontalGroup("Settings", width: .3f), VerticalGroup("Settings/Value"), BoxGroup("Settings/Value/Value"), LabelText("Target")]
public T currentValue; public T targetValue;
public T refVel; [HorizontalGroup("Settings"), VerticalGroup("Settings/Value"), BoxGroup("Settings/Value/Value"), LabelText("Current")]
public T currentValue;
public float targetSmoothing; // Smoothing changes how fast the value is changed. [HorizontalGroup("Settings"), VerticalGroup("Settings/Smoothing"), BoxGroup("Settings/Smoothing/Smoothing"), LabelText("Target"), ShowIf("@IsSmoothable()")]
public float targetEasing; // Easing changes how fast smoothing is changed, when given a new value. public float targetSmoothing; // Smoothing changes how fast the value is changed.
[HorizontalGroup("Settings"), VerticalGroup("Settings/Easing"), BoxGroup("Settings/Easing/Easing"), LabelText("Target"), ShowIf("@IsSmoothable()")]
public float targetEasing; // Easing changes how fast smoothing is changed, when given a new value.
public T Value{ public T Value{
get => currentValue; get => currentValue;
set => targetValue = value; set => targetValue = value;
} }
[HorizontalGroup("Settings"), VerticalGroup("Settings/Smoothing"), BoxGroup("Settings/Smoothing/Smoothing"), LabelText("Current"), ShowIf("@IsSmoothable()")]
private float currentSmoothing; private float currentSmoothing;
[HorizontalGroup("Settings"), VerticalGroup("Settings/Value"), BoxGroup("Settings/Value/Value"), LabelText("Default")]
public T defaultValue; public T defaultValue;
[HorizontalGroup("Settings"), VerticalGroup("Settings/Smoothing"), BoxGroup("Settings/Smoothing/Smoothing"), LabelText("Default"), ShowIf("@IsSmoothable()")]
public float defaultSmoothing; public float defaultSmoothing;
[HorizontalGroup("Settings"), VerticalGroup("Settings/Easing"), BoxGroup("Settings/Easing/Easing"), LabelText("Default"), ShowIf("@IsSmoothable()")]
public float defaultEasing; public float defaultEasing;
private float refVelFloat; // For use with SmoothDamp private float refVelFloat; // For use with SmoothDamp
private Vector3 refVelV3; // For use with SmoothDamp private Vector3 refVelV3; // For use with SmoothDamp
private Vector2 refVelV2; // For use with SmoothDamp private Vector2 refVelV2; // For use with SmoothDamp
private bool verified;
bool IsSmoothable(){
return (typeof(T) == typeof(float) || typeof(T) == typeof(Vector2) || typeof(T) == typeof(Vector3) || typeof(T) == typeof(Vector4) || typeof(T) == typeof(Quaternion) );
}
public SettingValue(T initValue, float defaultEasing = 2f, float defaultSmoothing = 1f){ public SettingValue(T initValue, float defaultEasing = 2f, float defaultSmoothing = 1f){
targetValue = initValue; targetValue = initValue;
defaultValue = initValue;
this.defaultSmoothing = defaultSmoothing; this.defaultSmoothing = defaultSmoothing;
this.defaultEasing = defaultEasing; this.defaultEasing = defaultEasing;
@@ -46,14 +60,20 @@ public struct SettingValue<T> : IResettableSettingValue{
refVelFloat = 0; refVelFloat = 0;
refVelV3 = default; refVelV3 = default;
refVelV2 = default; refVelV2 = default;
refVel = default;
defaultValue = initValue;
} }
public void Verify(){
if (targetValue.Equals(currentValue) == false) {
Debug.LogWarning($"A SettingValue ({this}) doesn't have its current and target value matching. This should have been set!");
}
verified = true;
}
public void SmoothAndEase(){ public void SmoothAndEase(){
Debug.Log("Worked!"); if (!verified) {
return; Debug.LogWarning($"A SettingValue ({this}) wasn't verified before being smoothed and eased. What's up with that?");
}
currentSmoothing = Mathf.MoveTowards(currentSmoothing, targetSmoothing, targetEasing * 1f * Time.deltaTime); currentSmoothing = Mathf.MoveTowards(currentSmoothing, targetSmoothing, targetEasing * 1f * Time.deltaTime);
@@ -77,4 +97,9 @@ public struct SettingValue<T> : IResettableSettingValue{
currentValue = (T)(object)new Vector4(v3value.x, v3value.y, v3value.z, v4value); currentValue = (T)(object)new Vector4(v3value.x, v3value.y, v3value.z, v4value);
} }
} }
public void Initialize(){
currentValue = targetValue;
Debug.Log(Value);
}
} }

View File

@@ -8,31 +8,31 @@ namespace Reset.Units{
[Serializable] [Serializable]
public class UnitMovementData : ICloneable{ public class UnitMovementData : ICloneable{
// Movement Direction // Movement Direction
public SettingValue<Vector2> moveSmoothing = new SettingValue<Vector2>(new Vector2(.5f, .5f)); [Title("Move Smoothing"), HideLabel, InlineProperty] public SettingValue<Vector2> moveSmoothing = new SettingValue<Vector2>(new Vector2(.5f, .5f));
public SettingValue<float> acceleration = new SettingValue<float>(5f); [Title("Acceleration"), HideLabel, InlineProperty] public SettingValue<float> acceleration = new SettingValue<float>(5f);
public SettingValue<float> deacceleration = new SettingValue<float>(5f); [Title("Deacceleration"), HideLabel, InlineProperty] public SettingValue<float> deacceleration = new SettingValue<float>(5f);
[SliderField(0,1)] [SliderField(0,1)]
public SettingValue<float> airDirectionDecay = new SettingValue<float>(1f); // TODO: Check default value [Title("Air Direction Decay"), HideLabel, InlineProperty] public SettingValue<float> airDirectionDecay = new SettingValue<float>(1f); // TODO: Check default value
// Move Speed // Move Speed
public SettingValue<float> softening = new SettingValue<float>(1f, defaultSmoothing: 1f); [Title("Softening"), HideLabel, InlineProperty] public SettingValue<float> softening = new SettingValue<float>(1f, defaultSmoothing: 1f);
public SettingValue<float> moveSpeed = new SettingValue<float>(15f, defaultSmoothing: 10f); [Title("Move Speed"), HideLabel, InlineProperty] public SettingValue<float> moveSpeed = new SettingValue<float>(15f, defaultSmoothing: 10f);
// Jumping // Jumping
[ShowInInspector] public SettingValue<float> jumpPower = new SettingValue<float>(0f); // [Title("Jump Power"), HideLabel, InlineProperty] [ShowInInspector] public SettingValue<float> jumpPower = new SettingValue<float>(0f);
public SettingValue<float> jumpPowerDecay = new SettingValue<float>(3f); // TODO: Check default value // [Title("Move Smoothing"), HideLabel, InlineProperty] public SettingValue<float> jumpPowerDecay = new SettingValue<float>(3f); // TODO: Check default value
// Gravity // Gravity
[ShowInInspector] public SettingValue<float> gravityPower = new SettingValue<float>(1f); [Title("Gravity Power"), HideLabel, InlineProperty] [ShowInInspector] public SettingValue<float> gravityPower = new SettingValue<float>(1f);
public SettingValue<float> gravityMax = new SettingValue<float>(8f); [Title("Gravity Max"), HideLabel, InlineProperty] public SettingValue<float> gravityMax = new SettingValue<float>(8f);
public SettingValue<float> gravityAcceleration = new SettingValue<float>(1f); [Title("Gravity Acceleration"), HideLabel, InlineProperty] public SettingValue<float> gravityAcceleration = new SettingValue<float>(1f);
public SettingValue<float> gravityScale = new SettingValue<float>(1f); [Title("Gravity Scale"), HideLabel, InlineProperty] public SettingValue<float> gravityScale = new SettingValue<float>(1f);
// Rotation // Rotation
[ShowInInspector, SerializeReference] public Enum rotateFacing; [Title("Rotate Facing"), HideLabel, InlineProperty] public SettingValue<PlayerFacingDirection> rotateFacing = new SettingValue<PlayerFacingDirection>(initValue: PlayerFacingDirection.Momentum);
public SettingValue<float> rotationSpeed = new SettingValue<float>(5f); [Title("Rotation Speed"), HideLabel, InlineProperty] public SettingValue<float> rotationSpeed = new SettingValue<float>(5f);
public SettingValue<float> rotationInputBlending = new SettingValue<float>(.3f); [Title("Rotation Input Blending"), HideLabel, InlineProperty] public SettingValue<float> rotationInputBlending = new SettingValue<float>(.3f);
public object Clone(){ public object Clone(){
return MemberwiseClone(); return MemberwiseClone();
@@ -41,13 +41,25 @@ namespace Reset.Units{
public List<IResettableSettingValue> GetAllSettings(){ public List<IResettableSettingValue> GetAllSettings(){
var outputList = new List<IResettableSettingValue>(); var outputList = new List<IResettableSettingValue>();
IResettableSettingValue[] test = new[]{ IResettableSettingValue[] settings = new[]{
moveSmoothing as IResettableSettingValue, moveSmoothing as IResettableSettingValue,
acceleration,
deacceleration,
airDirectionDecay,
softening,
moveSpeed,
// jumpPower,
// jumpPowerDecay,
gravityPower,
gravityMax,
gravityAcceleration,
gravityScale,
rotateFacing,
rotationSpeed,
rotationInputBlending,
}; };
test[0].SmoothAndEase(); outputList.AddRange(settings);
outputList.AddRange(test);
return outputList; return outputList;
} }

View File

@@ -1,19 +1,20 @@
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Reset.Core.Tools; using Reset.Core.Tools;
using Sirenix.OdinInspector; using Sirenix.OdinInspector;
namespace Reset.Units{ namespace Reset.Units{
public class UnitMovementHandler : MonoBehaviour{ public class UnitMovementHandler : MonoBehaviour{
[ShowInInspector] [ShowInInspector, InlineProperty, HideLabel, FoldoutGroup("Resolved Movement", expanded: true)]
public ResolvedMovement resolvedMovement; public ResolvedMovement resolvedMovement;
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private float outputSpeed; [HideInInspector] public Vector3 outputMoveDirection;
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private float additionalSpeed; [HideInInspector] public Vector3 additionalMoveDirection;
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] public Vector3 outputMoveDirection;
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] public Vector3 additionalMoveDirection; // SmoothDamp Velocities
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private Quaternion outputRotation; private Quaternion refVelocityRotationSpeed;
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private Quaternion specifiedRotation; private float refVelocityAcceleration;
[FoldoutGroup("Final Values"), ShowInInspector, ReadOnly] private float outputRotationSpeed; private float refVelocityDeacceleration;
// Smoothing Values // Smoothing Values
private float directionChangeDotLerp; private float directionChangeDotLerp;
@@ -26,112 +27,69 @@ namespace Reset.Units{
private LockOnManager lockOnManager; private LockOnManager lockOnManager;
// Movement Data // Movement Data
[ShowInInspector, PropertyOrder(2)] public UnitMovementData data = new(); [ShowInInspector, PropertyOrder(2), FoldoutGroup("Movement Data", expanded: true), InlineProperty, HideLabel] public UnitMovementData data = new();
void Awake(){ void Awake(){
controller = GetComponent<CharacterController>(); controller = GetComponent<CharacterController>();
controls = GetComponent<PlayerControls>(); controls = GetComponent<PlayerControls>();
lockOnManager = GetComponent<LockOnManager>(); lockOnManager = GetComponent<LockOnManager>();
InitAllSettings();
} }
void Start(){ void Start(){
resolvedMovement = new ResolvedMovement(); resolvedMovement = new ResolvedMovement{
resolvedMovement.moveDirection = new ResolvedMovement.MoveDirection(transform); moveDirection = new ResolvedMovement.MoveDirection(transform)
};
} }
void Update(){ void Update(){
SmoothAllSettings();
UpdateCurrentDirection(); UpdateCurrentDirection();
UpdateCurrentGravity(); UpdateCurrentGravity();
UpdateCurrentSpeed(); UpdateCurrentSpeed();
// UpdateCurrentRotation(); UpdateCurrentRotation();
DoMovement(); DoMovement();
}
// Add directly to the direction
public void AddToCurrentDirection(Vector3 inputDirection, float power){ // Old
additionalMoveDirection += inputDirection.normalized;
additionalSpeed = power;
}
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? // Old
additionalMoveDirection = inputDirection.Flatten(null, 0f, null);
}
public void SetNewGravity(float value){
additionalMoveDirection.y = value;
}
// 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){ // 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);
// Multiply it by the current magnitude (why? i forgor) DebugOverlayDrawer.ChangeValue("Movement", "Move Direction (Local)", resolvedMovement.moveDirection.Local);
dirToAdd *= controls.rawMoveInput.magnitude; DebugOverlayDrawer.ChangeValue("Movement", "Move Direction (World)", resolvedMovement.moveDirection.World);
// Blend the existing direction into it
dirToAdd = Vector3.Lerp(outputMoveDirection, dirToAdd, priority * controls.rawMoveInput.magnitude);
// Set the new direction
outputMoveDirection = new Vector3(dirToAdd.x, outputMoveDirection.y, dirToAdd.z);
// Everthing under here is for the speed now. If it's not set do...nothing. Otherwise set it to the max value between move speed and the new speed
if (float.IsPositiveInfinity(speed)) {
return;
}
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 // Update the direction, called every frame
private void UpdateCurrentDirection(){ private void UpdateCurrentDirection(){
// Get input value // Get input value
Vector2 targetDirection = new Vector2(controls.rawMoveInput.x, controls.rawMoveInput.y); Vector2 targetDirection = new Vector2(controls.rawMoveInput.x, controls.rawMoveInput.y);
targetDirection = (Camera.main.transform.rotation * targetDirection.ToVector3()).ToVector2();
// Deadzone // Deadzone
if (targetDirection.magnitude < .05f) { if (targetDirection.magnitude < .08f) {
targetDirection = Vector2.zero; targetDirection = Vector2.zero;
} }
// Set Raw Direciton
resolvedMovement.moveDirection.RawWorld = targetDirection;
// Get current direction // Get current direction
Vector2 currentDirection = resolvedMovement.moveDirection.Local; Vector2 currentDirection = resolvedMovement.moveDirection.World;
// Also need to find the dot value of the current input versus the current move direction // Also need to find the dot value of the current input versus the current move direction
Vector3 slerpedValue;
Vector2 lerpedValue;
Vector2 newDirection;
float switchedDirection = Vector3.Dot(targetDirection, currentDirection); float switchedDirection = Vector3.Dot(targetDirection, currentDirection);
float switchedDirectionRemapped = Mathf.Lerp(0, 1, switchedDirection); float switchedDirectionRemapped = Mathf.Lerp(0, 1, switchedDirection);
directionChangeDotLerp = Mathf.Lerp(switchedDirection, switchedDirectionRemapped, 5f * Time.deltaTime) ; // turn that .5f into a variable directionChangeDotLerp = Mathf.Lerp(switchedDirection, switchedDirectionRemapped, 5f * Time.deltaTime) ; // turn that .5f into a variable
DebugOverlayDrawer.ChangeValue("Movement", "Direction Change Dot", directionChangeDotLerp);
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 // 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 // Also checks when grounded to only use Slerp on the ground
Vector3 slerpedValue;
Vector2 lerpedValue;
Vector2 newDirection;
if (controller.isGrounded){ 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 slerpedValue = Vector3.Slerp(currentDirection, targetDirection, data.softening.Value * Time.deltaTime);
lerpedValue = Vector2.Lerp(currentDirection, targetDirection, data.softening.Value * Time.deltaTime); lerpedValue = Vector2.Lerp(currentDirection, targetDirection, data.softening.Value * Time.deltaTime);
newDirection = Vector2.Lerp(slerpedValue, lerpedValue, directionChangeDotLerp); newDirection = Vector2.Lerp(slerpedValue, lerpedValue, directionChangeDotLerp);
@@ -139,29 +97,23 @@ namespace Reset.Units{
newDirection = Vector2.Lerp(currentDirection, targetDirection, data.softening.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 the new direction
resolvedMovement.moveDirection.World = newDirection;
// 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 = newDirection;
} }
// Update the speed, called every frame // Update the speed, called every frame
private void UpdateCurrentSpeed(){ private void UpdateCurrentSpeed(){
float speed; // ""Smooth"" the speed
float smoothedSpeed;
if (resolvedMovement.moveDirection.Local.magnitude < controls.rawMoveInput.magnitude) { if (resolvedMovement.moveDirection.Local.magnitude < controls.rawMoveInput.magnitude) {
speed = data.moveSpeed.Value * Time.deltaTime * data.acceleration.Value; smoothedSpeed = Mathf.MoveTowards(resolvedMovement.moveSpeed, data.moveSpeed.Value, data.acceleration.Value * Time.deltaTime);
} else { } else {
speed = data.moveSpeed.Value * Time.deltaTime * data.deacceleration.Value; smoothedSpeed = Mathf.MoveTowards(resolvedMovement.moveSpeed, 0f, data.deacceleration.Value * Time.deltaTime);
} }
resolvedMovement.moveSpeed = speed; // Commit the speed
resolvedMovement.moveSpeed = smoothedSpeed;
DebugOverlayDrawer.ChangeValue("Movement", "Resolved Speed", resolvedMovement.moveSpeed); DebugOverlayDrawer.ChangeValue("Movement", "Resolved Speed", resolvedMovement.moveSpeed);
} }
@@ -173,75 +125,61 @@ namespace Reset.Units{
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);
// Apply a constant gravity if the player is grounded
if (controller.isGrounded) {
// resolvedMovement.gravity = -.3f;
}
// Create the final gravity value // Create the final gravity value
float gravityMoveDirection = data.jumpPower.Value + (Physics.gravity.y * resolvedMovement.gravity); float gravityMoveDirection = 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);
} }
// Update the rotation, called every frame // Update the rotation, called every frame
private void UpdateCurrentRotation(){ private void UpdateCurrentRotation(){
// Get input value // Get input value
Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y); Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y);
Quaternion targetRotation = Quaternion.identity;
// Switch the desired rotation based on current movement setting // Switch the desired rotation based on current movement setting
switch (data.rotateFacing) { switch (data.rotateFacing.Value) { // TODO: Check that this isn't broken
// Just look at target // Just look at target
case PlayerFacingDirection.TowardsTarget: case PlayerFacingDirection.TowardsTarget:
// Look directly at the target // Look directly at the target
outputRotation = Quaternion.LookRotation(transform.position.DirectionTo(lockOnManager.mainTarget.gameObject.transform.position)); targetRotation = Quaternion.LookRotation(transform.position.DirectionTo(lockOnManager.mainTarget.gameObject.transform.position));
break; break;
case PlayerFacingDirection.Momentum: case PlayerFacingDirection.Momentum:
// Look towards the current direction the agent is moving // Look towards the current direction the agent is moving
if (inputMovement.magnitude > .05f){ if (inputMovement.magnitude > .05f){
outputRotation = Camera.main.transform.rotation * Quaternion.LookRotation(outputMoveDirection); targetRotation = Quaternion.LookRotation(resolvedMovement.moveDirection.RawWorld.ToVector3(), Vector3.up);
} }
break; break;
case PlayerFacingDirection.MatchForward: case PlayerFacingDirection.MatchForward:
// Look towards the input direction....why??? I guess cause move direction is PlayerFacingDirection.Momentum. // Look towards the input direction....why??? I guess cause move direction is PlayerFacingDirection.Momentum.
if (controls.rawMoveInput.magnitude < 0.05f) { break; } if (controls.rawMoveInput.magnitude < 0.05f) { break; }
outputRotation = Camera.main.transform.rotation * Quaternion.LookRotation(inputMovement); targetRotation = Camera.main.transform.rotation * Quaternion.LookRotation(inputMovement);
break; break;
case PlayerFacingDirection.MatchCamera: case PlayerFacingDirection.MatchCamera:
// Look the same direction as the camera // Look the same direction as the camera
outputRotation = Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0, null, 0)); targetRotation = Quaternion.Euler(Camera.main.transform.rotation.eulerAngles.Flatten(0, null, 0));
break; break;
case PlayerFacingDirection.Static: case PlayerFacingDirection.Static:
// Don't change // Don't change
outputRotation = transform.rotation; targetRotation = transform.rotation;
break;
case PlayerFacingDirection.SpecifiedDirection:
// Look at an inputed rotation
outputRotation = specifiedRotation;
break; break;
} }
DebugOverlayDrawer.ChangeValue("Rotation", "Target Rotation", targetRotation.eulerAngles);
// Add the current input into the created rotation // Add the current input into the created rotation
if (inputMovement.magnitude > .05){ if (inputMovement.magnitude > .05) {
outputRotation = Quaternion.Lerp(outputRotation, Camera.main.transform.rotation * Quaternion.LookRotation(inputMovement), data.rotationInputBlending.Value); // Quaternion inputRot = Camera.main.transform.rotation * Quaternion.LookRotation(inputMovement);
// resolvedMovement.rotation = Quaternion.RotateTowards(resolvedMovement.rotation, targetRotation, 10f);
resolvedMovement.rotation = targetRotation;
} }
// 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.softening.Value * Time.deltaTime);
// Set final rotation // Set final rotation
transform.rotation = Quaternion.Slerp(transform.rotation, outputRotation, outputRotationSpeed * Time.deltaTime).Flatten(0, null, 0); transform.rotation = Quaternion.Slerp(transform.rotation, resolvedMovement.rotation, data.rotationSpeed.Value * Time.deltaTime).Flatten(0, null, 0);
} }
// Move with default settings // Move with default settings
public void DoMovement(){ 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.World, resolvedMovement.moveSpeed, data.gravityScale.Value); // TODO: Gets multiplied a second time in DoMovement by gravity scale????
} }
// Custom move from input // Custom move from input
@@ -249,63 +187,100 @@ namespace Reset.Units{
// Debug.Log($"moveDir: {moveDir}, agent velocity: {transform.InverseTransformDirection(controller.velocity.normalized)}"); // 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 // Seperate the different move directions. Additonal becomes it's own because it needs to be added independently of the other two
Vector2 moveXZDir = new Vector3(moveDir.x, moveDir.y); Vector2 moveXZDir = moveDir;
float moveYDir = resolvedMovement.gravity; float moveYDir = resolvedMovement.gravity;
Vector3 addDir = additionalMoveDirection;
// Add their related speeds // Add their related speeds
moveXZDir *= speed * Time.deltaTime; 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 // Construct the direction and move
Vector3 finalDir = new Vector3(moveXZDir.x, moveYDir,moveXZDir.y) ; Vector3 finalDir = new Vector3(moveXZDir.x, moveYDir, moveXZDir.y);
controller.Move((Camera.main.transform.rotation.Flatten(0, null, 0) * finalDir) + addDir); controller.Move(finalDir);
}
void LateUpdate(){
UpdateGravityLate();
DecayAdditionalDirection();
}
void DecayAdditionalDirection(){
// Get input value
Vector3 inputMovement = new Vector3(controls.rawMoveInput.x, 0f, controls.rawMoveInput.y);
// Ignore values under deadzone
if (inputMovement.magnitude < .1f) {
inputMovement = Vector3.zero;
}
// Remove Y from variables
Vector3 currentNoY = new Vector3(additionalMoveDirection.x, 0f, additionalMoveDirection.z);
// Decay the direction
if (inputMovement.magnitude < currentNoY.magnitude) {
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.deacceleration.Value * Time.deltaTime);
}
// Decay the gravity
additionalMoveDirection.y -= data.gravityPower.Value;
additionalMoveDirection.y = Mathf.Max(0f, additionalMoveDirection.y);
} }
private void UpdateGravityLate(){ // Setting absolute to true will cause the current gravity to snap to the new gravity value.
// Decay jump power // Keeping it false will make it apply additively to the current gravity. Both options use relativty for linear interpolation.
data.jumpPower.Value -= data.jumpPowerDecay.Value * Time.deltaTime; public void SetNewGravity(float value, float relativity, bool absolute){ // new
data.jumpPower.Value = Mathf.Max(0f, data.jumpPower.Value); if (absolute){
resolvedMovement.gravity = Mathf.Lerp(resolvedMovement.gravity, value, relativity);
} else {
resolvedMovement.gravity = Mathf.Lerp(resolvedMovement.gravity, resolvedMovement.gravity + value, relativity);
}
} }
[Button("Initialize Settings")] public void SetNewDirection(Vector2 value, float relativity, bool absolute){ // new
if (absolute){
resolvedMovement.moveDirection.World = Vector2.Lerp(resolvedMovement.moveDirection.World, value, relativity);
} else {
resolvedMovement.moveDirection.World = Vector2.Lerp(resolvedMovement.moveDirection.World, resolvedMovement.moveDirection.World + value, relativity);
}
}
public void SetNewRawDirection(Vector2 value, float relativity, bool absolute){ // new
if (absolute){
resolvedMovement.moveDirection.RawWorld = Vector2.Lerp(resolvedMovement.moveDirection.RawWorld, value, relativity);
} else {
resolvedMovement.moveDirection.RawWorld = Vector2.Lerp(resolvedMovement.moveDirection.RawWorld, resolvedMovement.moveDirection.RawWorld + value, relativity);
}
}
public void SetNewSpeed(float value, float relativity, bool absolute){ // new
if (absolute){
resolvedMovement.moveSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, value, relativity);
} else {
resolvedMovement.moveSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, resolvedMovement.moveSpeed + value, relativity);
}
}
public void SetNewRotation(Quaternion value, float relativity, bool absolute){ // new
if (absolute){
resolvedMovement.rotation = Quaternion.Lerp(resolvedMovement.rotation, value, relativity);
} else {
resolvedMovement.rotation = Quaternion.Lerp(resolvedMovement.rotation, resolvedMovement.rotation * value, relativity);
}
}
[Button("Initialize Settings", ButtonHeight = 30), PropertySpace(10,5 )]
void InitAllSettings(){ void InitAllSettings(){
var newthing = data.GetAllSettings(); var settingsList = data.GetAllSettings();
foreach (IResettableSettingValue value in settingsList) {
value.Initialize();
value.Verify();
}
} }
void SmoothAllSettings(){ void SmoothAllSettings(){
var settingsList = data.GetAllSettings();
foreach (IResettableSettingValue value in settingsList) {
value.SmoothAndEase();
}
}
public void AddToCurrentDirection(Vector3 inputDirection, float power){ // Old
Debug.LogError("Using an old movement command! Switch to one of the new alternatives!");
}
public void SmoothToSpeed(float desiredSpeed, float smoothing){ // Old
Debug.LogError("Using an old movement command! Switch to one of the new alternatives!");
}
public void SetNewDirection(Vector3 inputDirection){ // NOTE: If smoothing desired add a default bool for smoothing maybe? // Old
Debug.LogError("Using an old movement command! Switch to one of the new alternatives!");
}
public void SetNewGravity(float value){
Debug.LogError("Using an old movement command! Switch to one of the new alternatives!");
}
public void SetSpecifiedRotation(Quaternion inputRotation){ // Old
Debug.LogError("Using an old movement command! Switch to one of the new alternatives!");
}
public void OverwriteDirectionFromInput(Vector2 value, float priority, float speed = Mathf.Infinity){ // Old
Debug.LogError("Using an old movement command! Switch to one of the new alternatives!");
} }
} }
} }