Really struggling on this, need help finding nearest target

I have an asteroid field spawned at runtime and droid prefabs the player can release to gather raw material from the asteroids. Everything works but the droids go to the furthest part of the asteroid field to start collecting. I realize I likely need a list of objects to sort through somehow and output the closest.

I suck at coding but managed to get this working by getting a link to the spawned asteroids adding MovementAIRigidbody target to this script & finding one with target = GameObject.Find(“AsteroidNew2(Clone)”).GetComponent().

This script is based on a GitHub MIT project which is using Vector3 targetPos; I believe to select the target position. Hope this makes sense to a kind soul out there.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UnityMovementAI
{
public class OffSetPursuitUnityMy : MonoBehaviour
{
MovementAIRigidbody target;

public Vector3 offset;
public float groupLookDist = 1.5f;

SteeringBasics steeringBasics;
OffsetPursuit offsetPursuit;
Separation separation;

NearSensor sensor;

void Start()
{
steeringBasics = GetComponent();
offsetPursuit = GetComponent();
separation = GetComponent();
target = GameObject.Find(“AsteroidNew2(Clone)”).GetComponent();
sensor = GameObject.Find(“SeparationSensor”).GetComponent();
}

void LateUpdate()
{

Vector3 targetPos;
Vector3 offsetAccel = offsetPursuit.GetSteering(target, offset, out targetPos);
Vector3 sepAccel = separation.GetSteering(sensor.targets);

steeringBasics.Steer(offsetAccel + sepAccel);

/* If we are still arriving then look where we are going, else look the same direction as our formation target */
if (Vector3.Distance(transform.position, targetPos) > groupLookDist)
{
steeringBasics.LookWhereYoureGoing();
}
else
{
steeringBasics.LookAtDirection(target.Rotation);
}
}
}
}

Make sure when you post code in the forum you use Code Tags to make your code more readable.

For the solution, you just want to take the Distance check they’re doing and apply it to your code, and choose the asteroid with the lowest one.

1 Like

Thank you, I’m still learning. I went into the post and updated using code tags which worked but may have created another post instead of updating this one as I’m not seeing the changes reflected.

“you just want to take the Distance check they’re doing and apply it to your code, and choose the asteroid with the lowest one” - Yes exactly, that’s the part I need help with. I’ve looked at the manual, watched tutorials but don’t see how I accomplish it in this situation.

You get a List of all of your asteroids, and for each one you compare the Distance, what part are you getting stuck on?

Here’s some pseudocode of about what it should look like

List<GameObject> asteroids; // your collection of asteroids, get it from your spawner

float minDistance = float.MaxValue;
GameObject closestAsteroid = null;
foreach(GameObject asteroid in asteroids)
{
    float distance = Vector3.Distance(asteroid.transform.position, transform.position);
    if(distance < minDistance)
        {
            minDistance = distance;
            closestAsteroid = asteroid;
        }
}

// closestAsteroid is the closest one after the loop
1 Like

Thank you for this example. I’m getting stuck on how I plug in the result of the closestAsteriod into the existing code.

I’m using target = GameObject.Find(“AsteroidNew2(Clone)”) which would be replaced I’m guessing by the List … the LateUpdate function is using Vector3 targetPos; which is where I assume I need to plug in the result of the closestAsteroid somehow.