2D platform character not falling correctly

Hi guys/gals,

I’m putting together a platform game for learning purposes which is nice and simple. So far my guy runs left and right and jumps but although he falls correctly when simply finished jumping, if he has interacted with a platform and he has jumped, he continues to run after the platform has finished and doesn’t fall down to the lower platform until he has hit the screen edge (I’ve set up to box collider2d objects, one each side of the screen which are invisible and stop the player running off screen).

Please take a look at the code below and let me know if you can see what’s contradicting what as I am now baffled.

So you’re all aware, I have a script for the right button which sets jumping to true when pressed, if the player is not already jumping or falling.

if (jumping) {
Debug.Log (“jumping”);
anim.SetBool(“runLeft”, false);
anim.SetBool(“runRight”, false);
anim.SetBool(“idle”, true);
if (jumpVelocity >= 1) {
player.transform.position += Vector3.up * ((jumpForce * jumpVelocity) * Time.deltaTime);
jumpVelocity–;
} else {
jumping = false;
falling = true;
jumpVelocity = 25;
}
}
if (falling) {
Debug.Log (“falling”);
player.transform.position += Vector3.down * ((jumpForce * velocity) * Time.deltaTime);
velocity++;
} else {
velocity = 1;
}

}

public void OnTriggerStay2D(Collider2D other){
if (other.tag == “wall”) {
if (aRunRight) {
player.transform.position += Vector3.left * 0.5f;
} else if (aRunLeft) {
player.transform.position += Vector3.right * 0.5f;
}
aRunLeft = false;
aRunRight = false;

anim.SetBool(“runLeft”, false);
anim.SetBool(“runRight”, false);
anim.SetBool(“idle”, true);

Debug.Log (“Hit Wall”);
}

if (other.tag == “platform”) {
falling = false;
if (aRunLeft) {
anim.SetBool (“runRight”, false);
anim.SetBool (“idle”, false);
anim.SetBool (“runLeft”, true);
} else if (aRunRight) {
anim.SetBool (“idle”, false);
anim.SetBool (“runLeft”, false);
anim.SetBool (“runRight”, true);
} else {
anim.SetBool (“runLeft”, false);
anim.SetBool (“runRight”, false);
anim.SetBool (“idle”, true);
}
} else {
if (!jumping) {
falling = true;
}
}

}

}

Thanks all

James.

Check out this page for how to post code nicely on the forums: Using code tags properly

Remember that if you want to use physics, it’s a good idea to move your game objects with force or velocity, rather than adjusting the transform directly.

Also, as I tried to glance at your code, be sure you’re not overriding the gravity when you move. For example, do not tell the game that your next position or velocity is some distance to the left/right and no change in height, if you are falling. I think you are doing gravity yourself there.

Also, you must have a rigidbody2D on your character if you want the TriggerStay message to be called. Plus that must be a trigger.

After all that has been said, if you have not already gone over some of the tutorials on this site, you should really consider doing it to help yourself: Learn
Check out the introduction stuff for scripting, unity essentials, and 1 or 2 starter tutorials, as well as whatever other beginner lessons seems helpful to you. :slight_smile:

Good luck.

After reading your post I’ve decided to neatly re write my Class that was used for the player Controls.

I think I’d used this Class as self-tuition exercise and as such I’m not doing things in the most effective way.

It’s all running far smoother now, thanks for the advice!

James.

No problem. Take it easy & have fun! :slight_smile: