Contenido del Curso
Fighting Game in Unity
Fighting Game in Unity
Finish Player Animation
Jump Animation
For the jump, we will have two animations: Since the player will jump and then fall, we need to create two animations for that. The transition between them will be based on the player's "y velocity"; if y is positive, he is jumping, and if y is negative, he is falling. We can obtain the y velocity from the Rigidbody2D component attached to the player.
This code assigns the value for the transition parameter between the jump and the fall.
Code Explanation
This line checks if the Space key is pressed (Input.GetKeyDown(KeyCode.Space)
), the player is on the ground (isGrounded
), and the player is not currently attacking (animator.GetBool("isAttacking") == false
). If all conditions are met, the following block of code will execute.
This line adds an upward force to the Rigidbody (rb
) component attached to the player, simulating a jump. The force applied is in the upward direction (Vector2.up
) with magnitude defined by jumpForce
, and it is applied impulsively (ForceMode2D.Impulse
).
This line sets a boolean parameter "isJumping" in the Animator component to true, triggering the Jumping animation.
This line checks if the player is currently jumping (animator.GetBool("isJumping") == true
), the player's vertical velocity is negative (rb.velocity.y < 0
), and the player is on the ground (isGrounded
). If all conditions are met, the following block of code will execute.
This line sets the boolean parameter "isJumping" in the Animator component to false, indicating that the player has finished jumping and falling and will change its animation to idle.
This line casts a ray downwards from the position of the player's feet (playerFeet.position
) to detect if the player is grounded. It returns true if the ray intersects with any collider in the ground layer within a distance of 0.1 units, updating the isGrounded
variable accordingly.
¡Gracias por tus comentarios!