Home > Blockchain >  player movement problems in unity
player movement problems in unity

Time:07-28

Describe the problem: When I hit D key on my keyboard, the player moved to the right side. I hit the spacebar to jump then held the D key so my player stick to the right side of the platform. How can I stop this? (make the player fall off).
Illustrating video: google drive link

the script: using UnityEngine; using UnityEngine.InputSystem; public class PlayerMovement : MonoBehaviour {

    Vector2 vector2Move;
    Rigidbody2D rb2D;
    [SerializeField] float climbSpeed;
    [SerializeField] float playerSpeed;
    Animator animatorPlayer;
    [SerializeField] float jumpForce;
    bool isGrounded;
    Collider2D playerCollider;
    float theCurrentGravityScale;
    private void Awake()
    {
        rb2D = GetComponent<Rigidbody2D>();
        animatorPlayer = GetComponent<Animator>();
        playerCollider = GetComponent<Collider2D>();
    }
    void Start()
    {
        theCurrentGravityScale = rb2D.gravityScale;  
        
    }
    void Update()
    {
        Run();
        FlipSprite();
        ClimbLadder();
        CheckingIfPlayerIsJumping();
    }

    private void CheckingIfPlayerIsJumping()
    {
        if(rb2D.velocity.y > 0.0001)
        {
            animatorPlayer.SetBool("isGrounded", false);
            animatorPlayer.SetBool("isFalling", false);
        }
         if(rb2D.velocity.y < -0.000001)
        {
            animatorPlayer.SetBool("isFalling", true ); 
        }
        else
        {
            animatorPlayer.SetBool("isFalling", false);
        }
       
    }

    private void FlipSprite()
    {
        bool ifPlayerismoving = Mathf.Abs(rb2D.velocity.x) > 0;
        if (ifPlayerismoving) {
            if (rb2D.velocity.y > 0.0001) {
                animatorPlayer.SetBool("isRunning", false);
            }
            
                transform.localScale = new Vector2(Mathf.Sign(vector2Move.x), 1);
            
        }
        else
        {
            animatorPlayer.SetBool("isRunning", false);
        }
    }

    void OnJump(InputValue value)
    {
        if (playerCollider.IsTouchingLayers(LayerMask.GetMask("Ground")) && value.isPressed)
        {
            rb2D.velocity  = new Vector2(0f, jumpForce);
            animatorPlayer.SetBool("isIdling", false);
        }  
    }
    void OnMove(InputValue value)
    {
            vector2Move = value.Get<Vector2>();
    }

    void Run()
    {
            animatorPlayer.SetBool("isRunning", true);

            Vector2 playerMovement = new Vector2(vector2Move.x * playerSpeed, rb2D.velocity.y);
            rb2D.velocity = playerMovement;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        animatorPlayer.SetBool("isGrounded", true);  
    }
    void ClimbLadder()
    {
        if (playerCollider.IsTouchingLayers(LayerMask.GetMask("Ladder")))
        {
           
            if (rb2D.velocity.y < 0.0001 && rb2D.velocity.y > -0.0001)
            {
                animatorPlayer.SetBool("isClimbing",false);
                animatorPlayer.SetBool("isIdling", true);
            }
            else
            {
                animatorPlayer.SetBool("isIdling", false);
                animatorPlayer.SetBool("isClimbing", true);
            }
            rb2D.gravityScale = 0;
            Vector2 climbingLadder = new Vector2(rb2D.velocity.x, vector2Move.y * climbSpeed);
            rb2D.velocity = climbingLadder;

        }
        else
        {
            animatorPlayer.SetBool("isClimbing", false);
            rb2D.gravityScale = theCurrentGravityScale;
        }
    }

CodePudding user response:

It seems to me like the player collider is getting stuck with the tilemap collider, you can create a new physics material 2D (Assets > Create > 2D > Physics Material 2D) and set the friction to 0

Then you need to assign the material to the Rigid Body of the player, how many colliders do you have on the player?

  • Related