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

Changing gravity having no effect?

Discussion in 'Physics' started by Tovey-Ansell, Oct 6, 2016.

  1. Tovey-Ansell

    Tovey-Ansell

    Joined:
    Jul 8, 2015
    Posts:
    149
    I have a script that changes the strength of gravity as a rocket gains altitude, every FixedUpdate it runs the plugs the values of altitude and other stuff like the Earth's radius and mass and calculates an accurate value for gravity, setting the gravity using
    Code (CSharp):
    1. Physics.gravity = new vector3 (0, gravFieldStrength, 0);
    I can see the value for gravity is changing correctly, using
    Code (CSharp):
    1. Debug.Log(gravFieldStrength);
    I can see the value is updating through the console, however the rocket's RigidBody doesn't seem to care, I've even tested it by saying "when altitude is greater than 100, make gravity 1000", and there is no change in the RigidBody's velocity, I've even tried setting gravity to 1000 in the Start method, but the rocket still lifts off just fine, and when setting it to -1000 it doesn't fall upwards...

    I've made sure the RigidBody isn't sleeping by waking it every FixedUpdate but still not having any luck, does anybody have any ideas?

    Could this be something to do with my GameObjects being 2D and thus using RigidBody2Ds?

    Thanks,
    Fred T.A
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,418
    You should set Physics2D.gravity for 2D rigidbodies.

    The global classes Physics and Physics2D control the settings for 3D and 2D physics, respectively.
     
  3. Tovey-Ansell

    Tovey-Ansell

    Joined:
    Jul 8, 2015
    Posts:
    149
    Ugh stupid!

    That works now, thanks for pointing out I was being dumb :p

    I actually now have a more (I hope) legitimate problem, I can't make the gravity negative? Regardless of whether I add a negative sign, the rocket always falls downwards? Again, the console tells me that gravity is negative, but the rigidbodies seem to disagree?