BoxCollider2D + Rigidbody2D bug

So here’s the problem : in my scene, I have two main objects aside from the main camera, a player (with its own Rigidbody2D, BoxCollider2D and Animator components along with a C# script) and a 64x64 grey box with only a BoxCollider2D.

When the player tries to jump on the grey box, or, to be more precise, when the player is going upwards while hitting a lateral border of the box, the player’s jump speed increases and it goes flying above the grey box for absolutely no reason.

Knowing my explanations might not be the best, I included a picture from the game itself that describes visually the problem:

[28588-collision+bug.png|28588]

Here is the C# code of the player animation and movement actions:

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {

	Animator theAnimator;							// The reference of the Mecanim component, where all the animations are handled

	KeyCode goLeft;									// Key for going left
	KeyCode goRight;								// Key for going right
	KeyCode jump;									// Key for jumping

	bool isFacingRight = true;						// Is the character facing right? Will be used for character sidechange.

	float speed = 3;								// Walking speed
	float jumpSpeed = 400;							// Jumping speed

	// Set all the necessary variables before the script starts
	void Awake() {
		goLeft = KeyCode.A;
		goRight = KeyCode.D;
		jump = KeyCode.Space;
		theAnimator = GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {
		// Check for jump
		if (Input.GetKey(jump) && !theAnimator.GetBool ("IsJumping")) {
			theAnimator.SetBool ("IsJumping", true);
			rigidbody2D.AddForce (new Vector2(0f, jumpSpeed));
		}

		// Check for x movement change
		if (Input.GetKey (goLeft)) {
			if (!theAnimator.GetBool ("IsJumping")) theAnimator.SetBool ("IsWalking", true);
			rigidbody2D.velocity = new Vector2(-speed, rigidbody2D.velocity.y);

			if (isFacingRight) {
				SideChange ();
			}
		}
		else if (Input.GetKey (goRight)) {
			if (!theAnimator.GetBool ("IsJumping")) theAnimator.SetBool("IsWalking", true);
			rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y);

			if (!isFacingRight) {
				SideChange ();
			}
		}
		else if ((!Input.GetKey (goLeft) && !Input.GetKey (goRight)) || (Input.GetKey (goLeft) && Input.GetKey (goRight))) {
			theAnimator.SetBool ("IsWalking", false);
			rigidbody2D.velocity = new Vector2(0, rigidbody2D.velocity.y);
		}
	}

	void OnCollisionEnter2D(Collision2D colInfo) {
		if (colInfo.collider.tag == "Groundable") {
			theAnimator.SetBool ("IsJumping", false);
		}
	}

	void SideChange() {
		isFacingRight = !isFacingRight;

		Vector3 theScale = transform.localScale;
		theScale.x *= -1;
		transform.localScale = theScale;
	}
}

And finally, here are the component parameters of the player:

28589-parameters.png

Could I know what I have possibly done wrong either in my code or in the component parameters (or maybe both?), and, if so, how can I fix them? Thank you.

Your box has the tag “Groundable”. Therefore when the player hits the side of the box isJumping is reset, allowing him to start jumping again. Because you addForce in Update () it is possible to get multiple frames of added force. Hence the high jump.

Solving is not so easy. I would suggest adding a small collider on the top surface of the box that has the groundable trigger. I would also suggest adding the jump force in fixed update.

Note as your script is currently written you should be able to jump while falling too, as long as the fall did not begin with a jump.