- Home /
2D bounce not working
Hi there, I cannot seem to get game objects to bounce off walls the way I want them too. The walls have a boxcollider2d and BouncyBox physics material. The Objects (blue Squares) have a boxcollider2d and rigidbody2d on them, gravity set to 0.
The objects are spawned at random from the top of the screens with a movement script attached to them.
rb.velocity = new Vector2 (1,-y);
currently this is what happens

Whereas I need each box to move more freely bouncing from wall to wall before going off the bottom of the screen. Like the this,

Answer by tebandesade · Mar 04, 2015 at 08:57 PM
Hi! Is the movement script an Updated function? Or just want it starting? Y tried with this script, it just gets an object between to edge colliders, cero gravity to bounce between the two colliders.
public GameObject c;
private Rigidbody2D test;
// Use this for initialization
void Start ()
{
test = c.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
test.AddForce (Vector2.right);
}
And also changed the bouncy configuration of the material. To do this just double click the material and input some value in bounciness.
It's in FixedUpdate at the moment. I tried you script, the problem is that now it only goes directly right, not down. I'm thinking I need something that will change the direction of the cube upon collision so that it heads in the opposite direction until it collides with the other wall and so on.
Just not sure how to do this.
It shouldn't do that if the physics engine is working properly. Double check the mass of the ball, and the bounciness of the material. Also, try beginning the the force with .AddForce (new Vector2 (5, -5)); This will create a new vector that will add force the right but also down, so a diagonal will be formed
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Avoid character bouncing off of obstacles 0 Answers
Distribute terrain in zones 3 Answers
Avoid character bouncing off of obstacles 1 Answer
Multiple Cars not working 1 Answer