Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Apply hover force from 4 points

Discussion in 'Physics' started by trentallain, Sep 28, 2019.

  1. trentallain

    trentallain

    Joined:
    Mar 2, 2019
    Posts:
    9
    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:
    Capture.PNG

    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HoverForce : MonoBehaviour
    5. {
    6.     public float hover_height = 10f;
    7.  
    8.     void FixedUpdate()
    9.     {
    10.         RaycastHit hit;
    11.         if (Physics.Raycast(transform.position, Vector3.down, out hit, hover_height))
    12.         {
    13.             float th = hit.point.y + hover_height;
    14.             float dtt = Mathf.Max(th - transform.position.y, 0f);
    15.             dtt = Mathf.Min(dtt, 1f);
    16.             if (transform.position.y < th)
    17.             {
    18.                 GetComponent<Rigidbody>().AddForce(Vector3.up * dtt, ForceMode.VelocityChange);
    19.             }
    20.         }
    21.     }
    22. }
    How do I do that?
     
  2. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    This youtube tutorial can help you guide you to do a hovering car :
     
    trentallain likes this.
  3. trentallain

    trentallain

    Joined:
    Mar 2, 2019
    Posts:
    9
    Thank you I've made a lot of progress from that!
     
  4. trentallain

    trentallain

    Joined:
    Mar 2, 2019
    Posts:
    9
    Okay so I'm using this script for the hovering:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class HoverForce : MonoBehaviour
    6. {
    7.     public List<Transform> HoverPoints = new List<Transform>();
    8.     public float HoverHeight = 7;
    9.     public float HoverForces = 400;//200
    10.  
    11.     public Rigidbody rb;
    12.     void Start()
    13.     {
    14.         rb = GetComponent<Rigidbody>();
    15.     }
    16.  
    17.     void FixedUpdate()
    18.     {
    19.         // Hover forces
    20.         RaycastHit Hit;
    21.         for (int i = 0; i < 4; i++)
    22.         {
    23.             var hoverPoint = HoverPoints[i];
    24.             if (Physics.Raycast(hoverPoint.transform.position, -Vector3.up, out Hit, HoverHeight))
    25.             {
    26.                 rb.AddForceAtPosition(Vector3.up * HoverForces * (1.0f - (Hit.distance / HoverHeight)), hoverPoint.transform.position);
    27.             }
    28.         }
    29.     }
    30. }
    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)
     
  5. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    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

    it's about PID and hovering... see the PID Serializable class PIDController.cs and the method CalculatHover()
    of VehicleMovement.cs
     
    trentallain likes this.
  6. trentallain

    trentallain

    Joined:
    Mar 2, 2019
    Posts:
    9
    Oh thanks
     
  7. EZHover

    EZHover

    Joined:
    Mar 9, 2022
    Posts:
    7
    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 :)
     
    UnityFledgling likes this.