Apply hover force from 4 points

Hi, I’m new to unity. I’m trying to make a spaceship that hovers above the ground. So far, I’ve done this from the center of it, however, the ship wobbles around. So what I’m trying to do is apply 1/4 of the force to each 4 corners of the ship so that it is stabilized much better and looks better going over terrain.

Like this:

My object has a rigidbody with a mass of 20, drag of 0.25, and an angular drag of 0.05.

Here’s what I have so far:

using UnityEngine;
using System.Collections;

public class HoverForce : MonoBehaviour
{
    public float hover_height = 10f;

    void FixedUpdate()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, Vector3.down, out hit, hover_height))
        {
            float th = hit.point.y + hover_height;
            float dtt = Mathf.Max(th - transform.position.y, 0f);
            dtt = Mathf.Min(dtt, 1f);
            if (transform.position.y < th)
            {
                GetComponent<Rigidbody>().AddForce(Vector3.up * dtt, ForceMode.VelocityChange);
            }
        }
    }
}

How do I do that?

This youtube tutorial can help you guide you to do a hovering car :

1 Like

Thank you I’ve made a lot of progress from that!

Okay so I’m using this script for the hovering:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HoverForce : MonoBehaviour
{
    public List<Transform> HoverPoints = new List<Transform>();
    public float HoverHeight = 7;
    public float HoverForces = 400;//200

    public Rigidbody rb;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        // Hover forces
        RaycastHit Hit;
        for (int i = 0; i < 4; i++)
        {
            var hoverPoint = HoverPoints[i];
            if (Physics.Raycast(hoverPoint.transform.position, -Vector3.up, out Hit, HoverHeight))
            {
                rb.AddForceAtPosition(Vector3.up * HoverForces * (1.0f - (Hit.distance / HoverHeight)), hoverPoint.transform.position);
            }
        }
    }
}

This is better than before because it is now from 4 positions, however, the hovering only works if the script is disabled until the object is touching the ground. Otherwise the ship goes infinitely upwards. Once enabled again it bobs up and down as expected (would rather that it didn’t do this though). Another issue is that if I move the ship to the edge of the ground (a cube), it launches itself off of it and almost orbits it…

Any ideas on what’s going on there?

Mass = 255 (about 2500 kg with a 4m wingspan)
Drag = 0.3
Angular drag = 0.1
g = 9.81
Hover Height = 1.5m
Hover Forces = 800N each (4 bottom corners of ship’s box collider)

using 4 points to hover could maybe work on plane surfaces, but on complexe surfaces/terrains the render could be very weird…
I’m surprised you didn’t found on internet the Hover Racer Live
https://www.youtube.com/watch?v=ULDhOuU2JPY

it’s about PID and hovering… see the PID Serializable class PIDController.cs and the method CalculatHover()
of VehicleMovement.cs

1 Like

Oh thanks

Hover vehicles will likely need more hover points than just 4 at the edges, especially when hovering over terrain with irregularities.

I am currently developing a free tool called ‘EZ Hover’ that easily applies hover physics to any gameobject with a rigidbody. It can automatically generate a grid of hover points below the space ship and it allows for easy rotation vertically and horizontally.

You can view the Work in Progress thread here .

I’d be happy to share the code on GitHub if it helps :slight_smile:

1 Like