- Home /
How to make character stop at wall?
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));
}
}
Answer by Phililppikos · Jan 09 at 03:50 AM
I know sometimes colliders can't sense higher speed collisions; that is if the collider is an edge-type or some other non-primitive collider. Also of course check to make sure the collider isn't acting as trigger. Some else probably knows more though
Your answer
![](https://koobas.hobune.stream/wayback/20220613051433im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 2D Platformer Enemy follow Player on X-axis only, c# 1 Answer
1 Button Alternating Between 2 onClick Functions 1 Answer
2D Platformer Jump while Running Android 1 Answer
Player pass each other laterally but can step on each other 0 Answers
Trying to freeze multiple gameObjects at the same time 1 Answer