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 /
This question was closed Feb 05, 2020 at 03:08 PM by tormentoarmagedoom for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Willexwun · Feb 02, 2020 at 01:37 PM · 2d game2d-physics

From one gameobject, detect surrounding objects

My objective here is to have each generated 2d object [Block] detect whether there is an object above, below, or to the right or left.

What would be the best way to do this?

Comment
Add comment · Show 2
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 sacredgeometry · Feb 02, 2020 at 01:41 PM 0
Share

Are the objects on a grid or are they free to be anywhere?

avatar image Willexwun sacredgeometry · Feb 02, 2020 at 02:39 PM 0
Share

The current generation is grid-like, but they are free to be anywhere. Its generating a cave system

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by LeFlop2001 · Feb 02, 2020 at 04:15 PM

you could do that by giving every block a collider and then using the Physics2D.CircleCast() function, which returns a list of collides within the circle you cast. But I’m guessing you’re positioning your blocks in a strict grid. In that case the best solution would be to store all the blocks in a two dimensional array based on their x and y position. This way you can easily check surrounding blocks by entering the blocks position plus/minus 1.

 Gameobject[,] blocks;
 public GameObject blockPrefab;
 
 public Vector2Int start;
 public Vector2Int end;
 
 void Start()
 {
       blocks = new GameObject[Mathf.Abs(start.x)+Mathf.Abs(end.x),Mathf.Abs(start.y)+Mathf.Abs(end.y)];
       for(int x= start.x ; x < end.x ; x++)
       {
            for(int y= start.y ; y < end.y ; y++)
            {
                   GameObject go = Instantiate(blockPrefab,new Vector2 (x,y),Quaternion.Euler(0,0,0));
                   blocks[x-start.x,y-start.y] = go;
            }
       }
 }

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 Willexwun · Feb 02, 2020 at 04:53 PM 0
Share

Thank you very much!

Follow this Question

Answers Answers and Comments

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

Brick Breaker - Ball not bouncing back from the brick. 2 Answers

Rigidbody and Collision choices for 2D Adventure 0 Answers

How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer

How do you make a car drift in unity 2D 1 Answer

Snake Movement Problem 0 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