hi does anyone know how i can make a hovercar hover by raycasting? like if its too close to the ground then make it higher and if its too high then make it go lower?
float hoverDistance;
float hoverForce;
void FixedUpdate()
{
if(Physics.Raycast(transform.position, Vector3.down, hoverDistance)
{
rigidbody.AddForce(Vector3.up * hoverForce);
} else {
rigidbody.AddForce(Vector3.down * hoverForce);
}
}
There’s a basic one. You could do with some damping, but this should make it fly.
I have this script which creates hover vehicle. there’s a bunch of things in here that you probably wont need.
Create a gameobject with a Rigidbody component then add this script as a component.
var driveForce: float;
var hoverHeight: float;
var thrustForce: float;
var steerForce: float;
var steerPosZ: float;
var thrusters: Vector3[];
var maxVelocity: float;
var layerMask: LayerMask;
var enableControl: boolean = false;
static var driveMulti: float =1;
static var steerMulti: float =-1;
var steer: float;
var targetNormal : Vector3;
var hoverHit: RaycastHit;
function Start (){
//code to set height from start height
var dwn = transform.TransformDirection (Vector3.down);
if (Physics.Raycast (transform.position, dwn, hoverHit)) {
print (hoverHit.distance);
}
hoverHeight = hoverHit.distance;
targetNormal = transform.up;
}
function FixedUpdate () {
var hit: RaycastHit;
var normalhit :RaycastHit;
var addedhit :RaycastHit;
var thrustMulti : float;
var adjustRatio : float = 0.1; //amount of the difference to rotate by
var addedforce : float;
//Cast a ray to the object below to apply a fake gravity. You can comment these lines out if you want to use real gravity.
if (Physics.Raycast(transform.position, -transform.up, normalhit)){
targetNormal = hit.normal;
print (targetNormal);
Debug.DrawLine (transform.position, normalhit.point);
}
if (enableControl){
steer = Input.GetAxis("Horizontal");
var brakeLeft:float;
var brakeRight:float;
//Fake Gravity
rigidbody.AddRelativeForce(-targetNormal.up * 100);
var fwd: float = Input.GetAxis("Vertical");
if (fwd >0){
rigidbody.AddForceAtPosition(transform.forward * (driveForce * fwd), transform.TransformPoint(Vector3.forward * 1));
}
if (fwd <0){
rigidbody.AddRelativeForce(transform.forward * (driveForce * fwd));
}
//Steering
rigidbody.AddRelativeForce(transform.right * steer*steerForce);
rigidbody.AddForceAtPosition(transform.right * (steerForce * steer * steerMulti), transform.TransformPoint(Vector3.forward * steerPosZ));
if (Physics.Raycast(transform.position, -transform.up, addedhit))
{
addedforce = (hoverHeight - addedhit.distance);
if (addedforce <=0){
addedforce=0;
}
}
if (addedhit.distance <=2){
rigidbody.AddRelativeForce(targetNormal.up *(addedforce*20));}
rigidbody.AddRelativeForce(targetNormal.up * (thrustForce*(addedforce*addedforce)));
//Create an array for the vertical thrusters and apply a force to each one.
for (i = 0; i < thrusters.Length; i++) {
var wdThruster: Vector3 = transform.TransformPoint(thrusters*);*
-
if (Physics.Raycast(wdThruster, -transform.up, hoverHit))*
-
{*
-
discrep = (hoverHeight - hoverHit.distance);*
-
if (discrep <=0.1){discrep=0;}*
-
var upVel: float = rigidbody.GetRelativePointVelocity(wdThruster).y;*
_ rigidbody.AddForceAtPosition(transform.up * (thrustForce *discrep), wdThruster);_
-
}*
-
}*
-
}*
}
//Draw some gizmos to indicate thruster position.
function OnDrawGizmos() {
- for (i = 0; i < thrusters.Length; i++) {*
_ Gizmos.DrawWireSphere(transform.TransformPoint(thrusters*), 0.1);_
_ }*_
_ Gizmos.DrawWireSphere(transform.TransformPoint(Vector3.right * steer), 2);_
}
The script above looks interesting, but it gives me an error message:
Unknown identifier:‘i’
Unknown identifier:‘discrep’