Last active
September 24, 2020 07:05
-
-
Save thebeardphantom/65d2d0b5dd7a969b5a3863b084a443f8 to your computer and use it in GitHub Desktop.
A serializable struct wrapper for smooth damping. Debug inspector will show you Current Value and Velocity!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
[Serializable] | |
public struct SmoothDampState | |
{ | |
#region Fields | |
public float Damping; | |
public float MaxSpeed; | |
#endregion | |
#region Properties | |
public float TargetValue { get; set; } | |
public float CurrentValue { get; set; } | |
public float Velocity { get; set; } | |
#endregion | |
#region Methods | |
public SmoothDampState IntegrateNewState() | |
{ | |
return IntegrateNewState(Time.deltaTime); | |
} | |
public SmoothDampState IntegrateNewState(float dt) | |
{ | |
var copy = this; | |
var currentVelocity = Velocity; | |
copy.CurrentValue = Mathf.SmoothDamp(CurrentValue, TargetValue, ref currentVelocity, Damping, MaxSpeed, dt); | |
copy.Velocity = currentVelocity; | |
return copy; | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment