Skip to content

Instantly share code, notes, and snippets.

@trkyshorty
Created November 13, 2022 15:15
Show Gist options
  • Save trkyshorty/709fe950af285d31badae2efaae0ce6c to your computer and use it in GitHub Desktop.
Save trkyshorty/709fe950af285d31badae2efaae0ce6c to your computer and use it in GitHub Desktop.
public Vector3 MoveTowards(Vector3 current, Vector3 target)
{
float maxDistanceDelta = 6.75f;
switch (MySelf.Speed)
{
case 67: //Sprint - Swift
maxDistanceDelta = 10.125f;
break;
case 90: //Light Feet
maxDistanceDelta = 13.5f;
break;
}
return MoveTowards(current, target, maxDistanceDelta);
}
/// <summary>
/// Move to wards
/// </summary>
/// <remarks>https://github.com/Unity-Technologies/UnityCsReference/blob/c84064be69f20dcf21ebe4a7bbc176d48e2f289c/Runtime/Export/Math/Vector3.cs#L61</remarks>
public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)
{
// avoid vector ops because current scripting backends are terrible at inlining
float toVector_x = target.X - current.X;
float toVector_y = target.Y - current.Y;
float toVector_z = target.Z - current.Z;
float sqdist = toVector_x * toVector_x + toVector_y * toVector_y + toVector_z * toVector_z;
if (sqdist == 0 || maxDistanceDelta >= 0 && sqdist <= maxDistanceDelta * maxDistanceDelta)
return target;
var dist = (float)Math.Sqrt(sqdist);
float X = (float)Math.Round(current.X + toVector_x / dist * maxDistanceDelta, 1);
float Y = (float)Math.Round(current.Y + toVector_y / dist * maxDistanceDelta, 1);
float Z = (float)Math.Round(current.Z + toVector_z / dist * maxDistanceDelta, 1);
return new Vector3(X, Y, Z);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment