Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by potterman28wxcv · Mar 03, 2019 at 08:37 PM · rigidbody2dcollision2d

Arkanoid in Unity - Dealing with collisions between ball and paddle

Hello !

+++

I am starting out in Unity, and for a first project I would like to make a simple Arkanoid clone in 2D.

+++

I have walls with 2D box colliders that define the limits of where the paddle can move (as well as allowing the ball to bounce off it), I have a ball with a specific 2D physics material with {friction = 0; bounciness=1} + 2Dcirclecollider + 2Drigidbody (dynamic), then I have a paddle with another 2Drigidbody (dynamic with fixed Y component and fixed rotation on Z) and a 2D box collider.

+++

I am moving the paddle directly with the mouse, setting up an adequate velocity vector on each PhysicsUpdate(). xmax and xmin variables are set up "by hand" in the editor, and rb is the rigidbody, extracted in the Start() function by a GetComponent.

     void FixedUpdate()
     {
         float p = Input.mousePosition.x;
         float new_x = (xmax - xmin) / Screen.width * p + xmin;
         rb.velocity = new Vector2(new_x - rb.position.x, 0) / Time.deltaTime;
     }

The first problem I had to solve was how to make it so that the paddle does not clip through the walls - i've set the paddle to be a dynamic rigidbody, and also set collision detection to continuous to prevent "teleporting" collision issues.

+++

Another problem I had to deal with, was that the collisions were making the ball lose energy even though bounciness was set to 1 on the ball. I saw a thread with people having similar issues, and adapted a code from that thread to apply a repulsing force everytime the ball colliders, such that the energy stays the same

     private void OnCollisionEnter2D(Collision2D col)
     {
         // Normal
         Vector2 N = col.contacts[0].normal;
 
         //Direction
         Vector2 V = rb.velocity;
 
         // Reflection
         Vector2 R = Vector2.Reflect(V, N);
 
         rb.AddForce(rb.mass * (R - V));
     }

Now the ball wasn't losing any energy upon hitting walls, but it was still losing some energy when hitting the paddle. I have learnt that it is because the energy was going from the ball to the paddle, but the paddle has fixed Y position so it would just sink the energy. In order to tackle that, I set up the mass of the paddle to be very big, this seems to solve the issue.

+++

However, I am encountering a fourth problem, and I have no idea how to deal with this one. If I move the paddle too fast, and it hit the ball, the paddle will transfer its energy to the ball - the paddle has an absurdly high mass so the ball gets a super high velocity.

+++

Now that i have exposed my problem, my question is : is there a way for me to desactivate the transfer of energy from the paddle to the ball? Given that i already have a code OnCollisionEnter that applies a force that is supposed to do the job, I don't want Unity to apply another force on top of that.

+++

I saw that there were ways to deactivate collisions between two layers, but this is not what I want - since if i desactivate the collision between the ball and the paddle I will also desactivate the callbacks to OnCollisionEnter and the ball won't bounce at all. Instead, I would like to desactivate the interaction between the ball's rigidbody and the paddle's rigidbody, but just that particular interaction, without affecting the collision code.

+++

Thanks for your answers! And please excuse my weird +++ typing - this is my poor man's attempt at trying to write paragraphs in answers.unity.com

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision Detection on Rb with Interpolation and Rb with Extrapolation does not work the same way. 0 Answers

Stop Force on Collision 0 Answers

a chain of 2d collision problem ( object going trough other ) 0 Answers

Destroying object on any collision 1 Answer

Pre-solve Collision 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges