Search Unity

Hover car (roll over)

Discussion in 'Physics' started by StefanBahanov, Dec 9, 2018.

  1. StefanBahanov

    StefanBahanov

    Joined:
    Jan 25, 2017
    Posts:
    2
    Hi,
    I`m new to Physics. I'm trying to make a car ( a box for now) to hover about 3 meters above the ground. I saw this example code witch basically use four thrusters at each angle of the car and creates lifting force relative to height and the mass of the car. The problem is that the car always hover for a split second and then roll over. There is no balance at all. Should I restrict the rotating angle or the problem is something i miss.
    Thanks :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Hover : MonoBehaviour {
    6.  
    7.  
    8. public float thrusterStrenght;
    9. public float maxDistance;
    10.  
    11. Rigidbody rigidbody;
    12. public Transform[] thrusters;
    13.     void Start()
    14.     {
    15.         rigidbody = gameObject.GetComponent<Rigidbody>();
    16.     }
    17.     void FixedUpdate () {
    18.         RaycastHit hit;
    19.         foreach(var thruster in thrusters)
    20.         {
    21.             Vector3 downForce;
    22.             float distancePercentage;
    23.             if(Physics.Raycast(transform.position,thruster.up * -1, out hit, maxDistance))
    24.             {
    25.                 distancePercentage = 1-(hit.distance/maxDistance);          
    26.                 downForce = transform.up * thrusterStrenght * distancePercentage;
    27.                 downForce = downForce * Time.deltaTime * rigidbody.mass;
    28.                 rigidbody.AddForceAtPosition(downForce,thruster.position);      
    29.             }
    30.         }
    31.     }
    32. }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Start from single box. It should hover, if code work.
    If that confirmed, then check center of mas, of your car.
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Why actually use 4?
    Seems overly complicated for no reason.

    Just use the one on the pivot/CoM and put 4 truster graphics where you want them
     
    Antypodish likes this.
  4. StefanBahanov

    StefanBahanov

    Joined:
    Jan 25, 2017
    Posts:
    2
    This kind of works. Now the box is floating above the terrain but rotates in some unwanted direction. Should I restrict the rotation somehow. I mean that is like to put jet engine under the center of real car and expect to fly directly upwards?
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If you don't want the rotation consider using AddForce instead of the AddForceAtPosition, it just applies a force in a direction with no torque.

    You could also go to the rigidbody setting in the inspector and in the constraints lock it's rotation in what ever axis you want.
     
    Antypodish likes this.