Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Rellac · Oct 30, 2014 at 04:34 PM · c#arraypathfindinggrid

RTS Grid Initialisation

Hello. I am attempting to create a RTS game for my final year project at university.

I have managed to implement the majority of the game, but I have been using a lackluster pathfinding algorithm (go straight to the destination, no actual algorithm).

I have been attempting to ammend this and after speaking with my prof, he told me that I should be using an Array system to initialise the grid points. I've been at this for some time but I really don't understand how this should work.

The two main reasons behind this change are to reduce load times (they are pretty hefty) and to appropriately implement a pathfinding algorithm.

I do understand how an array will work, but I don't understand how it would have the various triggers I would need.

Currently, I have this as my implementation:

     void Start ()
     {
         //Initialise Grid Points
         //-----------------------
         for (int i=1; i <= MapDimension; i++)
             for (int j=1; j <=MapDimension; j++)
                 Instantiate(gridPoint, new Vector3(transform.position.x+i*5, transform.position.y, transform.position.z+j*5), Quaternion.identity);
 
         foreach (GameObject Grid in GameObject.FindGameObjectsWithTag("Grid"))
         {
             GridScript Script = Grid.GetComponent<GridScript>();
             Script.landtype = "forest";
             
             // Resource Generation
             //---------------------
 
             // Mountains
             if (Random.Range(0,1000)<1)
             {
                 float scale = 10;
                 Instantiate(mountain, new Vector3(Grid.transform.position.x+25, -13, Grid.transform.position.z), Quaternion.identity);
                 mountain.transform.localScale = new Vector3 (scale,scale,scale);
             }
             
             foreach(GameObject Mountain in GameObject.FindGameObjectsWithTag("mountain"))
             {
                 
                 if (Grid.transform.position.x > Mountain.transform.position.x - 35 && Grid.transform.position.x < Mountain.transform.position.x + 25 && Grid.transform.position.z > Mountain.transform.position.z - 25 && Grid.transform.position.z < Mountain.transform.position.z + 25)
                 {
                     if (Grid.transform.position.x != Mountain.transform.position.x -25)
                     {
                         if ((Grid.transform.position.x == Mountain.transform.position.x +20 && Grid.transform.position.z == Mountain.transform.position.z-20)||(Grid.transform.position.x == Mountain.transform.position.x +20 && Grid.transform.position.z == Mountain.transform.position.z-15)||(Grid.transform.position.x == Mountain.transform.position.x +15 && Grid.transform.position.z == Mountain.transform.position.z-20)||
                             (Grid.transform.position.x == Mountain.transform.position.x -20 && Grid.transform.position.z == Mountain.transform.position.z-20)||(Grid.transform.position.x == Mountain.transform.position.x -15 && Grid.transform.position.z == Mountain.transform.position.z-20)||(Grid.transform.position.x == Mountain.transform.position.x -20 && Grid.transform.position.z == Mountain.transform.position.z-15)||
                             (Grid.transform.position.x == Mountain.transform.position.x +20 && Grid.transform.position.z == Mountain.transform.position.z+20)||(Grid.transform.position.x == Mountain.transform.position.x +20 && Grid.transform.position.z == Mountain.transform.position.z+15)||(Grid.transform.position.x == Mountain.transform.position.x +15 && Grid.transform.position.z == Mountain.transform.position.z+20)||
                             (Grid.transform.position.x == Mountain.transform.position.x -20 && Grid.transform.position.z == Mountain.transform.position.z+20)||(Grid.transform.position.x == Mountain.transform.position.x -20 && Grid.transform.position.z == Mountain.transform.position.z+15)||(Grid.transform.position.x == Mountain.transform.position.x -15 && Grid.transform.position.z == Mountain.transform.position.z+20)
                             ){}
                         else
                         {
                             Script.active = false;
                             Script.landtype = "mountain";
                         }
                     }
                 }
             }
             
             // Trees
             if (Random.Range(0,100) <50)
             for (int i=1; i< Random.Range(0,3); i++)
             if (Script.landtype == "forest")
             {
                 GameObject t = (GameObject)Instantiate(tree, new Vector3(Grid.transform.position.x + Random.Range(-1.5f,1.5f), 0, Grid.transform.position.z + Random.Range(-1.5f,1.5f)), Quaternion.identity); 
                 Script.ClutterList.Add(tree);
                 t.GetComponent<Tree>().thisPoint = Grid;
                 t.GetComponent<Tree>().grid = Grid.GetComponent<GridScript>();
                 Script.active=false;
 
 
             }
 
             // Stones
             for (int i=6; i< Random.Range(0,8); i++)
             {
                 Instantiate(stones, new Vector3(Grid.transform.position.x + Random.Range(0,0), 0, Grid.transform.position.z + Random.Range(0,0)), Quaternion.identity);
                 Script.ClutterList.Add(stones);
             }
         }
     }

This uses a prefab of a cube to act as each grid point. It generates items on top of each point and adds those items to its "ClutterList", which will stop any building being done whilst this list is populated.

All of this coding does work, only I want to increase my efficiency and add a method for pathfinding with an array system.

Cuirrently, these grid points have the tags:

  • "active" (bool)

  • "highlighted" (bool)

  • "road" (bool)

  • "roadplaced" (bool)

  • "landtype" (string)

  • "ClutterList" (ArrayList)

These are all logical flags to allow me to implement everything I need and I need to be able to reference these variables within the grid list.

My initial thought was to create an arraylist of these same GameObjects, but I realised that this wouldn't make the slightest difference. I was told that I should be using a 2D int array of (1,1), (1,2), (2,2) etc, but I wouldn't know how to reference the variables I need without simply using the exact same system I already have.

Any help would be fantastic as I am very confused with the whole thing, thanks.

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

0 Replies

· Add your reply
  • Sort: 

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

26 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

Related Questions

How to Save an multidimensional array trough editor script 1 Answer

A* Pathfinding Project Problem with Obstacles 2 Answers

Astar Pathfinding not scanning graph 0 Answers

Multiple Cars not working 1 Answer

Pathfinding through pairs of connections 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