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 /
  • Help Room /
avatar image
0
Question by rmail2006 · Jan 16, 2016 at 06:39 PM · unity 5terrainarrayarraysterraindata

Convert a square array selection to circular selection

Hi Everyone,

I've got a bit of a problem when trying to edit my terrain.

I've got a square object which makes it easy to select an float array of heights under it based on the objects height etc.

The problem i have is if i want to edit the terrain but rather edit everything in the array (giving a square edit), I was wanting to edit the terrain in a more circular fashion but im not sure how to do it in code.

The idea if if I have an 8x8 int[] array I'd like to flatten a circle within that selection.

e.g. Array position to edit

01 02 03 04 05 06 07 08

09 10 11 12 13 14 15 16

17 18 19 20 21 22 23 24

25 26 27 28 29 30 31 32

33 34 35 36 37 38 39 40

41 42 43 44 45 46 47 48

49 50 51 52 53 54 55 56

57 58 59 60 61 62 63 64

I Hope that makes sense :)

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 NoseKills · Jan 18, 2016 at 12:04 AM

You can calculate the x and y coordinates of each of those cells in your rectangular selection and use distance from center to select a round area from it.

 for (int i = 0; i < array.Length; i++)
 {
     var x = i % width; // means rect grid width : 8
     var y = i / width;
     var halfWidth= width / 2;
     var thisPos = new Vector2(x, y);
     var center = new Vector2(halfWidth, halfWidth);
     var distanceSq = (thisPos - center).sqrMagnitude;
     if (distanceSq < halfWidth*halfWidth) // compare squares to avoid slow/heavy Mathf.sqrt()
     {
         // index is within radius of halfWidth
     }
 }

This isn't exactly right and won't give you the result shown in the pic but you should get there by modifying the formula to measure distance from between the center cells (4.5f, 4.5f) and perhaps by loosening the range condition if (distanceSq < (halfWidth*halfWidth) + 0.5f)

Comment
Add comment · Show 1 · 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 rmail2006 · Jan 18, 2016 at 09:26 AM 0
Share

Thanks Nose$$anonymous$$ills,

Yeah a little modification but i understand how it works, I'll put the final result up once I test at home :D

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity Desert Sand Dunes 0 Answers

Array[] of children of a child 3 Answers

How to play a random audio clip from an array in C#? 3 Answers

how to not repeat random array 1 Answer

Change Terrain Heightmap Resolution without Resizing Terrain 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