Voici mon code
Code : Tout sélectionner
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
private bool isGrounded;
private bool isJumping = false;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask collisionLayers;
public Rigidbody2D rb;
public Animator animator;
public SpriteRenderer spriteRenderer;
private Vector3 velocity = Vector3.zero;
private float horizontalMovement;
private float verticalMovement;
void Update()
{
if (Input.GetButtonDown("Jump") && isGrounded)
{
isJumping = true;
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
animator.SetTrigger("Jump");
}
Flip(rb.velocity.x);
float characterVelocity = Mathf.Abs(rb.velocity.x);
float verticalVelocity = rb.velocity.y;
animator.SetFloat("VerticalVelocity", verticalVelocity);
animator.SetFloat("Speed", characterVelocity);
animator.SetBool("IsGrounded", isGrounded);
animator.SetBool("IsJumping", isJumping);
}
void FixedUpdate()
{
horizontalMovement = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
verticalMovement = rb.velocity.y;
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, collisionLayers);
if (isGrounded)
{
isJumping = false;
}
MovePlayer(horizontalMovement, verticalMovement);
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * Physics2D.gravity.y * Time.deltaTime;
}
}
void MovePlayer(float _horizontalMovement, float _verticalMovement)
{
Vector3 targetVelocity = new Vector2(_horizontalMovement, _verticalMovement);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref velocity, .05f);
}
void Flip(float _velocity)
{
if (_velocity > 0.1f)
{
spriteRenderer.flipX = false;
}
else if (_velocity < -0.1f)
{
spriteRenderer.flipX = true;
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
Et mon Animator, j'utilise le isJumping dans les transition de l'idle et run à jump et le isgrounded de la jump à l'idle et run. Si quelqu'un a une solution, je lui serait reconnaissent.