Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.9 kB
2
Indexable
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting.FullSerializer;
using UnityEngine;

public class mouvementCeleste : MonoBehaviour
{
    public Animator animator;
    private float horizontal;
    [SerializeField]private float speed;
    [SerializeField]private float jumpingPower;
    private bool isFacingRight = true;
    private bool onWall;
    public bool isGrabbingWall = false;


    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private Transform groundCheck;
    [SerializeField] private LayerMask groundLayer;


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x,jumpingPower);
            animator.SetBool("IsJumping",true);
        }

        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }
        onWall = Physics2D.Raycast(transform.position, Vector2.right, 0.05f, groundLayer) || Physics2D.Raycast(transform.position, Vector2.left, 0.05f, groundLayer);
        if (onWall && Input.GetKeyDown(KeyCode.LeftShift))
        {
            Debug.Log("grab");
            isGrabbingWall = true;
            rb.velocity = Vector2.zero; // Arrêter tout mouvement
            rb.gravityScale = 0; // Désactiver la gravité pour l'accrochage
        }
        if (isGrabbingWall && Input.GetKeyUp(KeyCode.LeftShift))
        {
            Debug.Log("ungrab");
            isGrabbingWall = false;
            rb.gravityScale = 0.3f; // Rétablir la gravité
        }
        Flip();
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
        if (rb.velocity.x != 0 && IsGrounded())
        {
            animator.SetBool("IsRunning",true);
        }
        else
        {
            animator.SetBool("IsRunning",false);
        }
        if (rb.velocity.y < 0.001 && !IsGrounded())
        {
            animator.SetBool("IsFalling",true);
            animator.SetBool("IsJumping",false);
        }
        else
        {
            animator.SetBool("IsFalling",false);
        }
    }


    private bool IsGrounded()
    {
        return Physics2D.OverlapCircle(groundCheck.position, 0.02f, groundLayer);
    }

    private void Flip()
    {
        if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
        {
            isFacingRight = !isFacingRight;
            Vector3 localScale = transform.localScale;
            localScale.x *= -1f;
            transform.localScale = localScale;
        }
    }
}
Leave a Comment