Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to give an object more gravity?

Discussion in 'Editor & General Support' started by BlakeGillman, Apr 28, 2014.

  1. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Simple as the title, instead of increasing the gravity in the settings, is there a way I can define the gravity through JS for a specefic object? Like have an integer variable that I can adjust and depending on that variable will depends the object's gravity. Can't figure it out personally. Right now I am using RigidBody on my car, and it is light as a feather.

    (The reason I ask for per object instead of overall setting) is because inside a script I want a gravity function that I can adjust according to different vehicles I make.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    All you basically need to do is adding additional forces to the rigidbody. The physics engine will automatically consider the additional forces and calculate the correct motion/velocity.

    As it is a influence on physics, it should be done in FixedUpdate just like:

    Code (csharp):
    1.  
    2. public class gravityController : MonoBehaviour {
    3.  
    4.     public float counterGravity = 9.81f;
    5.  
    6.     void FixedUpdate()
    7.     {
    8.         rigidbody.AddForce(new Vector3(0,counterGravity,0));
    9.     }
    10. }
    11.  
    If the counterGravity is set to the opposite of the global setting, the object will act as if it is not influenced by gravity anymore because both forces ... [missing the correct word for it, sorry] the force will just equalize the gravity force. :S
     
    Last edited: Apr 28, 2014
  3. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Thanks it works :)
     
    Last edited: Apr 28, 2014