# Getting the instance of a cube that was collided with?

I have 10 cubes that are stacked on top of eachother. I have another cube that collides with the stack. How would I get the transform of the one of the stacked cubes which the other cube collided with?

First of all in order for your â€śother cubeâ€ť to collide with anything, it needs a Rigidbody component. When it has one the object is actually a physics object and can collide with other objects. When a collision happens, both objects would get an OnCollisionEnter callback which includes a Collision object that contains all information about the collision. It directly gives you the gameobject, the rigidbody and the collider of that other object. If you need more details about the collision, see the contacts which provide additional information like the collision normal vector and a few other things

So there is no way to do this without a rigidbody? I donâ€™t need the physics system.

Without the physics system there are no â€ścollisionsâ€ť. Collisions donâ€™t â€śjust happenâ€ť but they have to be tested for and needs to be resolved. Thatâ€™s the point of the physics system. Unity uses Nvidiaâ€™s PhysX for 3d physics which is essentially abstracted through the Rigidbody component and the various collider components.

Thank you. I guess the days where we used collisions in Game Engines without a physics system are gone.

Well, what do you mean by collisions then? Doing manual overlap checks for simple shapes can of course be done without a physics system. However The moment you have more complex shapes this is far from trivial and physics engines are highly optimised.

I thought that was obvious being that Iâ€™m using cubes? Complex shapes are not being used here. Just cubes.

Are they axis aligned? Or can they be rotated? If they are axis aligned, detecting an overlap is super simple. If they can be rotated (either the cubes in the scene or your moving cube) it gets a lot more complicated. Itâ€™s all doable, but would probably not scale well. when you have 1000 cubes in the scene you have to do the check 1000 times only to check one cube against those 1000 cubes. You donâ€™t have given much information about your usecase. Is it actually a 3d game / problem or a 2d game? What do you need this information for? Are you only interested in knowing which cubes may overlap with others or do you need additional information.

In any way, when doing those checks manually it means you have to have a list of all potential cube transforms so you can iterate through them and check each one against your moving cube. The Physics system uses a spatial storage structure internally to quickly filter out relevant objects by first doing an AABB check and only doing the detailed collision checks on those objects which are actually close.

Such checks may be simple when you put some constraints on them (like dealing with 2d instead of 3d or only having AABBs instead of rotated / scaled objects). However when asking for a generic collision detection function, the answer would simply be: Use the physics system.

1 Like

It just doesnt make any sense, invent something what is already invented.With this logic you could just stop using unity engine And create your own.But i bet you will not optimaze it better then Nvidia or Unity

1 Like

Your logic is to state that my logic is bad for not wanting to use a physics system for cubes and to not use Unity and make my own?

Ok Buddy!

I donâ€™t understand this statement. Maybe what youâ€™re calling a â€śphysics systemâ€ť differs from what others are referring to. Obviously a â€śRigidbodyâ€ť is not a physics system, itâ€™s just a component of it.

My question for clarification would be, if youâ€™re not using 3D physics then how are things â€śstackedâ€ť? What is detecting and resolving the â€ścollisionsâ€ť in this stack? Presumably no collider components either because that is the physics system. It would be helpful to define your existing set-up.

Are you asking how to detect collisions using the Bounds type or a BoxCollider type? If you want to do it via colliders then you can use a collider such as BoxCollider and then use Physics.OverlapBox to find out what it overlaps but thatâ€™s using the physics system so not sure.

Beyond that, itâ€™s not clear specifically what it is you need here.

1 Like

I think your logic is to try invent something better for detecing colissions then physics system.If there is something you can be sure that more people using it.That is what doesnt make sense for me.

If youâ€™re just making a 2D grid-based game (like Tetris or Bejeweled or Rogue or Chips Challenge) you NEVER need physics.

Thatâ€™s the entire point of a grid game: you do the checks yourself by keeping a 2D grid that represents the interesting parts of the world.

Often times you may have multiple â€śstackedâ€ť correlated 2D grids, such as one for the ground and walls, one for the enemies, one for the items, etc.

All the wiggly-jiggly stuff in these games is just canned driven animations and tweens.

Any tutorial that attempts to use physics for a 2D grid game is doing it The Hard Wayâ„˘. Yes, they might make it work, but wow, what a lot of extra things to hassle with and have go wrong.

1 Like

The cubes are not stacked using physics. They are just on top of eachother fixed in place as the level wall. The cube that collides with them just moves left and right, up or down. All I need is a way to check for a collision between the moving cube and to find out which cube in the wall it hit.

Is there a way to do that without a rigidbody or using the physics system?

Put colliders on them, use raycasts, overlaps, whatever. Job done.

1 Like

So the previous answer is certainly correct but you said without using the physic system so no colliders. I guess you mean something else when you say that so the source of all these posts and confusion.

â€¦

1 Like