Finish Player Animation
- , opens subtitles settings dialogsubtitles settings
- subtitles off
- , selectedEnglish Captions
- 2x
- 1.5x
- , selected1x
- 0.5x
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
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.
csharp912animator.SetFloat("JumpingForce", rb.velocity.y);
This code assigns the value for the transition parameter between the jump and the fall.
Code Explanation
csharp99123456789101112131415if (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);
csharp912345if (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.
csharp912rb.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
).
csharp912animator.SetBool("isJumping", true);
This line sets a boolean parameter "isJumping" in the Animator component to true, triggering the Jumping animation.
csharp912345if (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.
csharp912animator.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.
csharp912isGrounded = 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.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal