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 TomDanes87 · Apr 04, 2014 at 05:43 PM · c#arrayhow

How to select adjacent cubes along edges?

I have a grid of cubes, I am using raycasting to select which ones are hit.

 public void Click1(object h)
     {
         RaycastHit hit = (RaycastHit)h;
         GameObject hitGameObject = hit.collider.gameObject;
     
        Debug.Log("HIT at '" + hit.point + "' RECEIVED IN '" + gameObject.name + "'");
 }

Image 1

That works fine.

What I would like todo is select adjacent cubes to the one selected, like below:

Image 2

And then transform them as I please.

It's the method for selecting cubes to form the cross I am having problems with.

Currently I am using Collider[] nearObjects = Physics.OverlapSphere(hit.point, 1.75F); to get a list of objects near the hit and then iterating through the collider array, but for obvious reason it doesn't achieve what I am after.

Any suggestions as what to try? Or any help. I have a feeling I may need a multidimensional array, but been new to c# I may be very wrong.

screen shot 2014-04-04 at 1.20.58 pm.png (22.2 kB)
screen shot 2014-04-04 at 1.24.11 pm.png (23.3 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Apr 04, 2014 at 07:12 PM

The better way to solve this problem is to put a reference to all of your cubes in a 2D array. Then when the user clicks, you can go to your array and get all the cubes in the specified row and specified column. If you really want to do it dynamically, you can use Physics.RaycastAll() and do a raycast in the four directions.

An in-between solutions would be to get an array of all the cubes. You could do it by putting the same tag on all the cubes and use GameObject.FindGameObjectsWithTag(). Then you could simply compare both the x and y position of the clicked cubed against the x and y positions of all the entries in the array. You will need some sort of tolerance (i.e. don't compare floats directly). All the cubes with close 'y' positions and all the cubes with close 'x' positions will be the ones you want.

Comment
Add comment · Show 2 · 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 TomDanes87 · Apr 04, 2014 at 08:08 PM 0
Share

Thanks Robert, I thought I was over complicating it a bit.

I am currently using tags on my objects, so hopefully it shouldn't be too much of a problem.

I shall give your suggestions as try.

avatar image robertbu · Apr 04, 2014 at 08:36 PM 0
Share

Untested, but here is some code to show what I mean about the comparison:

 List<GameObject> GetRowCol(float x, float y) {
     List<GameObject> list = new List<GameObject>();

     GameObject[] gos = GameObject.FindGameObjectsWithTag("SomeTag");
     foreach (GameObject go in gos) {
         if (($$anonymous$$athf.Abs (go.transform.position.x - x) < 0.05) || ($$anonymous$$athf.Abs (go.transform.position.y - y) < 0.05)) {
             list.Add(go);
         }
     }
     return list;
 }
avatar image
0

Answer by NecrosDk · Apr 06, 2014 at 04:37 AM

I am doing this exact thing in a game I released recently. Basically what I do is when the first tile gets hit by the mouse raycast, the tile sends raycasts in all four directions, which then triggers whatever I need to do with them, and then a new raycast, until there is nothing more to hit. Essentially I just make a chain reaction :)

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 TomDanes87 · Apr 06, 2014 at 04:56 AM 0
Share

I'll give that a try.

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Using an array instead 2 Answers

Array Issue 1 Answer

how do I use static bool Contains (T[]array, T item); ? 1 Answer

Getting ArgumentOutOfRangeException on array c# 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