- Home /
Question by
YetiX · Jul 05, 2021 at 03:50 PM ·
unity 2dbeginnercollider2ddirection
[Game like Pong] Trying to change direction with a boxcollider
Hi, I'm trying to get into making video games and I've started recreating pong. To create the top and bottom wall I have made a box collider and to try to change the direction I have used this code
public movBall ball1;
// Start is called before the first frame update
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.transform.CompareTag("ball"))
{
if(ball1.movx == 1f && ball1.movy == 1f)
{
pelota.movy = -1f;
}
else if(ball1.movx == -1f && ball1.movy == 1f)
{
pelota.movy = -1f;
}
else if (ball1.movx == -1f && ball1.movy == -1f)
{
ball1.movy = 1f;
}
else if (ball1.movx == 1f && ball1.movy == -1f)
{
ball1.movy = 1f;
}
}
}
MovBall is the class where all the code for the ball to move is, where movx is the movement in x-axis and movy is the movement in y-axis. The problem is that when I start the game it doesn't work and the ball just goes through the box collider.
Comment
Aren’t you using Box Collider instead of Box Collider 2D for your walls?
Make sure you have a rigidbody on your ball or paddle and a 2d collider on both and that they are not set to isTrigger.