using UnityEngine; namespace UnityUtils { public static class Vector3Extensions { /// /// Sets any x y z values of a Vector3 /// public static Vector3 With(this Vector3 vector, float? x = null, float? y = null, float? z = null) { return new Vector3(x ?? vector.x, y ?? vector.y, z ?? vector.z); } /// /// Adds to any x y z values of a Vector3 /// public static Vector3 Add(this Vector3 vector, float x = 0, float y = 0, float z = 0) { return new Vector3(vector.x + x, vector.y + y, vector.z + z); } /// /// Returns a Boolean indicating whether the current Vector3 is in a given range from another Vector3 /// /// The current Vector3 position /// The Vector3 position to compare against /// The range value to compare against /// True if the current Vector3 is in the given range from the target Vector3, false otherwise public static bool InRangeOf(this Vector3 current, Vector3 target, float range) { return (current - target).sqrMagnitude <= range * range; } /// /// Divides two Vector3 objects component-wise. /// /// /// For each component in v0 (x, y, z), it is divided by the corresponding component in v1 if the component in v1 is not zero. /// Otherwise, the component in v0 remains unchanged. /// /// /// Use 'ComponentDivide' to scale a game object proportionally: /// /// myObject.transform.localScale = originalScale.ComponentDivide(targetDimensions); /// /// This scales the object size to fit within the target dimensions while maintaining its original proportions. /// /// The Vector3 object that this method extends. /// The Vector3 object by which v0 is divided. /// A new Vector3 object resulting from the component-wise division. public static Vector3 ComponentDivide(this Vector3 v0, Vector3 v1) { return new Vector3( v1.x != 0 ? v0.x / v1.x : v0.x, v1.y != 0 ? v0.y / v1.y : v0.y, v1.z != 0 ? v0.z / v1.z : v0.z); } } }