- Home /
How to detect an object inside another object?
Hey, I just started to use Unity and I am trying to understand some basics for a 2D puzzle game I am making for a Unity class. In this game you are moving the player inside other block-type objects, and the movements of the player's movement is altered depending on which of these blocks is the player inside of (Some will allow the player to move only vertically and other only horizontally). I am trying to make an OnCollisionEnter function that will change the movement of the player whenever its inside a new block, but it doesn't seem to work, so I was just wondering if anyone had an advice for an inexperienced Unity user.
Thanks.
PS: It would help if you explained it to me like I was five....please.
Answer by Teh_Bucket · Aug 11, 2013 at 12:53 AM
You want to use box colliders on each block-object. Make sure the block-objects also have rigidbodies. These are both components, you can add them to each block-object in the Inspector. Set the block objects to be triggers and set the rigidbody to be kinematic (this will make sure they don't act like actual physics objects). http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
To detect if the player is in one, you have to make an OnTriggerEnter function on the main player. http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html