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 Vadom6 · Dec 08, 2019 at 11:17 AM · listloopintvector2

How to make a function that runs a loop x amount of times (Determined by parsed int)

So what I want is a function that checks for a certain radius but measures the radius off a Parsed Int. So if Rad is parsed as 2 it will get all the tiles within a radius of 1 and a radius of two. I am confused on how to do it at the moment

This is the code I'm using.

      public List<CellCollection> GetRadius(Vector2Int Loc, int Rad)
             {
                 var radius = new List<CellCollection>();
                 radius.Add(GetAt(Loc.x - 1, Loc.y - 1));
                 radius.Add(GetAt(Loc.x, Loc.y - 1));
                 radius.Add(GetAt(Loc.x + 1, Loc.y - 1));
                 radius.Add(GetAt(Loc.x - 1, Loc.y));
                 radius.Add(GetAt(Loc.x, Loc.y));
                 radius.Add(GetAt(Loc.x + 1, Loc.y));
                 radius.Add(GetAt(Loc.x - 1, Loc.y + 1));
                 radius.Add(GetAt(Loc.x, Loc.y + 1));
                 radius.Add(GetAt(Loc.x + 1, Loc.y + 1));
                 return radius;
             }
 

This is the code I am using but I want it to check for each count in int Radius then add it to the List. CellCollection is a grid of tiles.

The way I think i can do this is through

      public List<CellCollection> GetRadius(Vector2Int Loc, int Rad)
             {
                 var radius = new List<CellCollection>();
                 radius.Add(GetAt(Loc.x - Rad, Loc.y - Rad));
                 radius.Add(GetAt(Loc.x, Loc.y - Rad));
                 radius.Add(GetAt(Loc.x + Rad, Loc.y - Rad));
                 radius.Add(GetAt(Loc.x - Rad, Loc.y));
                 radius.Add(GetAt(Loc.x, Loc.y));
                 radius.Add(GetAt(Loc.x + Rad, Loc.y));
                 radius.Add(GetAt(Loc.x - Rad, Loc.y + Rad));
                 radius.Add(GetAt(Loc.x, Loc.y + Rad));
                 radius.Add(GetAt(Loc.x + Rad, Loc.y + Rad));
                 return radius;
             }

Thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Dec 08, 2019 at 12:03 PM

Are you interested in the tiles in a certain radius (circular distance) or do you want to get a square of tiles around your location? Your code currently is generating a square and not a circle. The point Loc.x - 1, Loc.y - 1 does not have a radius of 1 but has a radius of 1.414.


Anyways in order to dynamically get as many tiles as you need you just need two nested for loops, one for x and one for y that looks like this:

 for(int x = -Rad; x <= Rad; x++)
 {
     for(int y = -Rad; y <= Rad; y++)
     {
         radius.Add(GetAt(Loc.x + x, Loc.y + y));
     }
 }

This will get all tiles in the square around your location. The square has a side length of 2*Rad+1. So if Rad is 1 you get 9 tiles (3x3). If Rad is 2 you get 25 tiles (5x5) ...


However if you actually want to get a circle of tiles based on an actual radius you can do this:

 public List<CellCollection> GetRadius(Vector2Int Loc, float Radius)
 {
     var results = new List<CellCollection>();
     int Rad = Mathf.CeilToInt(Radius);
     int r2 = Mathf.CeilToInt(Radius * Radius);
     for(int x = -Rad; x <= Rad; x++)
     {
         for(int y = -Rad; y <= Rad; y++)
         {
             if (x*x+y*y < r2)
                 results.Add(GetAt(Loc.x + x, Loc.y + y));
         }
     }
     return results;
 }

This will still iterate through all tiles in the enclosing square of our circle but only add the tiles which are actually in the circle. Note that I made radius a float since whole numbers probably doesn't make much sense. This method will return only 5 tiles when you pass a radius of 1 (the center tile and the 4 cardinal directions) If you pass 1.5 as radius it would again be 9 tiles (3x3). If you want to use this approach you might have to try out some radii which give you the wanted results. Of course for small radii you barely recognise a circle. However the larget the radius the more it will look like an actual circle.

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

120 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 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

A node in a childnode? 1 Answer

Can't add multiple integers to a List in a Loop! 2 Answers

Create a button from an int, remove button when clicked, and never load it again 1 Answer

Loop through a List to check whether a bool is true within a ScriptableObject 1 Answer

Looping over the axis between two vectors 1 Answer


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