How can i make an object to change directions after hitting a wall?
I have a 2d game where a ball goes from left to right. I want the ball to automatically change directions after hitting a wall but I can't find a way to do so. Ive try to use the on collision enter function but it is just not working. I just need to find a way to change a variable (the one that controls the direction) after the ball hits the wall. Everything is in 2d. The ball is a circle and the wall is a square. Both have collides and the ball has a rigidbody. Thanks in advance.
Answer by gustavim15 · Jun 25, 2016 at 03:13 PM
Hello @raaul.09.... This videos show's exactly what you want: Change direction Hitting Wall
Hope it helps you... :D
Answer by NitroGain · Jun 25, 2016 at 10:31 AM
This should work:
void OnCollisionEnter2D(Collision2D col)
{
if(coll.gameObject.tag == "Wall")
{
MovementSpeed = -MovementSpeed;
}
}
When I do this, it changes back (to positive) and gets stuck