Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 Naggil · Dec 31, 2019 at 01:38 AM · rigidbody2dphysics2dbouncebouncingbounciness

How to change bouncing direction

I have a boucing ball with rigidbody2d and a physics material with bounciness set to 1 how can make the ball go faster up/down when boucing between 90 and 80 degress to avoid being stuck in the tunnel or going up/down too slow? (also need the same thing when bouncing at the horizontal) alt text

sem-titulo.png (5.7 kB)
Comment
Add comment · Show 1
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 IggyZuk · Dec 31, 2019 at 03:00 AM 0
Share

You can use the dot product on the ball’s direction vector and the normal vector of the wall to find the angle between them. When the angle is almost the same apply a different force to the ball.

2 Replies

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

Answer by Twinklier · Dec 31, 2019 at 03:03 AM

Bouncing is a physics stuff, and I doubt there is any settings menu dedicated to bouncing balls. Better implement something!

So my idea is that when the ball hits the wall, instead of just bouncing back, a random force is added backwards with a slight offset. We gunna need a script attached to this ball.

First obvious one is collision detect

 void OnCollisionEnter(Collision collision)
 {
     GetComponent<RigidBody>.AddForce(-collision.contacts[0].normal + 
         new Vector2(Random.Range(-1, 1), Random.Range(-1, 1)));
 }

This chunk of code gets the first contact point, look at the direction it's being hit, and addforce to it at the opposite direction. To spice things up, some random vector is added to give it more...UMPH of a bouncy ball. You can change that random range thing to anything you wish really.

This should do it. Cheers!

Edit: mispelled the code there a bit

Comment
Add comment · Show 3 · 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 Naggil · Dec 31, 2019 at 03:48 PM 0
Share

take this image as example its not my game but my game is similar!alt text if I let the ball always bounce to a random direction it wont be possible to aim and control the ball direction also it can very often slow down the ball speed or stop the ball completely or speed it up too much the problem is that in my game when the ball hit the corners of any object sometimes it bounces back at 90 degrees or near it, then the ball wont move anymore or move too slow like the X image in my question

avatar image Twinklier Naggil · Jan 01, 2020 at 10:14 AM 0
Share

Then have it only randomize the bouncing direction when it approaches the 80 - 90 degree threshold.

avatar image Naggil · Jan 03, 2020 at 01:47 AM 0
Share

I used this and the other answer from @$$anonymous$$arioooo and made some checks so the ball wont go towards the same wall and now its working

avatar image
0

Answer by Marioooo · Dec 31, 2019 at 11:46 PM

I got into this problem once... What I did is to add a bool variable that sets true once the ball hits a wall.

Then I used late update. Late update occurs after all physics have been calculated. So now I know where the ball is going, I can use a if statement in order to change the direction.

 Private void lateUpdate() 
 {
     If (checkDir)
 {
     // IF RIGIDBODY DIRECTION IS HIGHER THAN 80 DEGREES AND LOWER THAN 110 
     float currentVelocity = Rigidbody2D.velocity.magnitude;
 
     // CALCULATE NEW DIRECTION
     Rigidbody2D.velocity = newDirection.normalized * currentVelocity;
 
 }
 }

You can use fixed vector2D values to set the new direction. Hope this helps!

I wrote this in my phone so don't just copy and paste the code.

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 Marioooo · Dec 31, 2019 at 11:50 PM 0
Share

Don't forget to set the checkDir to false and when you calculate the new direction check if you where moving upwards or downwards and use for x velocity $$anonymous$$athf.Abs so you don't have to worry if the ball is going to the left or the right.

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

125 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

Related Questions

2D bouncing ball not bouncing correctly 0 Answers

Unity 2D - Why does velocity causes the ball to change direction everytime it hits something? 1 Answer

2D Polygon Collider Ball with bounce material not stay completely still 0 Answers

2D Bounce gains energy each time 1 Answer

rigidbody2D.MovePosition() Can't Found 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