Skip to content

Commit 700d8b3

Browse files
committed
Fix interpolation snapping always rounding down
1 parent 822ec3d commit 700d8b3

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

Runtime/Interpolation.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using UnityEngine;
1+
using UnityEngine;
22

33
namespace Zigurous.Tweening
44
{
@@ -32,8 +32,8 @@ public static class Interpolation
3232
/// <returns>The interpolated value between the start and end value.</returns>
3333
public static float Lerp(float a, float b, float t, bool snapping = false)
3434
{
35-
float value = Mathf.Lerp(a, b, t);
36-
return snapping ? (int)value : value;
35+
float value = a + (b - a) * t;
36+
return snapping ? Mathf.Round(value) : value;
3737
}
3838

3939
/// <summary>
@@ -47,8 +47,8 @@ public static float Lerp(float a, float b, float t, bool snapping = false)
4747
/// <returns>The interpolated value between the start and end value.</returns>
4848
public static double Lerp(double a, double b, float t, bool snapping = false)
4949
{
50-
double value = Mathf.Lerp((float)a, (float)b, t);
51-
return snapping ? (int)value : value;
50+
double value = a + (b - a) * t;
51+
return snapping ? System.Math.Round(value) : value;
5252
}
5353

5454
/// <summary>
@@ -62,7 +62,7 @@ public static double Lerp(double a, double b, float t, bool snapping = false)
6262
/// <returns>The interpolated value between the start and end value.</returns>
6363
public static int Lerp(int a, int b, float t, bool snapping = false)
6464
{
65-
return (int)Mathf.Lerp(a, b, t);
65+
return (int)Lerp((float)a, (float)b, t, snapping);
6666
}
6767

6868
/// <summary>
@@ -76,6 +76,8 @@ public static int Lerp(int a, int b, float t, bool snapping = false)
7676
/// <returns>The interpolated value between the start and end value.</returns>
7777
public static long Lerp(long a, long b, float t, bool snapping = false)
7878
{
79+
return (long)Lerp((double)a, (double)b, t, snapping);
80+
}
7981

8082
/// <summary>
8183
/// Linearly interpolates between <paramref name="a"/> and
@@ -88,7 +90,7 @@ public static long Lerp(long a, long b, float t, bool snapping = false)
8890
/// <returns>The interpolated value between the start and end value.</returns>
8991
public static short Lerp(short a, short b, float t, bool snapping = false)
9092
{
91-
return (short)Mathf.Lerp(a, b, t);
93+
return (short)Lerp((float)a, (float)b, t, snapping);
9294
}
9395

9496
/// <summary>
@@ -118,8 +120,8 @@ public static Vector2 Lerp(Vector2 a, Vector2 b, float t, bool snapping = false)
118120
public static Vector2Int Lerp(Vector2Int a, Vector2Int b, float t, bool snapping = false)
119121
{
120122
return new Vector2Int(
121-
(int)Mathf.Lerp(a.x, b.x, t),
122-
(int)Mathf.Lerp(a.y, b.y, t));
123+
Lerp(a.x, b.x, t, snapping),
124+
Lerp(a.y, b.y, t, snapping));
123125
}
124126

125127
/// <summary>
@@ -149,9 +151,9 @@ public static Vector3 Lerp(Vector3 a, Vector3 b, float t, bool snapping = false)
149151
public static Vector3Int Lerp(Vector3Int a, Vector3Int b, float t, bool snapping = false)
150152
{
151153
return new Vector3Int(
152-
(int)Mathf.Lerp(a.x, b.x, t),
153-
(int)Mathf.Lerp(a.y, b.y, t),
154-
(int)Mathf.Lerp(a.z, b.z, t));
154+
Lerp(a.x, b.x, t, snapping),
155+
Lerp(a.y, b.y, t, snapping),
156+
Lerp(a.z, b.z, t, snapping));
155157
}
156158

157159
/// <summary>

0 commit comments

Comments
 (0)