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 yzr_unity · Feb 18, 2020 at 06:48 PM · 2d2d game2d-platformer2d-physics

simulate gravity in a 2D game

Is there a way to apply acceleration directly to RigidBody2D.velocity? If not, is there any way to simulate gravity as a gameobject's velocity? I am really new to Unity, and I am making a 2D game where you can use direction keys to change the gravity directions. Since the game is multiplayer, using physics2d.gravity might not be applicable.

Comment
Add comment · Show 2
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 sjhalayka · Feb 18, 2020 at 09:05 PM 0
Share

What part are you stuck at?

I keep a separate position and velocity pair, and pass that into my acceleration function. Then when I'm done adjusting the position and velocity, I call game_object.transform.position = temp_position; -- I do not use the transform.velocity variable.

Here is the Runge-$$anonymous$$utta order 4 integrator:

 void proceed_rk4(ref tennis_parameters t, ref Vector3 pos, ref Vector3 vel, Vector3 ang_vel, float dt)
 {
     const float one_sixth = 1.0f / 6.0f;
     
     Vector3 k1_velocity = vel;
     Vector3 k1_acceleration = acceleration(ref t, pos, k1_velocity, ang_vel);
     Vector3 k2_velocity = vel + k1_acceleration * dt * 0.5f;
     Vector3 k2_acceleration = acceleration(ref t, pos + k1_velocity * dt * 0.5f, k2_velocity, ang_vel);
     Vector3 k3_velocity = vel + k2_acceleration * dt * 0.5f;
     Vector3 k3_acceleration = acceleration(ref t, pos + k2_velocity * dt * 0.5f, k3_velocity, ang_vel);
     Vector3 k4_velocity = vel + k3_acceleration * dt;
     Vector3 k4_acceleration = acceleration(ref t, pos + k3_velocity * dt, k4_velocity, ang_vel);
     
     vel += (k1_acceleration + (k2_acceleration + k3_acceleration) * 2.0f + k4_acceleration) * one_sixth * dt;
     pos += (k1_velocity + (k2_velocity + k3_velocity) * 2.0f + k4_velocity) * one_sixth * dt;
 }

And here is the acceleration function:

 Vector3 acceleration(ref tennis_parameters t, Vector3 pos, Vector3 vel, Vector3 ang_vel)
 {
     // $$anonymous$$agnus effect, in metres per second, per second
     // http://farside.ph.utexas.edu/$$anonymous$$ching/329/lectures/node43.html
     // http://spiff.rit.edu/richmond/baseball/traj/traj.html
     Vector3 magnus_accel = Vector3.Cross(vel, ang_vel);
     magnus_accel *= 0.5f * t.air_density * t.lift_coeff * t.ball_cross_section / t.ball_mass;
     
     // Wind and drag, in metres per second, per second
     Vector3 drag_vel = t.wind_velocity - vel;
     Vector3 drag_accel = drag_vel * drag_vel.magnitude * 0.5f * t.air_density * t.drag_coeff * t.ball_cross_section / t.ball_mass;
     
     return t.gravitational_acceleration + magnus_accel + drag_accel;
 }




avatar image sjhalayka · Feb 18, 2020 at 09:07 PM 0
Share

I forgot to mention:

 gravitational_acceleration = new Vector3(0, -9.81f, 0);

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Feb 18, 2020 at 06:50 PM

Hello.

Simulate gravity is not add a velocity to an object. Is to add a force, in this case to the rigidbody component.

https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

This is the correct way to do it, so investigate it and do some tests.

Good luck"!

Comment
Add comment · Show 2 · 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 yzr_unity · Feb 18, 2020 at 08:02 PM 0
Share

Thanks for replying, but as I said, I am using RigidBody2D, and using AddForce won't give me an acceleration. Plus, Force$$anonymous$$ode2D doesn't have Acceleration as Force$$anonymous$$ode

avatar image tormentoarmagedoom yzr_unity · Feb 19, 2020 at 12:58 AM 0
Share

Hello.

Gravity is an acceleration, Acceleration is Force*mass. If you apply a force each frame in a direction, it will do the same as the gravity.

As manual of Addforce says: "Force is applied continuously along the direction of the force vector. Specifying the Force$$anonymous$$ode mode allows the type of force to be changed to an Acceleration, Impulse or Velocity Change."

Bye.

avatar image
0

Answer by Sinnamonn · Sep 21, 2020 at 10:07 PM

It is very late for me to answer now, but I just came across the same issue, and was able to fix it Since Force = acceleration * mass, when you use AddForce, the object gets acceleration of Force/mass so if you want to give a specific amount of acceleration, (-9.81 for example), you simply have to multply the force you are sending by the mass of the rigidbody object.

Long story short.

 AddForce(rb.mass * GravityVector);

should solve the issue

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 Sinnamonn · Sep 20, 2020 at 07:18 AM

It is very late for me to answer now, but I just came across the same issue, and was able to fix it Since Force = acceleration * mass, when you use AddForce, the object gets acceleration of Force/mass so if you want to give a specific amount of acceleration, (-9.81 for example), you simply have to multply the force you are sending by the mass of the rigidbody object.

Long story short.

 AddForce(rb.mass * GravityVector);

should solve the issue

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

272 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

Related Questions

Help Me! I want to make a pixel fairy! 1 Answer

Two Polygon2D Collider do not Collide with Each Other (Solved) 2 Answers

My ground checker only works... sometimes 1 Answer

How to make slider joint immovable by the player 1 Answer

How do I stop my sprite from jumping in the air? 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