- Home /
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.
http://docs.unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html
example :
var objectsInRange : Collider[] = Physics.OverlapSphere(transform.position, radius);
for (var object : Collider in objectsInRange)
{
//
}
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
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 =]
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.
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
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.
will that many hash tables have a big performance hit? there can be up to 100 blocks?
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.
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); } } }
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.
Your answer
Follow this Question
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