using UnityEngine; using System.Collections; using System.Collections.Generic; public class Jump : AbstractBehavior { public float jumpHeight = 5; public float airJumpHeight = 5; public int airJumpCount = 1; public float wallJumpHeight = 3; public float wallJumpHorizontalSpeed = 5; bool jumpRequested; int airJumpsMade = 0; void Update() { if (!jumpRequested && inputState.GetButtonPressed(inputButtons[0])) { jumpRequested = true; } float jumpState; Vector2 vel = body.velocity; jumpState = Mathf.Abs(vel.y) / GetJumpVelocity(jumpHeight); jumpState = 1 - Mathf.Clamp01(jumpState); if (jumpState == 1) { jumpState = 0; } jumpState = Mathf.Sqrt(jumpState); animator.SetFloat("jump state", jumpState); } void FixedUpdate() { if (jumpRequested) { Activate(); jumpRequested = false; } } public void Activate() { if (collisionState.onGround) { // ground jump airJumpsMade = 0; Vector2 vel = body.velocity; vel.y = GetJumpVelocity(jumpHeight); body.velocity = vel; } else if (collisionState.onWall) { // wall jump Vector2 vel = Vector2.up * GetJumpVelocity(wallJumpHeight); if (collisionState.onWallLeft) { vel += Vector2.right * wallJumpHorizontalSpeed; inputState.direction = Directions.Right; } if (collisionState.onWallRight) { vel -= Vector2.right * wallJumpHorizontalSpeed; inputState.direction = Directions.Left; } body.velocity = vel; } else if (airJumpsMade < airJumpCount) { // air jump Vector2 vel = body.velocity; vel.y = GetJumpVelocity(airJumpHeight); body.velocity = vel; airJumpsMade ++; } } float GetJumpVelocity(float height) { float gravity = Physics2D.gravity.y*body.gravityScale; return Mathf.Sqrt(-2 * gravity * height); } }