80 lines
3.0 KiB
C#
80 lines
3.0 KiB
C#
using System.Collections.Generic;
|
|
using System;
|
|
using UnityEngine;
|
|
|
|
namespace UnityUtils
|
|
{
|
|
public static class Vector3Extensions
|
|
{
|
|
/// <summary>
|
|
/// Sets any x y z values of a Vector3
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds to any x y z values of a Vector3
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a Boolean indicating whether the current Vector3 is in a given range from another Vector3
|
|
/// </summary>
|
|
/// <param name="current">The current Vector3 position</param>
|
|
/// <param name="target">The Vector3 position to compare against</param>
|
|
/// <param name="range">The range value to compare against</param>
|
|
/// <returns>True if the current Vector3 is in the given range from the target Vector3, false otherwise</returns>
|
|
public static bool InRangeOf(this Vector3 current, Vector3 target, float range)
|
|
{
|
|
return (current - target).sqrMagnitude <= range * range;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Divides two Vector3 objects component-wise.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 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.
|
|
/// </remarks>
|
|
/// <example>
|
|
/// Use 'ComponentDivide' to scale a game object proportionally:
|
|
/// <code>
|
|
/// myObject.transform.localScale = originalScale.ComponentDivide(targetDimensions);
|
|
/// </code>
|
|
/// This scales the object size to fit within the target dimensions while maintaining its original proportions.
|
|
///</example>
|
|
/// <param name="v0">The Vector3 object that this method extends.</param>
|
|
/// <param name="v1">The Vector3 object by which v0 is divided.</param>
|
|
/// <returns>A new Vector3 object resulting from the component-wise division.</returns>
|
|
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);
|
|
}
|
|
public static Vector3 Mean(this List<Vector3> vectors)
|
|
{
|
|
if (vectors == null || vectors.Count == 0)
|
|
{
|
|
throw new ArgumentException("The input list of vectors is empty or null.");
|
|
}
|
|
|
|
Vector3 sum = Vector3.zero;
|
|
|
|
foreach (var vector in vectors)
|
|
{
|
|
sum += vector;
|
|
}
|
|
|
|
return sum / vectors.Count;
|
|
}
|
|
}
|
|
|
|
}
|