n00b Time: Can't get Jumping or Collision Detection to work :/

Instead of pulling my hair out and violently hurling things across the room, I thought I’d ask for help from people who know what they’re doing. I’m making a 2D side-scroller, and right now I just have a sprite that can move left and right and the sprite changes when he’s moving left and right along with a rectangle below the player named “Floor”. Floor has a Box Collider 2D on it. The Player has a “Rigidbody 2D” and a “Box Collider 2D” component in addition to the code below.

Jumping does not work at all but the Player does move and animate correctly. He does collide with Floor even though Debug.Log says nothing when he does collide. I’ve been following all of the Unity tutorials and looked at the Unity Scripting References. I’ve searched all up and down these forums and while this came very close to answering my question, I am unlike the user who posted that thread because my object tagged “Floor” is not set as a Trigger.

Here’s the code attached to the Player, but please keep in mind I am really really new to programming in general and I’ve just spent months learning Javascript (I’m specifically having trouble with lines 8-13 and 59-64):

#pragma strict

var speed : float;
var anim : Animator;
var hit : Collision;
var grounded = false;

function OnCollisionEnter(hit : Collision) {
	if (hit.gameObject.tag == "Floor") {
		grounded = true;
		Debug.Log("Shit's hitting");
	}
}

function Start () {
	anim = gameObject.GetComponent(Animator);
}

function Update () {	
	//Getting input
	var Left = Input.GetKey(KeyCode.LeftArrow);
	var Right = Input.GetKey(KeyCode.RightArrow);
	var Up = Input.GetKey(KeyCode.UpArrow);
	var Down = Input.GetKey(KeyCode.DownArrow);
	var Jump = Input.GetKey(KeyCode.Z);
	
	//Commiting speed to sprite animation
	anim.SetFloat("Speed", speed);
	
	/*---------------------------------
			Horizontal Movement
	----------------------------------*/
	if (Left) {
		if (speed <= 4) {
			speed += 0.1;
		}
		else {
			speed += 0;
		}
		anim.SetInteger("Direction", -1);
		transform.Translate(Vector2.right * -speed * Time.deltaTime);
	}
	else if (Right) {
		if (speed <= 4) {
			speed += 0.1;
		}
		else {
			speed += 0;
		}
		anim.SetInteger("Direction", 1);
		transform.Translate(Vector2.right * speed * Time.deltaTime);
	}
	else {
		speed = 0;
	}
	/*-------------------------
				Jump
	-------------------------*/
	if (Jump) {
		while (grounded) {
			rigidbody2D.AddForce(Vector2.up * 1000);
			grounded = false;
		}
	}
}

I realize there are easier ways of getting horizontal movement by utilizing Input.GetAxis(“Horizontal”) but I feel like it should be within my capability to program for specific buttons if need be. I don’t know. :face_with_spiral_eyes: Any help would be greatly appreciated, but please keep in mind you might need to spell seemingly obvious things out for me because I am a sound editor, I studied sound for years and I am not a programmer. Not yet anyway, amiright? :o

I haven’t worked with the 2D physics at all, but I think most of the general principles are the same.

First possible problem I see is that you really should not be modifying the transform of an object with a rigidbody. The physics thinks it is in a certain spot and if it is not you can cause problems/jitteriness and you lose efficiency as the physics engine will have to do extra work to revalidate its state.

You should only move a rigidbody by adding forces. If its rotation or position is frozen you can manually move it using the transform or, better yet, using rigidbody.MovePosition or MoveRotation. I believe the 2D physics originally shipped without those functions but they are planned to be added (or may already be). They work with the physics engine to properly handle collisions as if the object very quickly moved/rotated in a single frame.

Is the collision function called at all? You could try just logging the name of the object for all collisions and see if anything comes up.

Nothing looks obviously wrong to me, but it’s easy to overlook something when I can’t have the code running in front of me to experiment with.

It could be the layers are disabled in the physics options. Double-check your tag and the properties of the colliders.