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 kreso · Mar 22, 2014 at 08:57 AM · physicsballbouncing

Simple ball bounce (like pang/bubble trouble)

Hello,

I am trying to build a sequel to my game in Unity (http://www.rebubbled.com). If you feel like looking at the first level of the game you will understand what I am after.

In writing - I am trying to achieve the effect of ball (in a perfect bounce world) bouncing always same velocity on X after collision, and adjusting Y velocity depending on its Y position if needed so it always jumps to predetermined height (if it is jumping upwards after collision - if not, let it just use gravity and keep falling 'naturally').

2D is perfect, 3D works too.

alt text

After trying and failing many times over I figured I need some help.

Here is a snippet of calculating the new velocity.y (in case ball is moving upwards, after collision), if that helps..

      float relativePositionY = JUMP_HEIGHT - currentPosition.y;
      float newYVelocity = Mathf.Sqrt(2 * relativePositionY * Physics2D.gravity.y);


Thank you in advance!

Kreso

untitled-1.png (25.9 kB)
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

3 Replies

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

Answer by kreso · Jul 28, 2014 at 07:53 AM

To answer my own question:

This is very easy to do. First mistake I did was use physics materials. No need for those here. Other than that - just using hit normal did the trick.

 private float velX = 1.9f;//horizontal speed of ball
 private Vector2 Vel_in;//save incoming velocity here

 private float startY;//max jump height (every time ball hits floor it will calculate force needed to reach this height).


 void FixedUpdate()
 {
     Vel_in = rigidbody2D.velocity;
  }

  void OnCollisionEnter2D(Collision2D coll)
  {

     ContactPoint2D hit = coll.contacts[0]; //(for debug only) the first contact is enough
     Vector3 Vel_out = Vector3.Reflect(Vel_in, hit.normal);

     if(hit.normal.x < 0)
         rigidbody2D.velocity = new Vector2( - velX, Vel_in.y);
     else if(hit.normal.x > 0)
         rigidbody2D.velocity = new Vector2( velX, Vel_in.y);
     else
         rigidbody2D.velocity = new Vector2( velX*(Vel_in.x/Mathf.Abs(Vel_in.x)), rigidbody2D.velocity.y);

     if(hit.normal.y < 0)
     {
         if (Mathf.Abs(Vel_in.y) < 1)
             rigidbody2D.velocity = new Vector2( rigidbody2D.velocity.x, -1);
         else
             rigidbody2D.velocity = new Vector2( rigidbody2D.velocity.x, -Mathf.Abs(Vel_in.y));

     } else if(hit.normal.y > 0)
     {     //jumping up, calculate how much force is needed to jump to certain height (startY)
         float relPos = transform.position.y - startY;
         if(relPos > 0f)
             relPos = 0f;
         float newYVel = Mathf.Sqrt(2 * relPos * Physics2D.gravity.y * rigidbody2D.gravityScale);
         if (newYVel == 0) newYVel = 1f;
         rigidbody2D.velocity = new Vector2( rigidbody2D.velocity.x, newYVel);
     }


     //save now, because sometimes collision happens before next FixedUpdate tick
     Vel_in = rigidbody2D.velocity;
 }
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
avatar image
0

Answer by Gordinho · Apr 24, 2016 at 04:24 PM

Hello, I played your pang clone and it looks cool. Do you think you could share the code to make the spheres bounce and move?. I tried the code above but my sphere stays jumping from one side to the other and doesn't move anywhere else, just stays jumping in two spots. Thanks.

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
avatar image
0

Answer by Gnomemann · Jun 15, 2018 at 09:46 PM

Seems like you get an error at

rigidbody2D.velocity = new Vector2( velX*(Vel_in.x/Mathf.Abs(Vel_in.x)), rigidbody2D.velocity.y);

At least I did and I can't seem to figure out how to fix it. It returns a NaN so I assume it's a division by 0.

Edit: I made the calculation, it multiplies 1.9f with 0 / 0 which returns a NaN.

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

22 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

Related Questions

Bouncing ball stops when hitting edge of platform 2 Answers

Make a rolling ball always on ground without falling when reaching the edges of the map 1 Answer

The Physics of Ball Spin in Tennis, Topspin and Backspin 3 Answers

What is the best way to move a paddle accurately in a circle? 2 Answers

moving and jumping ball using physics 2 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