Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Hovering vehicles?

Discussion in 'Scripting' started by Dragon Rider, Jan 23, 2008.

  1. Dragon Rider

    Dragon Rider

    Joined:
    Jan 17, 2008
    Posts:
    280
    At first, I thought WheelColliders would do the trick, because they have a good spring suspension that can be easily modified to act like "air suspension", or "hovering vehicle suspension". But when I actually tried it, they proved very difficult to adapt to this task. The forward/sideways friction is one part of the problem, and the other is the very small collider area. They work fine for wheels, but I can't seem to get them to work for vehicles that hover above the ground. I basically need the same spring motion, only without the wheel physics. Right now, I'm experimenting with different combinations of SpringJoints, colliders, and rigidbody settings without much luck. If anyone has a found a good simple(ish) way to do this, please tell! Thanks.
     
  2. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    Just use the AddForce functions to keep the vehicle hovering. You can check your altitude above ground using a raycast and whenever the vehicle drops below some limit AddForce to the center (or other points) on the vehicle to make it rise/bounce/etc. I've got a helicopter that works quite well this way.
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    This thread has a script attached to it that simulates a hovering vehicle. Might help get you going even if it's not exactly what you want.
     
  4. Dragon Rider

    Dragon Rider

    Joined:
    Jan 17, 2008
    Posts:
    280
    Thanks guys.
     
  5. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
  6. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    I used that method on this hovercraft Its a little wonky, but thats the effect I was going for. Hovercrafts should slide all over the place. :p It takes a really long time tweaking the distance and force amount, there really is a art to it.

    Bill
     
  7. Dragon Rider

    Dragon Rider

    Joined:
    Jan 17, 2008
    Posts:
    280
    Hmm. I get an error when I place the hover.cs script in my Assets folder: "Couldn't load the script 'hover' because it's file name doesn't match the class name." What? :?
     
  8. Ricko

    Ricko

    Joined:
    Dec 9, 2007
    Posts:
    169
    The class name has a capital H. Try naming the file Hover.cs instead.

    Ricko
     
  9. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    A little tip for doing this...

    Bear in mind that an object's weight is equal to its mass times Physics.gravity - if you negate the weight, you get the force needed to hold the object up without rising or falling (you could call it "neutral lift", say).

    Subtract the downward raycast length from your desired hover height - the result is the discrepancy from the desired height, slightly greater than zero if the object is too low and less than zero if it is too high. Multiply the discrepancy by Vector3.up and add it to the neutral lift - you get a force that tends to keep the object at the desired height. You can also multiply Vector3.up by a second value to vary the "springiness" of the hover. You might want to do the traditional velocity damping on the "spring", but the undamped bobbing can give a nice floaty effect for a hovering vehicle. Note that if the actual height is too great then the vehicle will actually get sucked downward by the added force - clamp it to >= zero if there is any danger of this happening.

    I find it easier to think in terms of desired hover height and springiness, so I prefer this approach to the strictly proportional spring system for this kind of thing. Also, since this is basically steadicam physics, it is handy when you want a camera that tracks the ground but doesn't get bumped by small obstacles - just use a really weak springiness value.
     
  10. Dragon Rider

    Dragon Rider

    Joined:
    Jan 17, 2008
    Posts:
    280
    Thanks Ricko, but I have already translated it into JavaScript, with a slight modification to allow for non-spherical planets(Unity terrain). So far, it works pretty good. But my trial ends in one day, so I don'thave time to use it... :cry: Oh well.
     

    Attached Files:

  11. Fabs

    Fabs

    Joined:
    Jun 12, 2012
    Posts:
    3
    Man, if you were a woman I would have married you... your script is the only one working for me after days and days of try-and-fail-and-search-and-fail-too! Thanks a lot for sharing!
     
  12. lupo

    lupo

    Joined:
    Apr 4, 2009
    Posts:
    43
    Big thanks to everyone who contributed to this. I took hover_150.js, which didn't quite work for me, and modified it to hover_150c.js, which is maybe not a true hovercraft feel, but still works quite well for cruising over terrain. I don't see how to attach code to this post but I will put in a link to where i have the code pasted which includes a video tutorial for anyone who wants to try it out. Here is what I call the unity flying box tutorial.
     
  13. Nirav-Madhani

    Nirav-Madhani

    Joined:
    Jan 8, 2014
    Posts:
    27
    What about this ?
     

    Attached Files:

    • JCar.cs
      File size:
      1.2 KB
      Views:
      1,069