# Collider2D and Rigidbody2D, how do they work?

I have been learning JavaScript and Unity for a week now. I learned how to make Cube as a Ground and another Cube as a player and I used this code to make the Player Cube move forward and backward and jumping

var walkspeed: float = 5.0;
var jumpheight: float = 250.0;
var grounded = false;

function Update() {

rigidbody.freezeRotation = true;

if (Input.GetKey("a"))
transform.Translate(Vector3(-1, 0, 0) * Time.deltaTime * walkspeed);
if (Input.GetKey("d"))
transform.Translate(Vector3(1, 0, 0) * Time.deltaTime * walkspeed);

if (Input.GetButton("Jump")) {
Jump();
}
}

function OnCollisionEnter(hit: Collision) {
grounded = true;
}

function Jump() {
if (grounded == true) {
grounded = false;
}
}


I also learned how to make a character hit box. how to make a sprite and animation. pretty much the basic stuff.

Couple of days ago I created simple ground in Photoshop and a simple character and imported them to Unity3D. Whenever I use my code above the character keeps falling from the scene. Like the character has nothing to stand on. After thinking about it it make sense because I really didn't make anything to make the player character understand that he should stand on something so I started reading about this issue and I realized that there is something called Collider2D and Rigidbody2D.

Now I'm really stuck here I just don't know what to do. I applied the rigibody2d to my character picture and the Collider2D to the ground picture but whenever I play the project the gravity makes my character falls down.

This is my question:

How can I make the rigibody2d object realize that it shouldn't fall if there is a Collider2D object under it? So when I jump it's going to jump and the gravity going to bring it back to the ground.

• You mention cubes, those are 3D objects. Collider2D and Rigidbody2D are for 2D objects. Have you tried using RigidBody and an BoxCollider? Maybe worked through a simple tutorial on a character controller? – MichaelHouse Feb 24 '14 at 22:32
• @Byte56 well i was using cubes just to learn how to control but now im trying to learn 2d development .. and honestly there is not a single tutorial talking about Collider2D and Rigidbody2D and how to set them up .. – user42646 Feb 25 '14 at 4:09
• The 2D aspects of Unity3D are pretty new. There might not be complete tutorials for them yet. Make sure you're setting up an entire scene for 2D however, mixing 3D with 2D objects isn't going to work very well from my understanding. – MichaelHouse Feb 25 '14 at 5:22
• There is documentation and tutorials available for 2D physics on the unity website. – bummzack Jun 11 '14 at 18:06

Here is a bullet point list of items you may want to look over.

1. Your function for the collision is wrong. You’re using “OnCollisionEnter” when you need to be using “OnCollisionEnter2D” if you’ve attached 2D versions of the components to your game objects.

2. For your left/right input do not use transform.Translate, this ignores physics calculations and can cause weird effects (like appearing partially in a wall and being bounced back out of it). Use rigidbody2D.AddForce(...) or rigidbody2D.MovePostion(...). This takes physics into account when moving the object.

3. Your ‘floor’ and ‘character’ both need a Collider2D component. The moving object ‘character’ also needs the Rigibody2D component. Without a Collider2D component on both objects, collision will not be detected by the Rigibody2D.

4. Make sure your objects are actually intersecting. If you’re in 2D space, make sure they are both at the same Z (or whatever axis you’ve chosen to represent your depth into the screen).

5. Make sure the bounds of the Collider2Ds are intersecting. Select both objects in the hierarchy to see the green outline of the Collider2D, make sure they’re large enough. Hopefully these point you in the right direction.

Quick overview of Collider2D and Rigidbody2D.

Objects which have Rigidbody2D components will interact with Collider2D components automatically (unless they've specifically been told not to through some other method).

Collider2D have two modes to them, trigger and non trigger.

A trigger Collider2D does not invoke physics on a Rigidbody2D. They can be triggered by other Collider2Ds and are pushed through the OnTriggerEnter2D/Stay2D/Exit2D methods.

A non-trigger Collider2D will invoke physics on a Rigidbody2D. This typically means the Rigidbody2D item will apply various forces if its Collider2D hits another Collider2D.

Rigidbody2D controls the physics, but your character needs a Collider2D as well otherwise no collision will occur.

- Character GameObject
- Collider2D
- RigidBody2D

- Any GameObject that character will collider with
- Collider2D


First of all, it is best to work strictly in a 2D environment by disregarding any form of 3D entity such as cubes. I recommend importing in sprites and re-configuring your Unity project so that all the scripts are working in accordance to them.

Also, in order to make your RigidBody2D be able to recognize impact with a Collider2D, your RigidBody2D must have a Collider2D (BoxCollider2D, etc) assigned as well.

The reason for this is because the RigidBody2D simply handles the physics of its assigned entity. However, in order for the entity to even be able to collide and interact with the environment, you must define its boundaries/collision area through assigning a Collider2D.

http://docs.unity3d.com/ScriptReference/Rigidbody2D.html

"... Adding a Rigidbody2D component to a sprite puts in under the control of the physics engine. By itself, this means that the sprite will be affected by gravity and can be controlled from scripts using forces. By adding the appropriate collider component, the sprite will also respond to collisions with other sprites."