Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jammer3000 · Aug 27, 2014 at 08:00 PM · javascriptphysicsbounce

Realistic Bouncing Effect

I have a 2D project and am trying to make a nice smooth bouncing effect. I have a ball surrounded by walls and every 3 seconds adds a force to it and I also have a 2D physics material that is bouncy. But it doesn't look natural and smooth at all? The code below is what I am doing now to get this effect. Any suggestions on how to do this properly?

Code

 function FixedUpdate () 
 {
     AddForcesOverTime();
 }

 
 function AddForcesOverTime ()
 {
     yield WaitForSeconds(3);
     rigidbody2D.AddForce (Vector2.right * 10);
     yield WaitForSeconds(3);
     rigidbody2D.AddForce (Vector2.up * 10);
     yield WaitForSeconds(3);
     rigidbody2D.AddForce (Vector2.right * -10);
     yield WaitForSeconds(3);
     rigidbody2D.AddForce (Vector2.up * -10);
 }

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 AlwaysSunny · Aug 27, 2014 at 08:04 PM 0
Share

For one thing, you're calling this function every FixedUpdate() Unity - $$anonymous$$anual: Coroutines

1 Reply

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

Answer by AlwaysSunny · Aug 27, 2014 at 11:19 PM

@kacyesp - This answer is misleading.

Applying forces to rigidbodies should always be done during FixedUpdate() - if you're using co-routines to achieve some specific time interval, you should yield return WaitForFixedUpdate() prior to applying a force.

Multiplying values by Time.deltaTime to achieve framerate-indepentent reactions is wise in every OTHER scenario. That is, calculations which DON'T take place during FixedUpdate(), which is framerate-independent by its nature.

To clarify the last point, according to Unity's physics system, a unit is equivalent to one meter for all physics calculations.

@Jammer3000 Regarding multiplying values by Time.deltaTime:

This is done during any non-fixed-timestep loop like Update() or LateUpdate() to any value whose purpose is related to changing values over a specific interval of time. This is because each update or "frame" can take different amounts of time, and failing to multiply by Time.deltaTime will mean your variable will behave differently depending on the framerate. This is (almost) always bad.

If you were moving something during Update(), without rigidbodies or the physics system, you'd want to multiply the desired speed by Time.deltaTime like this...

 somePosition.x += speed * Time.deltaTime;

...to ensure the correct distance per second is achieved regardless of framerate. That goes for any value which needs to change at a specific rate-per-second in any update loop except FixedUpdate()

The important feature of FixedUpdate() is that the interval between each frame is always the same amount of time, regardless of framerate. Applying forces should be done in FixedUpdate because the physics simulation updates in tandem with FixedUpdate.

If you want an object to bounce off other colliders:

Create a physics material with a high bounce value, no friction value, making sure to set the bounce multiplier to "maximum" and friction multiplier to "minimum" then assign this material to your bouncy object's collider.

Also ensure there's no drag on the rigidbody component of this bouncing object.

Apply a force one time, (at least, not every frame) and it'll go on bouncing for a good long while. (until floating point imprecision causes the velocity to decay)

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 Kiwasi · Aug 28, 2014 at 04:44 AM 0
Share

This is a considerably better solution then $$anonymous$$e, have removed $$anonymous$$e.

If you wanted to get around the floating point problem you could reset the velocity to a standard magnitude at some fixed interval. But if you need this to run for long enough that floating point precision causes errors you should probably consider using deter$$anonymous$$istic physics.

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

25 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

Related Questions

Show "Score:5" on my screen, how? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Faux Gravity Prolem? #2 2 Answers

Time.timescale cause extreme lag? 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