Working on a 2d platformer, and am using rigidbody2d along with box colliders. I’ve never had a problem with the colliders until today. Ground collision works perfectly fine. However, when I try to walk into a wall, my character walks right through instead of stopping. Help?
float horizontal;
float spd;
public float jspd;
Vector3 movement;
public Rigidbody2D rb;
bool grounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
public bool touchingFront;
public Transform frontCheck;
public bool wallSliding;
public float wspd;
void Start()
{
spd = 5f;
jspd = 5f;
grounded = false;
}
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
if(horizontal == -1)
{
transform.rotation = Quaternion.Euler(0, 180f, 0);
}
if(horizontal == 1)
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
if ((Input.GetButtonDown("Jump")) && (grounded))
{
rb.velocity = Vector2.up * jspd;
}
movement = new Vector3(horizontal * spd, 0, 0);
}
void FixedUpdate()
{
transform.position += movement * Time.fixedDeltaTime;
grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
touchingFront = Physics2D.OverlapCircle(frontCheck.position, checkRadius, whatIsGround);
if (touchingFront && !grounded && horizontal != 0)
{
wallSliding = true;
}
else
{
wallSliding = false;
}
if (wallSliding)
{
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wspd, float.MaxValue));
}
}