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.
animator.SetFloat("JumpingForce", rb.velocity.y);
This code assigns the value for the transition parameter between the jump and the fall.
Code Explanation
if (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);
if (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.
rb.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
).
animator.SetBool("isJumping", true);
This line sets a boolean parameter "isJumping" in the Animator component to true, triggering the Jumping animation.
if (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.
animator.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.
isGrounded = 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.
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 3.33
Finish Player Animation
Scorri per mostrare il menu
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.
animator.SetFloat("JumpingForce", rb.velocity.y);
This code assigns the value for the transition parameter between the jump and the fall.
Code Explanation
if (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);
if (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.
rb.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
).
animator.SetBool("isJumping", true);
This line sets a boolean parameter "isJumping" in the Animator component to true, triggering the Jumping animation.
if (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.
animator.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.
isGrounded = 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.
Grazie per i tuoi commenti!