Skip to content

Instantly share code, notes, and snippets.

@thebeardphantom
Last active September 24, 2020 07:05
Show Gist options
  • Save thebeardphantom/65d2d0b5dd7a969b5a3863b084a443f8 to your computer and use it in GitHub Desktop.
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!
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