Unity Gravity script lowering FPS because of overlap sphere checking collision What are some ideas or ways that I can increase the efficiency of this gravity script because it is way to depending for what i want it for.
The game has a target at the center and there are objects far away all around the target and when gravity script is enabled I create an overlap sphere check which objects have a rigidbody and collision and pull them to target at a constant speed, but my FPS drops to the floor.
Would it be more efficient to have a script in each of these objects with a AddForce and direction towards target? Or any other ideas? Maybe something to change in this current script?
Am I right on the gravity script being too demanding because of the distance the gravity script is checking?
[RequireComponent(typeof(Rigidbody))]
public class Gravity : MonoBehaviour
{
private Rigidbody ownBody;
[HideInInspector]
public float maxVelocityOfInfluence;
private float rangeOfInfluence = 7000.0f;

private void OnEnable()
{
    if (ownBody == null)
    {
        ownBody = GetComponent&lt;Rigidbody&gt;();
    }
}

private void FixedUpdate()
{
    Collider[] cols = Physics.OverlapSphere(transform.position, rangeOfInfluence);
    List&lt;Rigidbody&gt; rbs = new List&lt;Rigidbody&gt;();

    foreach (Collider c in cols)
    {
        Rigidbody otherBody = c.attachedRigidbody;
        if (otherBody != null &amp;&amp; otherBody != ownBody &amp;&amp; !rbs.Contains(otherBody) &amp;&amp; 
HasTagDownHierarchy(otherBody.transform, &quot;Boat&quot;))
        {
            rbs.Add(otherBody);

            Vector3 offset = transform.position - c.transform.position;
            otherBody.velocity = (offset.normalized * maxVelocityOfInfluence);
        }
    }
}

private static bool HasTagDownHierarchy(Transform transform, string tag)
{
    if (!transform)
    {
        return false;
    }

    if (transform.CompareTag(tag))
    {
        return true;
    }

    foreach (Transform child in transform)
    {
        if (HasTagDownHierarchy(child, tag))
        {
            return true;
        }
    }

    return false;
}
You are allocating like crazy use Physics.OverlapSphereNonAlloc
dont do List&lt;Rigidbody&gt; rbs = new List&lt;Rigidbody&gt;();
Replace with o(1) lookup HasTagDownHierarchy(otherBody.transform, &quot;Boat&quot;) also avoid tags. Use markup classes.
edit: With look up I mean, in Boat Awake method cache the Colliders in a look up Dictionary&lt;Collider, Boat&gt;