- Home /
Question by
UniluckStudios · Oct 14, 2017 at 11:25 PM ·
2drigidbody2dphysics2d2d-physicsboxcollider2d
Ball bounces at different heights when it shouldn't
I have a simple game where i have a ball that falls onto a square. I have code set up that makes it so that the ball can only bounce when it is touching the cube and when i press any button. I want the ball to jump at the same height every time i press the button but it won't. Most of the time it jumps the same height but on some occasions, the ball will either bounce higher. Here is code:
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.tag == "Death") {
Debug.Log ("You have succeeded");
canJump = true;
}
}
void OnCollisionExit2D (Collision2D col) {
if (col.gameObject.tag == "Death") {
canJump = false;
circle.GetComponent <PlanetRotation> ().rotateSpeed = 0;
}
}
void Update () {
if (canJump == true) {
if (Input.anyKey) {
rb.AddForce (Vector2.up * jumpHeight);
}
}
}
public void AdjustJumpHeight(float newJumpHight) {
jumpHeight = newJumpHight;
}
}
Comment