![]() ![]() ![]() Can anyone provide some code snippet on how to handle the velocity when it collides with the screen edge to make it bounce back? Or if there is an easier of doing this, please let me know as I'm still fairly new to this stuff. Based on the documentations available online, I know that I can use the OnCollisionEnter2D() method when the bubble touches another game object with a collider.īut I do not know how to calculate the velocity to make it bounce back. I know it continues to move upwards because of the initial velocity I wrote. For example, when it hits the right edge of the screen, it just continues to move upwards instead of bouncing off of it. ![]() But when it hits the screen edge, it does not rebound back. Here's the problem I'm facing right now: The object spawns and moves as expected. This basically gives the bubble game object a starting velocity when it spawns. ThisBody.velocity = new Vector2(0.5f, 0.5f) In my script, I have: public Rigidbody2D thisBody I also have 4 empty game objects for the left/right/top/bottom screen edge with a Box Collider 2D A GameObject’s functionality is defined by the Components attached to it. In the Rigidbody component, I have Gravity Scale set to 0 since gravity is not really involved here I think. Introduction to collision Unity handles collision between GameObjects with colliders, which attach to GameObjects and define the shape of a GameObject The fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. I have a game object prefab for the bubble with a Circle Collider 2D and Rigidbody2D and a custom script that I wrote. I am trying to copy the bubble screen saver behavior from Windows in Unity where there are bubbles moving across the screen and bouncing when it collides with the screen edge and/or with each other. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |