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.