namespace Zinnia.Extension { using UnityEngine; /// /// Extended methods for the Type. /// public static class Vector3Extensions { /// /// Determines if two values are equal based on a given tolerance. /// /// The to compare against. /// The to compare with. /// The tolerance in which the two values can be within to be considered equal. /// if the two values are considered equal. public static bool ApproxEquals(this Vector3 a, Vector3 b, float tolerance = float.Epsilon) { return (Vector3.Distance(a, b) <= tolerance); } /// /// Divides each component of the given against the given . /// /// The value to divide by each component. /// The components to divide with. /// The quotient. public static Vector3 Divide(float dividend, Vector3 divisor) { return new Vector3(dividend / divisor.x, dividend / divisor.y, dividend / divisor.z); } /// /// Divides two s component-wise. /// /// The value to divide by each component. /// The components to divide with. /// The quotient. public static Vector3 Divide(this Vector3 dividend, Vector3 divisor) { return Vector3.Scale(dividend, Divide(1, divisor)); } /// /// Check whether the source and target points are within a given tolerance distance of each other. /// /// The source point. /// The target point. /// The tolerance of distance equality. /// Whether the source and target are within the distance equality tolerance. public static bool WithinDistance(this Vector3 a, Vector3 b, Vector3 tolerance) { return a.x.ApproxEquals(b.x, tolerance.x) && a.y.ApproxEquals(b.y, tolerance.y) && a.z.ApproxEquals(b.z, tolerance.z); } /// /// Converts an unsigned Euler angle into a signed Euler angle. /// /// The unsigned Euler angle to convert. /// The converted signed Euler angle. public static Vector3 UnsignedEulerToSignedEuler(this Vector3 eulerAngles) { return new Vector3(eulerAngles.x.GetSignedDegree(), eulerAngles.y.GetSignedDegree(), eulerAngles.z.GetSignedDegree()); } /// /// Gets the direction from a source to a target. /// /// The starting point. /// The finishing point. /// Whether to normalize the direction. /// The direction that the target is in from the source. public static Vector3 Direction(this Vector3 source, Vector3 target, bool isNormalized = false) { Vector3 heading = target - source; return heading / (isNormalized ? heading.magnitude : 1f); } } }