Diminuer la vitesse d'un personnage quand il est en l'air
Publié : 14 Nov 2020 19:05
Voici mon script :
Je voudrais baisser la vitesse de déplacement quand mon personnage est en l'air. Comment faire ?
Merci.
Code : Tout sélectionner
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
private bool isJumping;
private bool isGrounded;
public Transform groundCheckLeft;
public Transform groundCheckRight;
public Rigidbody2D rb;
private Vector3 velocity = Vector3.zero;
void FixedUpdate()
{
isGrounded = Physics2D.OverlapArea(groundCheckLeft.position, groundCheckRight.position);
float horizontalMovement = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
if (Input.GetButtonDown("Jump") && isGrounded)
{
isJumping = true;
}
MovePlayer(horizontalMovement);
}
void MovePlayer(float _horizontalMovement)
{
Vector3 targetVelocity = new Vector2(_horizontalMovement, rb.velocity.y);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref velocity, .05f);
if(isJumping == true)
{
rb.AddForce(new Vector2(0f, jumpForce));
isJumping = false;
}
}
}
Merci.