unity3d - Trying to get a game object/rigidbody to bounce across the screen endlessly -
i trying copy bubble screen saver behavior windows in unity there bubbles moving across screen , bouncing when collides screen edge and/or each other.
so far here's have. have game object prefab bubble circle collider 2d , rigidbody2d , custom script wrote. in rigidbody component, have gravity scale set 0 since gravity not involved here think.
i have 4 empty game objects left/right/top/bottom screen edge box collider 2d
in script, have:
public rigidbody2d thisbody; // use initialization void start () { thisbody.velocity = new vector2(0.5f, 0.5f); } this gives bubble game object starting velocity when spawns.
here's problem i'm facing right now: object spawns , moves expected. when hits screen edge, not rebound back. example, when hits right edge of screen, continues move upwards instead of bouncing off of it.
i know continues move upwards because of initial velocity wrote. based on documentations available online, know can use oncollisionenter2d() method when bubble touches game object collider.
but not know how calculate velocity make bounce back. can provide code snippet on how handle velocity when collides screen edge make bounce back? or if there easier of doing this, please let me know i'm still new stuff. maybe there's way accomplish without using script @ , let physics engine handle movement?
create new physics material , add sphere want have bounce adjust bounciness 1 , set bounce combine max. set cubes around screen collide , use in sphere script. should on right path.
public rigidbody thisbody; private float addvelocity = 400f; void start() { thisbody.addforce(new vector3(addvelocity, addvelocity, 0)); } //you can set collision check , add spin or void oncollisionenter(collision other) { if(other.gameobject) { thisbody.addforce(0, 0, 0);//adjust force } }
Comments
Post a Comment