Floating above ground / gravity play

Discussion in 'Scripting' started by Jessy, Jul 12, 2007.

  1. Jessy

    Jessy

    Member

    Joined:
    Jun 7, 2007
    Messages:
    7,173
    Hi!

    Though what I want to do is more complicated than what I am about to ask, I believe this may be a simpler case that could lead me to figuring out more on my own.

    Imagine a planetoid (a small sphere is a fine place to start) and a hovercraft. Let's allow the hovercraft to float above the surface of the planetoid at a certain distance, have it be a rigidbody so force can be applied to move it around, and have it not fall off of the planetoid into space as it travels to any point on the surface (even though the surface area might be only a few hundred square meters and the hovercraft about a cubic meter).

    My main goal is solidifying that fixed distance from another stationary object deal, but all of it is interesting to me.

    Thanks!
  2. drJones

    drJones

    Member

    Joined:
    Oct 19, 2005
    Messages:
    1,352
  3. Jessy

    Jessy

    Member

    Joined:
    Jun 7, 2007
    Messages:
    7,173
    The closest thing I got from those links was the following bit from the Mathfx section of the Wiki:

    Code (csharp):
    1. public static Vector3 NearestPoint(Vector3 lineStart, Vector3 lineEnd, Vector3 point)
    2.     {
    3.         Vector3 lineDirection = Vector3.Normalize(lineEnd-lineStart);
    4.         float closestPoint = Vector3.Dot((point-lineStart),lineDirection)/Vector3.Dot(lineDirection,lineDirection);
    5.         return lineStart+(closestPoint*lineDirection);
    6.     }
    However, I must confess that I have no idea how to make that useful to me, as I want to connect objects to winding curves, not straight lines, which this appears to allude to, at least to my uneducated eyes.
  4. jeremyace

    jeremyace

    New Member

    Joined:
    Oct 12, 2005
    Messages:
    1,661
    Not sure about orienting the craft to use the planet's center as the gravity point, but to make something float off a surface you could use a Raycast to get the distance to the "ground", then you apply a vertical relativeForce using that distance multiplied by a control var (to set the strength).

    HTH,
    -Jeremy
  5. podperson

    podperson

    Member

    Joined:
    Jun 6, 2006
    Messages:
    1,364
    I thought about this for a long time and decided that it just wasn't worth the hassle.

    The problem isn't so much applying gravitational force as dealing with the concept of "horizontal". Basically, everything in Unity3D is designed to be easy if the X-Z plane is parallel to "the ground". I'm sure it's perfectly possible to tweak everything so that "the ground" can be an arbitrary plane, but it would involve abandoning a whole lot of free, off-the-shelf functionality.

    One day when I feel a lot more comfortable with Unity I'll probably revisit it (I love the bits in Ratchet and Clank where the world is flipped in different directions, but even in this, very polished commercial game, they've obviously not done it perfectly; the camera becomes flaky and the physics is only correct for some objects), but I think it's biting off more than I can chew right now.
  6. andeeeee

    andeeeee

    Unity Technologies

    Joined:
    Jul 19, 2005
    Messages:
    8,729
    I have put together a simple hovercraft script (attached) which should be enough to get you started with this. The hovering effect is created by four invisible downward thrusters in the vehicle's "corners". A downward force is added in the direction of the planet object to create some gravity.

    Try attaching the script to an available object and make the planet a big (1000-radius) sphere. Start with the hovercraft slightly above the surface of the sphere so that it can detect the ground. The thrusters point in the craft's downward direction.

    The hovercraft has a slightly bouncy/floaty effect that can be altered using the springiness and damping parameters. The damping is very sensitive, however, so it should be kept very low (0 to 0.02 or so).

    One problem I have already found in my brief experience with this script is that the standard Smooth Follow camera doesn't cope very well because it expects a flat surface. You will need a slightly more sophisticated camera to follow the hover around the planet correctly. (Note that the gravity effect can easily be commented out of the code if you change your mind and put the hover on a flat surface.)

    Attached Files:

  7. andeeeee

    andeeeee

    Unity Technologies

    Joined:
    Jul 19, 2005
    Messages:
    8,729
    PS - make sure you turn gravity off for the hovercraft or it will be have two competing gravity forces affecting it (we've all been to places like that). Arrow keys for thrust.
  8. dragiron

    dragiron

    New Member

    Joined:
    Nov 15, 2009
    Messages:
    9
    I have a working camera script that will follow it well.

    Hope it helps!
  9. justicar

    justicar

    Member

    Joined:
    Dec 21, 2010
    Messages:
    76
    Thanks guys. I'm not at the point where I can use these, but have a project that definitely will need them. Into the toolbox they go.
  10. Broken Toy

    Broken Toy

    Member

    Joined:
    Jan 16, 2010
    Messages:
    455
    Very nice! Also, if you're working with non-standard gravity, look into the Constant Force component.
    It's optimized for the purpose of applying forces (and just plain simpler to use).

    http://unity3d.com/support/documentation/ScriptReference/ConstantForce.html
    Last edited: Jan 19, 2011
  11. Kourosh

    Kourosh

    New Member

    Joined:
    Jul 18, 2010
    Messages:
    15
    Hello everyone,
    I've been trying to make perfectly stable and cool hovercraft like the one you find here:
    http://www.youtube.com/watch?v=3xWfBgtI_BA

    Technically I'm following the same concept everyone has been talking about in all these forums with a slight deviation. In the script I defined 4 corners and tried to play around with constant force instead of adding force in those corners, cause I couldn't achieve a perfect equation for how much force it needs to be applied to each corner in order to avoid sudden clashes into the terrain. However I use the corners to cast a ray from, and get the normal of the terrain in the hit points, calculate an average vector and use Quaternion to rotate the hovercraft parallel to the ground. It works fine however it still needs to be polished in some areas:

    1. How is it possible to have the bounciness only when the hovercraft is not moving?
    2. What is the right physics force to apply so that it neither clashes to the terrain nor jumps off high into the sky?
    3. I'm not sure how to deal with collisions. So that it nicely slips on top of the terrain when hit by a missile or any other collisions.

    here is the link to the scene: http://bit.ly/jXMIKT

    Thank you very much for your time and help. :)
    Last edited: Apr 30, 2011
  12. 258pilot

    258pilot

    New Member

    Joined:
    Jul 15, 2011
    Messages:
    20
    How do you rotate your hovercraft parallel to the ground? i'm looking for so long to a solution for that...
  13. Kourosh

    Kourosh

    New Member

    Joined:
    Jul 18, 2010
    Messages:
    15
    I get the normal of the ground and rotate the object to match the normal's vector.
  14. niks07

    niks07

    New Member

    Joined:
    Oct 11, 2011
    Messages:
    11
    hello andeee...

    i am new in unity and i am creating a 3D game.
    i want to float my flying object in forward direction in air same as in water but i can not find any way to do so..please tell me the way i can go with.

    anyone there who had the way to do so or having code please give it me.

    Thank you very much.
  15. ztripez

    ztripez

    New Member

    Joined:
    Dec 15, 2011
    Messages:
    1
    Can someone explain howto calculate the dampning?
    I get very lost by all the inlinefunctions.

    I've modifed the script to be a "hover" object that you apply to a sub-gameobject to my craft, to make it easier to balance the craft. However i can't understand the dampning calculation :/.

    I get the craft to hover but it Woobles alot! :(.

    Here is my somewhat modied script:
    Code (csharp):
    1. void FixedUpdate () {
    2.                 Transform RacerTransform = transform.parent.parent;
    3.         Vector3 relUp = transform.up;
    4.         Vector3 dampVec = new Vector3(0, damping, 0);
    5.         RaycastHit hit;
    6.         Rigidbody r = RacerTransform.rigidbody;
    7.         if (Physics.Raycast(transform.position, -transform.up, out hit))
    8.         {
    9.             liftforce = (gravity+r.mass) * (HoverHeight - hit.distance) * springiness;
    10.             RacerTransform.rigidbody.AddForceAtPosition(relUp * liftforce,transform.position);
    11.             Debug.DrawRay (transform.position, relUp * (liftforce),DebugColor,2);
    12.         }
    13. }
    Here is a build that demostrates my problem:

    http://dl.dropbox.com/u/5093350/AirRace/Unity/WebPlayer/WebPlayer.html