- Home /
Question by
ajoshi91 · May 29, 2017 at 06:30 AM ·
unity 5unity 2dcollider2d
Jumping from Obstacle while adding velocity to Player
Am creating a free runner game where environment containing obstacles and background is moving. When the player jumps on an obstacle because of rigid body associated, it gets stuck to the obstacle and moves in the opposite direction. To counter this i added a code to add velocity to the player when its on the obstacle but due to some reason, 5 out of 10 times - the player fails to jump while on the obstacle. I tried Update() and fixedUpdate() also tried adding jump code inside the check-onObstacle condition but still there at times the player doesn't jump. Please suggest.
void Update () {
if (inputState.standing) {
if (inputState.actionButton && transform.position.y<45) {
body2d.velocity = new Vector2 (transform.position.x < 0 ? forwardSpeed : 0, jumpSpeed);
} if (playerOnObstacle) {
if (inputState.actionButton && transform.position.y<45) {
body2d.velocity = new Vector2 (transform.position.x < 0 ? forwardSpeed : 0, jumpSpeed);
}
body2d.velocity = new Vector2(body2d.transform.position.x < 0 ? forwardSpeed+10 : 0,0);
playerOnObstacle = false;
}
}
}
A box collider is added to which enables the playerOnObstacle flag.
Comment