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

Counteract Gravity?

Discussion in 'Scripting' started by infinitypbr, Dec 11, 2014.

  1. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Hello,

    I have a dragon that I'm trying to make fly in a semi-realistic way, but not by using real physics.

    I'm attempting to use gravity so that when the dragon is level, he doesn't fall or rise, but when he tilts up or down, the amount of counter-acting force is changed to slow climbs and speed dives.

    However, in FixedUpdate(), my anti-gravity code doesn't seem to work -- he still falls. If I do not use Time.fixedDeltaTime then he rises.

    What's the proper way to negate gravity?

    Code (csharp):
    1. var gravity = Physics.gravity;
    2. rigidbody.AddForce(-gravity);
     
  2. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    You can always set: dragon.rigidbody.usegravity to false while he's flying, then when not flying, set it to true.
     
  3. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    True -- however, I'm hoping to get an effect where he slows down while flying up and speeds up while flying down. To do this, I was going to modify the upward thrust (the -gravity above), so there is less thrust when he's flying down and more thrust when flying up. So when he's not tilted, it should be no change in height.
     
  4. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Alright, so it looks like 1/2 gravity is being used -- if I counteract with -gravity * 0.5, the dragon will stay in the same spot. No idea why -- this physics stuff is perhaps over my head.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    What's the mass of your object?

    When you call 'AddForce' and you don't specify the ForceMode, it defaults to ForceMode.Force.

    http://docs.unity3d.com/ScriptReference/ForceMode.html

    As you can see here, ForceMode.Force is:
    Thing is gravity is a massless acceleration. All things, no matter the mass, fall at the same rate (in a vacuum of course, air resistance changes this).

    Try ForceMode.Acceleration:

    Code (csharp):
    1.  
    2. rigidbody.AddForce(-Physics.gravity, ForceMode.Acceleration);
    3.  
     
    Last edited: Dec 13, 2014
  6. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Could you elaborate on that? Do you mean that you are avoiding the built in physics to save on CPU? Because if so, that means that you would need to cut out the rigid body and the gravity.

    Try starting off with the variables you'll need to know. You know that you'll need:
    -Gravity (in units accelerated per second)
    -Wind Resistance and/or terminal velocity
    -Pitch
    -Momentum
    -Force of Wings Flapping

    Then you get the effects of gravity based on the pitch (both forward and back, as well as side to side), subtract a percentage of the momentum based on wind resistance, and add directional force every time it flaps its wings.

    Would you like me to elaborate more in math terms?
     
  7. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Thanks guys -- I ended up ditching gravity altogether, and going instead with just a speed offset based on the tilt of the dragon. If the dragon is tilted up, from 0 - 0.33 points less speed based on the extent of the tilt. When the dragon is tilted down, from 0 - 0.5 points more speed based on the tilt.

    To get the "hard to pull out of a dive" effect, I simply reduce the amount the joystick will change the tilt based on the speed bonus (that 0 - 0.5 points on the dive), so that the tilt out of a dive starts off slower and picks up as you hold the joystick down.

    The effect seems to be what I'm going for.


    I wanted to avoid real physics because I figure it'd be hard *(for me) to get dragon-physics correct. But also because I heard about a VR thing where people could flap their arms to affect bird wings in the game and the bird physics were real, but people just couldn't figure it out, so they had to change the physics to give the people an edge. I figure that players won't be interested in learning the physics of the dragon. So as long as it feels right -- not like an airplane, but not difficult or overly complicated -- then the game should be fun. Especially since piloting the dragon is only 1/2 of it -- you still have to shoot down enemies with your magic or bow!
     
  8. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Cool, sounds like you were one step ahead of me.