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 Prodigga · Jul 23, 2012 at 02:34 PM · physicstriggerscollisiondetection

See if an object is in contact with any number of a certain type of object

Hey guys, I am making a 'Jenga' game, and i am trying to detect when the player has completely pulled a piece out of the tower. I need to check whether there are any other blocks touching the current block. There can be blocks above, bellow, left and right of the current block.

I wanted to use something like a Box Cast in these directions, and check if I hit any blocks. But unity doesnt have a box cast! I also tried to put 4 box collider/triggers on all sides, but I cant just query a trigger whether or not its in contact with something at a given time. I have to use OnTriggerX Functions and this makes it impossible to track whether or not a block is hitting another, since a block may hit more than 1 block.

Comment
Add comment · Show 4
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 AlucardJay · Jul 23, 2012 at 02:47 PM 1
Share

http://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html

example :

 var objectsInRange : Collider[] = Physics.OverlapSphere(transform.position, radius);
 for (var object : Collider in objectsInRange) 
 {
     //
 }
avatar image Prodigga · Jul 23, 2012 at 02:49 PM 0
Share

anything more accurate than this? I mean, it works, but its more accurate along the X axis than it is along the Y and Z axis'. The Jenga blocks are rectangular

avatar image AlucardJay · Jul 23, 2012 at 03:12 PM 1
Share

sry, that's all I know of. maybe check with spheres at 3 positions on the block. Hope one of the pro's can help =]

avatar image Julien-Lynge · Jul 23, 2012 at 03:57 PM 1
Share

Have you looked into http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionStay.html

Straight from the docs: "OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider."

Physics isn't something I've needed to use extensively in our projects, so let me know if that doesn't work for you.

1 Reply

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

Answer by Bovine · Jul 23, 2012 at 06:22 PM

As @Julien.Lynge syasm OnCollisionStay(), Enter and Exit could work for you.

I suggest that you:

  • Have a hash within the script for each block

  • For each OnCollisionEnter() you can add the collider to the hash

  • Remove colliders in OnCollisionExit()

  • When you remove a collider you can check to see if the size of the hash is 0

You could maybe do a similar thing with OnCollisionStay() building a hash each frame and in LateUpdate() checking to see the size of the hash.

Hope that helps

Comment
Add comment · Show 6 · 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 Bovine · Jul 23, 2012 at 06:23 PM 0
Share

There may be some oscillation, so you might want to check for no contact within the last 100$$anonymous$$S say, just in case you get some rapid Enter/Exit stuff going on.

avatar image Prodigga · Jul 24, 2012 at 04:09 AM 0
Share

will that many hash tables have a big performance hit? there can be up to 100 blocks?

avatar image Bovine · Jul 24, 2012 at 05:57 AM 0
Share

Possibly. It depends on your platform but each block is unlikely to get more than a few blocks.

You could use an in ins$$anonymous$$d actually and that should work as you should only get the enter and exit per collider.

If you did it in OnCollisionStay() and counted the result in LateUpdate() which I think will come after. Then that might be more reliable.

The interesting thing will be whether a block at rest can end up at rest and not touching another collider.

You'll want to tag the blocks probably as well so you can count only blocks and not things like the floor!

I'll update my answer to reflect these new suggestions.

avatar image Prodigga · Jul 24, 2012 at 09:05 AM 0
Share

the hash table solution works really nicely:

void OnCollisionStay(Collision collision) { if(collision.gameObject.GetComponent() != null) { blocksCollidingWith[collision.gameObject] = GetDirection(collider.transform.position); } }

void OnCollisionExit(Collision collision) { if(collision.gameObject.GetComponent() != null) { if(blocksCollidingWith.Contains$$anonymous$$ey(collision.gameObject)) { blocksCollidingWith.Remove(collision.gameObject); } } }

avatar image Prodigga · Jul 24, 2012 at 09:06 AM 0
Share

I have a 'getdirection' function which just sets the key to the 'direction' of the jenga cube, so I can figure out which ones are the top row.

Show more comments

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detecting Input on ~100 Objects 2 Answers

Problem: One Trigger activating a seperate Trigger it is not in contact with. 1 Answer

Did triggers inside loaded gameobjects break in a recent update? 0 Answers

Collision Handling (Pushout and Sliding) for Customized Collision Object? 0 Answers

Pulling boxes 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