- Home /
2d Platformer Jumping Not Working
The movement works, but the OnCollisionEnter2d doesn't seem to set jumpEnable to true.(I have also tried OnCollisionEnter, but the problem persisted) Also I am 100% sure that the platforms have the tag "Platform", so that is not the problem.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public int moveSpeed;
public int jumpHeight;
public bool jumpEnable;
void Start () {
}
void Update () {
transform.Translate (Vector2.right * Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime);
if (Input.GetKey("space") && jumpEnable == true){
rigidbody2D.AddForce(Vector2.up * jumpHeight);
jumpEnable = false;
}
}
void OnCollisionEnter2d (Collision2D col) {
if (col.gameObject.tag == "Platform") {
jumpEnable = true;
}
}
}
Answer by StephenMorris · May 19, 2014 at 06:06 AM
It should be OnCollisionEnter2D not OnCollisionEnter2d (notice the upper and lower case difference on the D)
Also $$anonymous$$d that this way you could collide with the platform from the bottom and the side making you able to jump again, which I guess isn't what you want to achieve. Consider using a raycast casting beneath the player.
See the documentation on raycasts here: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
Your answer
Follow this Question
Related Questions
Why do I double jump? This isn't supposed to happen... 2 Answers
Why does my OnCollisionEnter2D not work? 3 Answers
jumping through and on a platforms(2d) 1 Answer
Stop or cancel movement if the player hits wall. 2 Answers
How to determine if the player can jump, without using raycasts. (2D) 1 Answer