Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Stuck in wall when jumping

Discussion in '2D' started by retrj986, Mar 28, 2020.

  1. retrj986

    retrj986

    Joined:
    Feb 7, 2020
    Posts:
    8
    Hello,
    I have a problem: when i jump my character can suddenly stuck in a wall
    I tryed creating a sleepy physicsMaterial2D (friction to zero, diffirent values of bounsness),
    but this isn't working...

    Wall is just a couple of blocks(Terraria like game)

    Here is my code:


    void Update()
    {
    if (Input.GetKey(KeyCode.D))
    {
    player.velocity += Vector2.Lerp(new Vector2(player.transform.position.x, 0), new Vector2(speed, 0), Time.deltaTime*0.3f);
    if (player.velocity.x >= speed)
    player.velocity = new Vector2(speed, player.velocity.y);
    if(!right)
    {
    player.transform.localScale = new Vector3(1,1);
    right = !right;
    }
    }
    else if (Input.GetKey(KeyCode.A))
    {
    player.velocity -= Vector2.Lerp(new Vector2(player.transform.position.x, 0), new Vector2(speed, 0), Time.deltaTime * 0.3f);
    if (player.velocity.x <= speed)
    player.velocity = new Vector2(-speed, player.velocity.y);
    if (right)
    {
    player.transform.localScale = new Vector3(-1, 1);
    right = !right;
    }
    }
    else
    player.velocity = new Vector2(0, player.velocity.y);

    PlayerGlobalSpecs.jumpReload -= Time.deltaTime;

    if(Input.GetKey(KeyCode.Space))
    {

    if(PlayerGlobalSpecs.onGround && PlayerGlobalSpecs.jumpCount < PlayerGlobalSpecs.canJumpTimes && PlayerGlobalSpecs.jumpReload <= 0f)
    {
    player.velocity = new Vector2(player.velocity.x, PlayerGlobalSpecs.jumpPower);
    PlayerGlobalSpecs.jumpCount = 0;
    PlayerGlobalSpecs.jumpReload = PlayerGlobalSpecs.jumpReloadDefolt;
    PlayerGlobalSpecs.onGround = false;
    }
    }

    }
     
  2. retrj986

    retrj986

    Joined:
    Feb 7, 2020
    Posts:
    8
    Fixed by using raycast and smart disabling of movement