Player collision is not really working well

Basically when our player hits a wall he gets stuck and the only way to unstuck him is to walk the direct oposite way of the wall. for instance if you hit a wall on the right you cant walk forward backwards or right (right is intentional) the only way to get out is to walk left.

this is our code for the colission check.

 private void OnCollisionStay(Collision collision)
{
    // Determine if we can jump
    var normal = collision.contacts[0].normal;
    if (normal.y > 0)
    {
        // Hit Bottom
        moveAction.ableToJump = true;
        moveAction.ableToMoveBackward = moveAction.ableToMoveForward = moveAction.ableToMoveLeft =     moveAction.ableToMoveRight = true;
    }

    if (normal.x > 0)
    {
        Debug.Log("Object you hit is to your left");
        //Hit Left
        moveAction.ableToMoveLeft = false;
    }
    else if (normal.x < 0)
    {
        Debug.Log("Object you hit is to your right");
        //Hit Right
        moveAction.ableToMoveRight = false;
    }
    else if (normal.z < 0)
    {
        Debug.Log("Object you hit is in front of you");
        //Hit Front
        moveAction.ableToMoveForward = false;
    }
    else if (normal.z > 0)
    {
        Debug.Log("Object you hit is behind you");
        //Hit Back
        moveAction.ableToMoveBackward = false;
    }
}

Hello, can you share the moveAction code?

But the problem isnt there since the Debug.Log doesnt show the right answers. they basically say left and right at the same time when i walk against something in the front etc…

Here is the moveAction code:

public override void OnEnter()
{
base.OnEnter();
idleTime = totalIdleTime;
}

public override void OnUpdate()
{
    // Determine wether we are walking or sprinting
    if (Input.GetAxis("Sprint") <= 0)
    {
        // Walking
        usedVelocity = velocity;
    }
    else
    {
        // Sprinting
        usedVelocity = sprintVelocity;
    }

    //Animations
    if ((Input.GetAxis("Horizontal") == 0) && (Input.GetAxis("Vertical") == 0))
    {
        
    }



    // Walking or Sprinting
    float moveLeftRight = Input.GetAxis("Horizontal") * usedVelocity * Time.deltaTime;
    float moveForwardBackward = Input.GetAxis("Vertical") * usedVelocity * Time.deltaTime;

    // Prevent sticking on wall after jumping
    if (!ableToMoveLeft && moveLeftRight < 0)
    {
        moveLeftRight = 0;
    }
    if (!ableToMoveRight && moveLeftRight > 0)
    {
        moveLeftRight = 0;
    }
    if (!ableToMoveForward && moveForwardBackward > 0)
    {
        moveForwardBackward = 0;
    }
    if (!ableToMoveBackward && moveForwardBackward < 0)
    {
        moveForwardBackward = 0;
    }

    // Jump
    if (Input.GetButtonDown("Jump") && ableToJump)
    {
        PlayerRB.velocity += Vector3.up * jumpSpeed;
        ableToJump = false;
    }

    PlayerRB.velocity = new Vector3(moveLeftRight, PlayerRB.velocity.y, moveForwardBackward);

    if (PlayerRB.velocity == Vector3.zero)
    {
        idleTime -= Time.deltaTime;
    }
    else
    {
        idleTime = totalIdleTime;
    }
    if (idleTime <= 0)
    {
        Finish();
    }
}
1 Like

I remember doing some collision testing when I was making a platformer and used to get stuck in walls, and one thing that I eventually found was that when a collision occurs and your going fast enough, there is an ammount of penetration with the two colliders and no ammount of moving will sort them out.

the way I ended up fixing that was once a collision occurs, move the player outside of the collision state with the wall.

so I would move the player back, away from the wall + a little skin gap too.

might be worth a shot.

2 Likes

ty fixed it

1 Like

Privacy & Terms