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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to add more gravity to the object?

Discussion in 'Scripting' started by babji3, Jul 8, 2015.

  1. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Hi All, I know how to increase the gravity in editor , its in Edit>Project Settings>Physics.
    By code also i know
    Physics.gravity = new Vector3 (0,-15,0);

    But How I give more gravity to the specific object that i want?

    The Above two are effected all the object.
    Thanks
     
  2. AnthonySturdy

    AnthonySturdy

    Joined:
    Feb 24, 2014
    Posts:
    55
    If you have a rigidbody on it, just edit the gravity from the rigidbody.
     
  3. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    @Ansity Can you really edit gravity for rigidbody? I didnt know that.. well if it doesnt work, you can always addForce(0,-15,0)
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,201
    If you want "realistic" force, remember that gravity is not affected by mass, so you should use Forcemode.Acceleration. Putting one cube with gravity on next to a cube with gravity off and this code on it:

    Code (CSharp):
    1. public void FixedUpdate() {
    2.     GetComponent<Rigidbody>().AddForce(Physics.gravity, ForceMode.Acceleration);
    3. }
    makes them fall exactly in lockstep, meaning that it's a good way to do gravity manually. So if you want something to fall twice as fast as "normal" things, you just do AddForce(Physics.gravity * 2f, ForceMode.Acceleration);
     
    D4Cheap, aradfr, johnfu11er and 5 others like this.
  5. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    "If you have a rigidbody on it, just edit the gravity from the rigidbody."

    U cant edit the gravity in rigidbody..

    Baste
    "AddForce(Physics.gravity * 2f, ForceMode.Acceleration);"

    Its not taking exact gravity ,its pulling down force.
    U check By editing the gravity in editor and writing this code , both are not same.
    Is their any other way??
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  7. AnthonySturdy

    AnthonySturdy

    Joined:
    Feb 24, 2014
    Posts:
    55
    Yes you can.
     
  8. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    Oh, that is rigidbody 2d. Newer used those. It use different physics engine, perhaps there is some differences with settings.

    edit: op was talking about 3d
     
  9. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    ya am talking about 3d objects not for 2d. Thanks
     
  10. AnthonySturdy

    AnthonySturdy

    Joined:
    Feb 24, 2014
    Posts:
    55
    oh I thought it was pretty much the same for 2D and 3D, fair enough.