Unity Community


Results 1 to 15 of 15

  1. Location
    NE Ohio, USA
    Posts
    7,173

    Floating above ground / gravity play

    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. Location
    New Hampshire
    Posts
    1,352
    i can't offer much insight myself but here's some older posts on the issue:

    http://forum.unity3d.com/viewtopic.php?t=4021

    http://forum.unity3d.com/viewtopic.php?t=3845

    http://forum.unity3d.com/viewtopic.php?t=4230

    i could swear i remember another one but i can't seem to find it right now ; )
    -O


  3. Location
    NE Ohio, USA
    Posts
    7,173
    The closest thing I got from those links was the following bit from the Mathfx section of the Wiki:

    Code:  
    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. Location
    Ontario, Canada
    Posts
    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
    GUI-eXtended 1.x is now Open-Source!
    https://bitbucket.org/JeremyHollings...ded_opensource


  5. Location
    Arlington, VA
    Posts
    1,356
    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. Unity tech writer

    Location
    Blackpool, United Kingdom
    Posts
    8,696

    Re: Floating above ground / gravity play

    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. Unity tech writer

    Location
    Blackpool, United Kingdom
    Posts
    8,696
    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. Posts
    9
    I have a working camera script that will follow it well.

    var target : Transform;
    var distance = 3.0;
    var height = 3.0;
    var damping = 5.0;
    var smoothRotation = true;
    var rotationDamping = 10.0;
    var heightDamping = 2.0;

    function LateUpdate () {

    wantedRotationAngle = target.eulerAngles.y;
    wantedHeight = target.position.y + height;

    currentRotationAngle = transform.eulerAngles.y;
    currentHeight = transform.position.y;

    currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
    currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

    var wantedPosition = target.TransformPoint(0, height, -distance);
    transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);

    if (smoothRotation) {
    var wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
    transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
    }

    else transform.LookAt (target, target.up);
    }
    Hope it helps!


  9. Posts
    53
    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. Location
    Québec, Canada
    Posts
    452
    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).

    Rigidbody.AddForce applies a force to the Rigidbody only for one frame, thus you have to keep calling the function.
    ConstantForce on the other hand will apply the force every frame until you change the force or torque to a new value.
    http://unity3d.com/support/documenta...tantForce.html
    Last edited by Werewraith; 01-19-2011 at 05:10 AM.


  11. Posts
    15

    Hovercraft under construction

    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 by Kourosh; 04-30-2011 at 01:53 AM.
    Kourosh


  12. Location
    Braga, Portugal
    Posts
    20
    How do you rotate your hovercraft parallel to the ground? i'm looking for so long to a solution for that...


  13. Posts
    15
    I get the normal of the ground and rotate the object to match the normal's vector.
    Kourosh


  14. Posts
    11

    floating object in air as in water

    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. Posts
    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:  
    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/AirR...WebPlayer.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •