using System.Collections.Generic;
using UnityEngine;
namespace Core.Utilities
{
public static class VectorHelper
{
///
/// A helper function that finds the average position of several component objects,
/// specifically because they have transforms
///
///
/// The list of components to average
///
///
/// The Unity Component which has a transform
///
///
/// The average position
///
public static Vector3 FindAveragePosition(TComponent[] components) where TComponent : Component
{
Vector3 output = Vector3.zero;
foreach (TComponent component in components)
{
if (component == null)
{
continue;
}
output += component.transform.position;
}
return output / components.Length;
}
///
/// A helper function that finds the average position of several component objects,
/// specifically because they have transforms
///
///
/// The list of components to average
///
///
/// The Unity Component which has a transform
///
///
/// The average velocity
///
public static Vector3 FindAverageVelocity(TComponent[] components) where TComponent : Component
{
Vector3 output = Vector3.zero;
foreach (TComponent component in components)
{
if (component == null)
{
continue;
}
var rigidbody = component.GetComponent();
if (rigidbody == null)
{
continue;
}
output += rigidbody.velocity;
}
return output / components.Length;
}
///
/// A helper function that finds the average position of several component objects,
/// specifically because they have transforms
///
///
/// The list of components to average
///
///
/// The Unity Component which has a transform
///
///
/// The average position
///
public static Vector3 FindAveragePosition(List components) where TComponent : Component
{
Vector3 output = Vector3.zero;
foreach (TComponent component in components)
{
if (component == null)
{
continue;
}
output += component.transform.position;
}
return output / components.Count;
}
///
/// A helper function that finds the average position of several component objects,
/// specifically because they have transforms
///
///
/// The list of components to average
///
///
/// The Unity Component which has a transform
///
///
/// The average velocity
///
public static Vector3 FindAverageVelocity(List components) where TComponent : Component
{
Vector3 output = Vector3.zero;
foreach (TComponent component in components)
{
if (component == null)
{
continue;
}
var rigidbody = component.GetComponent();
if (rigidbody == null)
{
continue;
}
output += rigidbody.velocity;
}
return output / components.Count;
}
}
}