Skip to content

Commit c16724b

Browse files
committed
Add smooth damping data structures
1 parent 0f3c8a3 commit c16724b

File tree

8 files changed

+226
-0
lines changed

8 files changed

+226
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Animation
4+
{
5+
/// <summary>
6+
/// Gradually changes a value over time using a spring-damper function,
7+
/// which will never overshoot.
8+
/// </summary>
9+
/// <typeparam name="T">The type of value to be animated.</typeparam>
10+
[System.Serializable]
11+
public abstract class SmoothDamp<T>
12+
{
13+
protected T _value;
14+
protected T _velocity;
15+
16+
/// <summary>
17+
/// The current value.
18+
/// </summary>
19+
public T value
20+
{
21+
get { return _value; }
22+
protected set { _value = value; }
23+
}
24+
25+
/// <summary>
26+
/// The current velocity, this value is modified by the function every
27+
/// time you call it.
28+
/// </summary>
29+
public T velocity
30+
{
31+
get { return _velocity; }
32+
protected set { _velocity = value; }
33+
}
34+
35+
/// <summary>
36+
/// Approximately the time it will take to reach the target. A smaller
37+
/// value will reach the target faster.
38+
/// </summary>
39+
[Tooltip("Approximately the time it will take to reach the target. A smaller value will reach the target faster.")]
40+
public float smoothTime = 0.1f;
41+
42+
/// <summary>
43+
/// Optionally allows you to clamp the maximum speed.
44+
/// </summary>
45+
[Tooltip("Optionally allows you to clamp the maximum speed.")]
46+
public float maxSpeed = Mathf.Infinity;
47+
48+
/// <summary>
49+
/// Smoothes the current value to the <paramref name="target"/> value.
50+
/// </summary>
51+
/// <param name="target">The target value.</param>
52+
/// <returns>The new current value.</returns>
53+
public abstract T Update(T target);
54+
55+
/// <summary>
56+
/// Smoothes the current value to the <paramref name="target"/> value.
57+
/// </summary>
58+
/// <param name="target">The target value.</param>
59+
/// <param name="deltaTime">The time since the last call to this function.</param>
60+
/// <returns>The new current value.</returns>
61+
public abstract T Update(T target, float deltaTime);
62+
63+
}
64+
65+
}

Runtime/DataStructures/SmoothDamp.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Animation
4+
{
5+
/// <summary>
6+
/// Gradually changes a float over time using a spring-damper function,
7+
/// which will never overshoot.
8+
/// </summary>
9+
[System.Serializable]
10+
public class SmoothDampFloat : SmoothDamp<float>
11+
{
12+
/// <inheritdoc />
13+
/// <param name="target">The target value.</param>
14+
public override float Update(float target)
15+
{
16+
this.value = Mathf.SmoothDamp(this.value, target,
17+
ref _velocity,
18+
this.smoothTime,
19+
this.maxSpeed);
20+
21+
return this.value;
22+
}
23+
24+
/// <inheritdoc />
25+
/// <param name="target">The target value.</param>
26+
public override float Update(float target, float deltaTime)
27+
{
28+
this.value = Mathf.SmoothDamp(this.value, target,
29+
ref _velocity,
30+
this.smoothTime,
31+
this.maxSpeed,
32+
deltaTime);
33+
34+
return this.value;
35+
}
36+
37+
}
38+
39+
}

Runtime/DataStructures/SmoothDampFloat.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Animation
4+
{
5+
/// <summary>
6+
/// Gradually changes a Vector2 over time using a spring-damper function,
7+
/// which will never overshoot.
8+
/// </summary>
9+
[System.Serializable]
10+
public class SmoothDampVector2 : SmoothDamp<Vector2>
11+
{
12+
/// <inheritdoc />
13+
/// <param name="target">The target value.</param>
14+
public override Vector2 Update(Vector2 target)
15+
{
16+
this.value = Vector2.SmoothDamp(this.value, target,
17+
ref _velocity,
18+
this.smoothTime,
19+
this.maxSpeed);
20+
21+
return this.value;
22+
}
23+
24+
/// <inheritdoc />
25+
/// <param name="target">The target value.</param>
26+
public override Vector2 Update(Vector2 target, float deltaTime)
27+
{
28+
this.value = Vector2.SmoothDamp(this.value, target,
29+
ref _velocity,
30+
this.smoothTime,
31+
this.maxSpeed,
32+
deltaTime);
33+
34+
return this.value;
35+
}
36+
37+
}
38+
39+
}

Runtime/DataStructures/SmoothDampVector2.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Animation
4+
{
5+
/// <summary>
6+
/// Gradually changes a Vector3 over time using a spring-damper function,
7+
/// which will never overshoot.
8+
/// </summary>
9+
[System.Serializable]
10+
public class SmoothDampVector3 : SmoothDamp<Vector3>
11+
{
12+
/// <inheritdoc />
13+
/// <param name="target">The target value.</param>
14+
public override Vector3 Update(Vector3 target)
15+
{
16+
this.value = Vector3.SmoothDamp(this.value, target,
17+
ref _velocity,
18+
this.smoothTime,
19+
this.maxSpeed);
20+
21+
return this.value;
22+
}
23+
24+
/// <inheritdoc />
25+
/// <param name="target">The target value.</param>
26+
public override Vector3 Update(Vector3 target, float deltaTime)
27+
{
28+
this.value = Vector3.SmoothDamp(this.value, target,
29+
ref _velocity,
30+
this.smoothTime,
31+
this.maxSpeed,
32+
deltaTime);
33+
34+
return this.value;
35+
}
36+
37+
}
38+
39+
}

Runtime/DataStructures/SmoothDampVector3.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)