I’m working on a 2D platformer without using physics. Sometimes happens that my character, moving to the right, gets stuck inside a wall. My code is:
//horizontalSpeed is > 0
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right, horizontalSpeed, LayerMask.GetMask("Walls"));
if (hit)
{
float margin = 0.01f;
transform.position = new Vector2(hit.point.x + boundingBox.offset.x - boundingBox.bounds.extents.x - margin, transform.position.y);
horizontalSpeed = 0;
}
else
transform.position = new Vector2(transform.position.x + horizontalSpeed, transform.position.y);
Could this problem be caused by hit.point to be inside the Wall’s box collider instead of being on the edge?