2d Picture Object falls down from the Ground

Hey guys … ive been reading JavaScript and Unity Tutorials for a week now aiming to someday be able to make my own Epic 2d Game …

I learned the basic idea of Character Moving and jumping and it really worked on a plane and a cube project or a Cube over a Cube project and everything is really smooth .

i spent the whole night trying to figure how to make that with 2D objects … Like i draw a background and a character and i’m trying to make him walking on the ground but whenever i play the project the character falls down throw the Y dimension .

I’m Using this Code :

``````#pragma strict

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

function Start() {

}

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) {
rigidbody.AddForce(Vector3.up * jumpheight);
grounded = false;
}
}
``````

I thought that the problem was that i dont have a plane so actually the character could find something to stand on but even if i added one its the same issue … the character falls throw the plane , FYI i added the component Rigibody to the character in order to work with the gravity … am I missing something guys ?

thank u all

For 2D physics you need to use Rigigbody2D instead of simple 3D Rigidbody. That also apply for colliders.

Make a plane with BoxCollider2D and Rigidbody2D as components.
Make a longer plane (like a ground) below your box. And try it.

The Z axis is not important, 2D in unity works something like this

![1]

The collision will go al the way from negative to positive Z axis, your rigidbody’s2D will not go through the red line, it collide with all your rigidbody’s2D regardless of the Z axis

Read the documentation:

Hope it helped you