I’ve written my own hovercraft physics, however I can’t seem to get it to work. I have Ray Position game objects that I place on different corners of the vehicle to dictate where the raycasts will go. These positions are then used to create the raycasts (going down), then determine where to add force on each corner of the object. I know I have nothing rotating or moving it forward, but what am I missing to get it off of the ground?
EDIT: Okay I figured out how to get it off of the ground with help from Wolfram, now it rocks and flips over a lot how can I fix that?
#pragma strict
var vehicleSpeed = 20;
var rotationSpeed = 10.0;
var airborneForce = 5.0;
var offgroundHeight = 10.0;
var startRotation : Quaternion;
var startPosition : Vector3;
var rayPos : GameObject[];
function Start () {
rayPos = GameObject.FindGameObjectsWithTag("RayPos");
startRotation = transform.rotation;
startPosition = transform.position;
}
function Update () {
var down = transform.TransformDirection (-Vector3.up);
var forwardForce : float = Input.GetAxis ("Vertical") * vehicleSpeed;
var flight = transform.TransformDirection (Vector3.up * airborneForce);
rigidbody.AddForce (transform.forward * forwardForce);
if(Input.GetAxis ("Horizontal")){
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
transform.Rotate (0, rotation, 0);
}
if(Input.GetButtonDown("Jump")){
rigidbody.drag = 50;
}else
if(Input.GetKeyDown("left ctrl")){
transform.rotation = startRotation;
transform.position.y += 2;
rigidbody.drag = 50;
}else
if(Input.GetKeyDown("r")){
transform.rotation = startRotation;
transform.position = startPosition;
transform.position.y += 2;
rigidbody.drag = 50;
}else
rigidbody.drag = 0;
if (Physics.Raycast (rayPos[0].transform.position, down, offgroundHeight)) {
rigidbody.AddForceAtPosition(flight, rayPos[0].transform.position);
}
if (Physics.Raycast (rayPos[1].transform.position, down, offgroundHeight)) {
rigidbody.AddForceAtPosition(flight, rayPos[1].transform.position);
}
if (Physics.Raycast (rayPos[2].transform.position, down, offgroundHeight)) {
rigidbody.AddForceAtPosition(flight, rayPos[2].transform.position);
}
if (Physics.Raycast (rayPos[3].transform.position, down, offgroundHeight)) {
rigidbody.AddForceAtPosition(flight, rayPos[3].transform.position);
}
}