From 040632219856fa1ada31942539eac26b41b6269e Mon Sep 17 00:00:00 2001 From: Geek5510 Date: Sun, 10 Jul 2022 10:44:00 +0300 Subject: [PATCH] More efficent distance calculation instead of using Vector3.distance using squaredMagnitued which removes the square root making the code more efficent, from unity docs: "If you only need to compare magnitudes of some vectors, you can compare squared magnitudes of them using sqrMagnitude (computing squared magnitudes is faster)." --- Assets/Scripts/GoDice/App/Modules/Dice/Shells/Shell.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/GoDice/App/Modules/Dice/Shells/Shell.cs b/Assets/Scripts/GoDice/App/Modules/Dice/Shells/Shell.cs index d37d57f..d6bb80d 100644 --- a/Assets/Scripts/GoDice/App/Modules/Dice/Shells/Shell.cs +++ b/Assets/Scripts/GoDice/App/Modules/Dice/Shells/Shell.cs @@ -20,8 +20,9 @@ public int AxisToValue(Vector3 axis) var value = 0; var distance = float.MaxValue; foreach (var pair in _values) - { - var lDist = Vector3.Distance(axis, pair.Value); + { + Vector3 offset = axis - pair.Value; + var lDist = offset.sqrMagnitude; if (lDist >= distance) continue; @@ -38,4 +39,4 @@ public int AxisToValue(Vector3 axis) public int GetSensitivity() => _sensitivity; } -} \ No newline at end of file +}