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.
csharpanimator.SetFloat("JumpingForce", rb.velocity.y);
This code assigns the value for the transition parameter between the jump and the fall.
Code Explanation
csharp991234567891011121314if (Input.GetKeyDown(KeyCode.Space) && isGrounded && animator.GetBool("isAttacking") == false){rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);animator.SetBool("isJumping", true);}if (animator.GetBool("isJumping") == true && rb.velocity.y < 0 && isGrounded){animator.SetBool("isJumping", false);}animator.SetFloat("JumpingForce", rb.velocity.y);isGrounded = Physics2D.Raycast(playerFeet.position, Vector2.down, 0.1f, groundLayer);
csharp91234if (Input.GetKeyDown(KeyCode.Space) && isGrounded && animator.GetBool("isAttacking") == false){// Code to execute when the conditions are met}
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.
csharprb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
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
).
csharpanimator.SetBool("isJumping", true);
This line sets a boolean parameter "isJumping" in the Animator component to true, triggering the Jumping animation.
csharp91234if (animator.GetBool("isJumping") == true && rb.velocity.y < 0 && isGrounded){// Code to execute when the conditions are met}
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.
csharpanimator.SetBool("isJumping", false);
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.
csharpisGrounded = Physics2D.Raycast(playerFeet.position, Vector2.down, 0.1f, groundLayer);
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.
Bedankt voor je feedback!
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.