- Home /
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
Answer by jmgek · Dec 20, 2016 at 05:34 AM
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);
}
This script is on the truck also I cannot tell the truck where any resource depots are in start as there will be none at the start and after that it will be placed anywhere on the map by the player only once its placed can I tell the truck where it is
No problem. You should have a List of all those depots. Each time you create a new depot, add it to the list. The truck just need to loop through the list of all depots. Each truck should not have this list, the list should be stored centrally, all trucks should look in this central depot list when needing it.
@jmgek this is wrong, sorry. Your current code is searching for the further dropoff, not the closest.
float closestDistance = $$anonymous$$athf.infinity; not 0.
[...] if (DistanceFromDropoff < closestDistance) [...] not ">"
also ResourcesDepot list should be outside this function. If inside it, as you did here, the list is empty each time you call "ResourceFull" function.
also, your last line is wrong, you cant GetComponent on a List. It would be ok if closestResource.GetComponent[...]. Actually no, that still feels wrong. The list should really be central and not be present on each depot in any case. Why add a depot list on each depot, especially if that depot list is not the same on each depot. Confusing
Yeah my bad on setting the resource deposit distance to 0.
The truck could go through the list of resource depots then calculate how far it is from each one using a for each loop and then travel to the nearest one
@Gr$$anonymous$$I
Then how should I organize my list of which trucks are going to that resource depot? Since each one will be different
I updated the code at the top take a look and see if it makes sense If not let me know what to change
(First, variables should have first letter lower case, Class should have first letter upper case. This does not do any practical difference, but is a common best practice that will help everyone understand you better)
What is R$$anonymous$$? is it a reference to a script that exist only once in your game scene? If it is (and it should be...) read bellow. If not, you should have one script containing some lists that exist only once in your scene, containing things like truck list, depot list, ... No need for each truck to save his own copy of the exact same list any ways. So, assu$$anonymous$$g R$$anonymous$$ is that i'll try to show a better implementation of your code.
When searching for a closest thing, you first need to find the ONE closest thing, then only (after the foreach loop) do what you want with it. That is what I've done here.
GameObject closestDepot;
public void ResourceFull()
{
float closestDistance = $$anonymous$$athf.Infinity;
foreach (GameObject resourceDepot in R$$anonymous$$.resourceDepotLocations)
{
float distanceToDroppoff = Vector3.Distance (resourceDepot.transform.position, gameObject.transform.position);
if (distanceToDroppoff < closestDistance)
{
closestDepot = resourceDepot;
}
}
if (closestDepot != null)
{
closestDepot.GetComponent<ResourceDepot>().resourceDepositLine.Add (gameObject);
agent.destination = closestDepot.transform.position;
}
}
mathf.infinity means the bigger possible value. So before your loop closestDistance = infinity. So the search value is not limited by distance, the goal is to find the closest one, so the initial radius is infinitly big (not really infinitly, but so huge you can think of it as beeing infinite)
Answer by GrKl · Dec 20, 2016 at 07:10 AM
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 :) ). They dont have to be gameObjects at all, but if you do use them as gameObjects, they can be empty GameObjects
Your answer
Follow this Question
Related Questions
GameObject.findGameObjectsWithTag returning empty? 1 Answer
Are there any educational resources or middleware for creating Real-Time Strategy games in Unity? 5 Answers
Can someone help me find the closest waypoint to a player? 1 Answer
RTS Building Creation System Code not working (no errors) 2 Answers
How can you make an AI perform specific movements? 2 Answers