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 jpierre88 · Jul 24, 2013 at 03:17 AM · raycasthitspherecast

Get all surrounding gameObjects

I have a 8 x 8 sets of cube as so:

 for (var x = 0; x < 8; x++){
     for (var y = 0; y < 8; y++){
         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cube.transform.position = Vector3(x, y, 0);
     }
 }

When OnMouseDown on a cube I would like to get the set of surrounding cubes.

For example, if I click the blue cube, I want to get the all of the yellow cubes. Also is there a function to expand radius? For example, I want to include sets of red cubes with the yellow cubes.

alt text

grid.jpg (33.3 kB)
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 robertbu · Jul 24, 2013 at 09:19 AM 0
Share

For me, I'd put all game objects in a 2D array and use the array to find the cubes I want to select. If you don't want to use an array, you could use Physics.OverlapSphere(). For the 'position' parameter use the position of the blue cube. For radius, use a value somewhat larger than 1/2 of the face diagonal of the cube.

avatar image jpierre88 · Jul 25, 2013 at 06:58 AM 0
Share

@robertbu Even if I put all the game objects in a 2D array, how would you go about getting the surrounding cubes giving only the On$$anonymous$$ouseDown event? Physics.OverlapSphere() would seem easier, but I'm concerned about performance. I'm curious about the 2D approach.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BimSekai · Jul 24, 2013 at 02:34 PM

I'm not so good at javascript, but I can give you the equivalent in C#:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GridCube{
 
     public static List<GridCube> AllCubes = new List<GridCube>();
 
     private int x;
     private int y;
 
     private GameObject cube;
 
     // Use this for initialization
     void Start () {
         AllCubes.Add(this);
     }
 
     public bool HasPosition(int aX, int aY)
     {
         return (x == aX && y == aY);
     }
 
     public void Place(int aX, int aY)
     {
         if (cube != null)
         {
             GameObject.Destroy(cube);
         }
         x = aX;
         y = aY;
         cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
     }
 
     public int GetSpacesTo(GridCube targetCube)
     {
         int ySpaces = Mathf.Abs(this.y - targetCube.y);
         int xSpaces = Mathf.Abs(this.x - targetCube.x);
         return xSpaces + ySpaces;
     }
 
     public static GridCube GetCubeOfCoords(int aX, int aY)
     {
         foreach (GridCube gridcube in AllCubes)
         {
             if (gridcube.HasPosition(aX, aY))
             {
                 return gridcube;
             }
         }
         return null;
     }
 
     public static List<GridCube> GetCubesAround(GridCube centerCube , int nbCases)
     {
         List<GridCube> cubesAround = new List<GridCube>();
         foreach (GridCube gridCube in AllCubes)
         {
             if (centerCube.GetSpacesTo(gridCube) == nbCases)
             {
                 DoSomethingToThatCube();
             }
         }
     }
 }
 

This is very far from being optimised but it will do the work =) Does it suit you ?

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

16 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

Related Questions

Sphercast hitinfo TextureCoord rounded? 1 Answer

anyone know the maths to take into account Bounciness when calculating trajectories? 1 Answer

Why is my spherecast returning an incorrect hit on mesh collider? 1 Answer

Physics.SphereCastAll - how far did the trace travel? 3 Answers

How can the resultant velocity of an object after a potential collision be predicted? 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