So what I’m trying to do is create a script which can be attached to some circles that are inside of some walls (gameobjects with colliders). What I want is for, at the start, the ball to choose a random rotation and move in that direction but once it hits the wall it bounces off at the appropriate ,mirrored, angle. And if it collides with other balls does the same.
The overall goal is to simulate gas particles.
Any help will be HIGHLY appreciated.
If you don’t need full control over the objects, but are okay with letting the physics engine do its thing, you can make a “Physic Material 2D” (using the create menu in your Assets). Set its bounciness to 1 and assign it to your object’s collider. To start off in a random direction, use Random.insideUnitCircle, normalize the returned vector and multiply it with the desired speed, then set that vector as the rigidbody’s velocity:
rigidbody2d.velocity = Random.insideUnitCircle.normalized * speed;
Do all that in Start and put that script on the object to send it flying.
If you want more control over the objects (like making sure their speed is constant), you’d have to extend that script. You could save the direction in a Vector2 field and set it as the velocity in each FixedUpdate. You could also use OnCollisionEnter2D to handle the bounce calculation on your own. Vector2.Reflect would help you there.