RTS Resource Droppoff

Im making a RTS game with resource collecting units, when they are full they are put into a list so only one truck gets to move on the drive thru resource droppoff ramp at once. I have an idea on how to start this but need some help, I’m wondering how I can do two things, Make the truck find the nearest droppoff point using tags and vector3.distance and how can I make a list sort trucks based on how close they are to the building so the closest truck can droppoff instead of waiting forever for another dumptruck far away to get there. Also when moving the trucks to the resource depot and on the ramp is it best to make invisible gameobjects they have to move to or is there a more convenient way? Thanks!!

public void ResourceFull()
	{
		
		float ClosestDistance = Mathf.Infinity; 


		foreach (GameObject ResourceDepot in RM.ResourceDepotLocations)
		{

			DistanceToDroppoff = Vector3.Distance (ResourceDepot.transform.position, this.gameObject.transform.position);

			if (DistanceToDroppoff < ClosestDistance) 
			{

				ResourceDepot.GetComponent<ResourceDepot> ().ResourceDepositLine.Add (this.gameObject);
				agent.destination = ResourceDepot.transform.position;
							}
		}
			}

Basically checks if there is a resource depot to move to, where its located and how far away the truck is away from it

It’s not the full answer but it’s a little help with the logic of your code.
When you say “find the nearest dropoff” You’re only looking for one with the tag:

GameObject ResourceDepot = GameObject.FindGameObjectWithTag ("ResourceDepot").gameObject;

I would suggest adding your Resource drop offs in a list and take it out of your update:

void ResourceFull()
{
    List<GameObject> ResoureceDepot = new List<GameObject();
    float closestDistance = 0; 
    gameObject closestResouce;
    foreach (gameObject go in ResourceDepot)
    {
        float DistanceFromDroppoff = Vector3.Distance (ResourceDepot.transform.position, this.gameObject.transform.position);
        if (DistanceFromDropoff > cloasestDistance) 
        {
            closestResouce = this.gameObject; 
            // I don't fully understand why you're setting it as "This" gameobject Should it not be Truck?
         }
    }
ResourceDepot.GetComponent<ResourceDepot> ().ResourceDepositLine.Add (closestResouce);
}

hard to read…

First, dont “Find” gameObject (I’m talking about ‘FindGameObjectWithTag’. Or do it the least as possible. Never do it in Update. Awfull for game performance.

You should have a list of all your ‘RessourceDepot’ somewhere and call that to find the closest one. To find the closest object check this post. Just again, limit the “Find” methods.

For your ressource depot location, best would be to have a list of Vector3 with those locations that could be set from the start (before the Start :slight_smile: ). They dont have to be gameObjects at all, but if you do use them as gameObjects, they can be empty GameObjects