Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by Riki9811 · Aug 16, 2020 at 10:52 AM · 2drigidbody2dphysics2dbounceenergy

2D Bounce gains energy each time

I want to create a bouncy ball in unity 2D that will bounce infinitely without losing/gaining energy with each bounce. My setup is: a ball sprite with circle collider and rigidbody2D set to dynamic, and a floor sprite with a box collider.

I have already found a lot of questions like this one and the answer is always:
1. Create a Physics2D material with friction = 0 and bounciness = 1.
2. Set the ball's rigidBody2D to have 0 drag ad attach the new physics material to it.
3. Set the rigidbody2D to have discrete collision detection.

Problem is I already have done all of the above but the ball bounces higher and higher every time. Is it a bug in unity 2019.4.2f1 or am I missing something?

Comment
Add comment · Show 3
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
avatar image davidcox70 · Aug 16, 2020 at 05:15 PM 1
Share

This is a hunch, but try setting the bounciness of your physics material to match the value of gravity in Project Settings>Physics. So if your gravity is -9.81, then try your bounciness at 0.981.

If that works, then I guess bounciness is an absolute value, rather than a relative value (i.e. bounce back at 100% of the inco$$anonymous$$g speed).

avatar image Riki9811 davidcox70 · Aug 17, 2020 at 08:09 AM 0
Share

Just tried it, but that's not the solution unfortunately. I believe bounciness is a relative value since 0.99 gives me a bounce that loses very little energy so the ball will take a lot of time to stop, but eventually will. But then why would 1 not work? Still looking for a solution...

avatar image davidcox70 Riki9811 · Aug 17, 2020 at 08:13 AM 1
Share

I agree that bounciness of 1 sounds like it should give a perfect bounce when all drag and friction is set to 0. But when I tried it, it gave a significant boost as you pointed out. Another option might be to detect the collision and set the velocity of the rigidbody to be the reflection of the pre-collision velocity vector.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Riki9811 · Aug 17, 2020 at 09:08 AM

I managed to get what I wanted (it's not perfect elastic bounce but for my project will do just fine). Thanks to @davidcox70 for the tip.
Here is my setup: I removed the physics material from the rigidbody and left it to null. All the other settings for the rigidbody remain untouched. Then I created a script and attached it to the ball. The script has:

 [RequireComponent(typeof(Rigidbody2D))]
 public class BallController : MonoBehaviour
 {
     private Vector2 preHitVelocity;
     private Rigidbody2D rb;
 
     void Start()
     {
         rb = GetComponent<Rigidbody2D>();
     }
 
     void Update()
     {
         preHitVelocity = rb.velocity;
     }
     
     void OnCollisionStay2D(Collision2D other)
     {
         Vector2 avgNormal = Vector2.zero;
         for (int i = 0; i < other.contactCount; i++)
         {
             avgNormal += other.GetContact(i).normal;
         }
         avgNormal /= other.contactCount;
         rb.velocity = Vector2.Reflect(preHitVelocity, avgNormal);
     }
 }
 


This works by caching the velocity of the ball each frame in the Update() method.
Then in the OnCollisionStay2D() method (I initially used OnCollisionEnter2D() but it didn't work for me don't know why) I calculate the average normal of the collision and then I use it with the preHitVelocity to reflect the velocity of the rigidbody.

This gives me an almost perfect bounce. Sometimes the ball will bounce slightly lower then it should, but on the next bounce it will go back up to the right height. Tested it a bunch and seems to be a stable solution. I'm not sure if it's the optimal one though...

Comment
Add comment · Share
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

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

284 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 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

Enemy bounce from screen edges 0 Answers

GameObject not detecting collision with floor 0 Answers

2D rigidbody behaves strangely with a negative scale 0 Answers

physics Material 2D not working !!!! 1 Answer

Many 2D rigidbodies optimization 1 Answer


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