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 /
avatar image
0
Question by ilyamez · Jan 22, 2018 at 01:38 AM · physicsrigidbodyphysics2dbouncereflect

How to calculate a RigidBody2D's bounce from a collider?

Ok, so after trying to figure it out by myself for the last three days I'm finally ready for some help :P.

I'm building circle pong like game and i've been trying to implement a mechanic where time slows down to almost a halt (0.0001) for everything ingame except the player. During this slowmotion mode i want to show the trajectory that the ball will take after time is back to normal, like this:
alt text

But when i'm using this code to calculate the trajectory:

 LayerMask playerMask = LayerMask.GetMask("Player");
         LayerMask enemyMask = LayerMask.GetMask("Enemy");
 
         while (active){
             Vector2 dir = this.transform.position - ball.transform.position;
             RaycastHit2D hit = Physics2D.CircleCast(ball.transform.position, ball.GetComponent<CircleCollider2D>().radius, dir, Mathf.Infinity, playerMask | enemyMask);
                 if (hit.point != null) { 
                 lr.SetPosition(0, ball.transform.position);
                 lr.SetPosition(1, hit.point);
                 Vector2 ballpos2d = new Vector2(ball.transform.position.x, ball.transform.position.y);
                 Vector2 ballTrajectory = Vector2.Reflect( hit.point - ballpos2d, hit.normal);
                 lr.SetPosition(2, ballTrajectory * 50);
                 }
             yield return null;
         }
           
         }

The ball is set to dynamic rigidBody2D with no mass or drag and physics material2D with 0.3 bounce and no friction.The problem is that the ball off by some degree, like this:

alt text

So is there any way to calculate how physics based object would bounce off a collider?

2.jpg (104.7 kB)
2.png (496.2 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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by UnityVeris · Jan 22, 2018 at 02:32 AM

For a simple style like that I would encourage you to write your own physics from scratch, you would have way more control and then this kind of operation would become trivial.

Barring that, I don't believe it is possible to force a physics Update, because physics are based on time, so if no time has passed then nothing will happen, but you want an answer each frame.

Another option is to (with some trial and error) is to use the velocity of the ball (rigidbody.velocity) to figure out where the ball is going and how it would bounce when it hits the next object along it's path.

Comment
Add comment · Show 1 · 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 ilyamez · Jan 22, 2018 at 05:03 PM 0
Share

I tried your suggestion with using trial and error, but since physics calculations are not open source and I dont see the correlation between my projections and the actual bounce, had to drop this angle.

I also tried switching to custom physics but there seems to be a problem with collision detection when i do. I'm pretty sure it happens since my project is not scaled correctly..

Right now, I'm trying setting the ball to kinematic after the slow$$anonymous$$otion ends and then restoring it to dynamic and setting it's velocity manually on it's first collision.

Thank for the help btw!!

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

148 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

Related Questions

Problem with Ball Bouncing after Collision 2 Answers

How to make physics relative to moving parent 1 Answer

Rigidbody2D: Follow other Rigidbody2D strictly 1 Answer

Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer

Rigidbody bounce to a place without physics material. 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