Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DJGhostViper · Dec 20, 2016 at 02:00 AM · resourcesrtsfindgameobjectswithtagvector3.distancefindgameobjectwithtag

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }


Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DJGhostViper · Dec 20, 2016 at 11:56 AM 0
Share

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

avatar image GrKl DJGhostViper · Dec 20, 2016 at 12:08 PM 0
Share

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.

avatar image GrKl · Dec 20, 2016 at 12:16 PM 0
Share

@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

avatar image jmgek GrKl · Dec 20, 2016 at 04:33 PM 0
Share

Yeah my bad on setting the resource deposit distance to 0.

avatar image DJGhostViper · Dec 20, 2016 at 08:01 PM 0
Share

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

avatar image DJGhostViper · Dec 21, 2016 at 12:14 AM 0
Share

@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

avatar image DJGhostViper · Dec 21, 2016 at 12:59 AM 0
Share

I updated the code at the top take a look and see if it makes sense If not let me know what to change

avatar image GrKl DJGhostViper · Dec 21, 2016 at 06:55 AM 0
Share

(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;
     }
 }
avatar image GrKl GrKl · Dec 21, 2016 at 12:26 PM 0
Share

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)

Show more comments
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

63 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges