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.
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
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.