- Home /
Question by
xpauloppelt · Nov 09, 2016 at 08:30 AM ·
scripting problemhoverc #hovering
Hovercar Script, object slowly descends instead
I'm trying to write a script using various examples with the end-goal of making an object hover.
However, my code simply makes the object slowly descend to a plane underneath it.
There are 4 thrusters that are placed within the car's hierarchy and are within the script's array in the inspector tab.
The code is as follows:
using UnityEngine;
using System.Collections;
public class ThrusterScript : MonoBehaviour {
//DEFINITIONS
//FLOATS - independent variables
public float desiredHeight;
public Rigidbody rb;
//ARRAY - containing all vehicle thrusters
public Transform[] thruster_group;
//FORCES - fN (normal force) and fG (gravitational force)
Vector3 fN;
Vector3 fG;
void Start()
{
//Defining rb as Rigidbody; rb.mass as its mass
rb = GetComponent<Rigidbody>();
//Defining global forces for simplicity
fN = -1 * (Physics.gravity * rb.mass);
fG = (Physics.gravity * rb.mass);
}
void FixedUpdate()
{
//RAYCASTING from all thrusters in array
RaycastHit hit;
foreach (Transform thruster in thruster_group)
{
Vector3 fThrust;
float HDifCo;
//SIMPLICITY - refer to desiredHeight as dH
float dH = desiredHeight;
if (Physics.Raycast (thruster.position, thruster.up * (-1), out hit, desiredHeight)) {
//DEFINITION - Height Difference Coefficient
HDifCo = 1 - (hit.distance / dH);
fThrust = fG * HDifCo;
rb.AddForceAtPosition(fThrust, thruster.position);
}
}
}
}
Can anyone pinpoint the issue? Thank you.
Comment