• 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
Question by fulcanmal · Nov 18, 2012 at 07:37 PM · physicsrigidbodygravityforce

Replicate physics.gravity with custom code.

I'm working on a kart game, and I want to be able to play with the gravity. I have a simple box with cylinders fitted with wheel colliders. I have turned off the 'useGravity' and tried various methods to apply custom gravity, such as rigidbody.AddForce, as well as a ConstantForce component. First I set them all to the same value as the default physics.gravity, then I tried other numbers. When using this custom gravity the kart does not respond well at all: bumping, sliding, turning in odd directions.

Here is one example the relevant code:

 function Update(){
 ApplyGravity();
 }
 
 function ApplyGravity(){    
 rigidbody.AddForce(-Vector3.up*Physics.gravity.magnitude);
 }
Comment
Deeweext

People who like this

1 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 fulcanmal · Nov 18, 2012 at 07:40 PM 0
Share

Hmm. I just read on the Physics Manager document that the default gravity setting is meters per second. I need to look up a way to translate this to AddForce.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by sparkzbarca · Nov 19, 2012 at 06:47 AM

gravity is per second. It should be applied during fixedupdate.

Your applying 9.8 meters of force every whatever. Whatever is an unknown. update runs as many times as it possibly can during the game. Slow comps run less often for example.

FixedUpdate runs BY DEFAULT 50 times per second.

if you want 9.8 meters per second then in fixed update run it but applyGravity should be

(-vector3.up * 9.8 ) / 50

Roughly

Comment
MrVerdoux
Deeweext

People who like this

0 Show 5 · 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 sparkzbarca · Nov 19, 2012 at 06:50 AM 0
Share

just in case thats not clear.

your problem is that if your running the regular update more than once per second your not portraying gravity because that much force should only be applied over a full second.

if your running that 50 times a second your replicating 50 times the force of gravity. and since update varies in speed your actually changing gravity. Its not a constant since update varies in frequency.

avatar image fulcanmal · Nov 26, 2012 at 12:06 AM 0
Share

If I multiply the 50 rather than dividing, it works perfectly now, thanks!!

rigidbody.AddForce(-Vector3.up*9.8*50);

avatar image Map-Builder · Nov 26, 2012 at 12:37 AM 0
Share

rigidbody.AddForce(-Vector3.up*9.8*Time.deltaTime);

deltaTime is the time between 2 frame (~1/50 but is more accurate)

avatar image sparkzbarca · Nov 26, 2012 at 02:20 PM 0
Share

if you'll notice i assumed he would run it in fixed update, in which case fixed update is 50 frames per second by design. Unless your game goes below 50 frames

avatar image Wolfram · Nov 26, 2012 at 02:38 PM 0
Share

@sparkzbarca: not entirely correct. Yes, if nothing gets ever changed in the settings, Time.deltaTime will default to 1/50 in FixedUpdate(). But actually it depends on the value of Edit->ProjectSettings->Time->FixedTimestep (which is Time.fixedDeltaTime). You should never use hardcoded numbers in scripts that could refer to modifiable values.

On a sidenote, if you scale your simulation values with Time.deltaTime, the timing will be "correct", whether you do your calculations in Update() or in FixedUpdate(). The difference is that doing it in FixedUpdate() guarantees that it is executed regularly at equdistant time intervals, while when using Update() it is executed each frame, and the time interval depends on your framerate.

avatar image

Answer by XienDev · Nov 19, 2012 at 07:22 AM

Add constantForce to your gameobject, and play with it =)?

Comment
Fattie
Wolfram
Deeweext

People who like this

1 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 Fattie · Dec 05, 2012 at 09:09 AM 1
Share

it's that simple, great answer

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Gravity is preventing object from moving straight toward its direction when calling rigidbody.AddForce... 1 Answer

Tornado throwing back force after pulling 1 Answer

Artificial Gravity 2 Answers

How to make a object jump constantly at y and move to the next position to z (perfectly) 0 Answers

What's the Rigidbody's gravity unit? 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