Created
November 13, 2022 15:15
-
-
Save trkyshorty/709fe950af285d31badae2efaae0ce6c to your computer and use it in GitHub Desktop.
This file contains 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
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