RaycastHit.distance

var distance : float

Description

The distance from the ray's origin to the impact point.

JavaScript
    // Movable, levitating object.

// This works by measuring the distance to ground with a
// raycast then applying a force that decreases as the object
// reaches the desired levitation height.

// Vary the parameters below to
// get different control effects. For example, reducing the
// hover damping will tend to make the object bounce if it
// passes over an object underneath.

// Forward movement force.
var moveForce = 1.0;

// Torque for left/right rotation.
var rotateTorque = 1.0;

// Desired hovering height.
var hoverHeight = 4.0;

// The force applied per unit of distance below the desired height.
var hoverForce = 5.0;

// The amount that the lifting force is reduced per unit of upward speed.
// This damping tends to stop the object from bouncing after passing over
// something.
var hoverDamp = 0.5;



function Start () {
// Fairly high drag makes the object easier to control.
rigidbody.drag = 0.5;
rigidbody.angularDrag = 0.5;
}


function FixedUpdate () {
// Push/turn the object based on arrow key input.
rigidbody.AddForce(Input.GetAxis("Vertical") * moveForce * transform.forward);
rigidbody.AddTorque(Input.GetAxis("Horizontal") * rotateTorque * Vector3.up);

var hit: RaycastHit;
var downRay = new Ray(transform.position, -Vector3.up);

// Cast a ray straight downwards.
if (Physics.Raycast(downRay, hit)) {
// The "error" in height is the difference between the desired height
// and the height measured by the raycast distance.
var hoverError = hoverHeight - hit.distance;

// Only apply a lifting force if the object is too low (ie, let
// gravity pull it downward if it is too high).
if (hoverError > 0) {
// Subtract the damping from the lifting force and apply it to
// the rigidbody.
var upwardSpeed = rigidbody.velocity.y;
var lift = hoverError * hoverForce - upwardSpeed * hoverDamp;
rigidbody.AddForce(lift * Vector3.up);
}
}
}

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public float moveForce = 1.0F;
public float rotateTorque = 1.0F;
public float hoverHeight = 4.0F;
public float hoverForce = 5.0F;
public float hoverDamp = 0.5F;
void Start() {
rigidbody.drag = 0.5F;
rigidbody.angularDrag = 0.5F;
}
void FixedUpdate() {
rigidbody.AddForce(Input.GetAxis("Vertical") * moveForce * transform.forward);
rigidbody.AddTorque(Input.GetAxis("Horizontal") * rotateTorque * Vector3.up);
RaycastHit hit;
Ray downRay = new Ray(transform.position, -Vector3.up);
if (Physics.Raycast(downRay, out hit)) {
float hoverError = hoverHeight - hit.distance;
if (hoverError > 0) {
float upwardSpeed = rigidbody.velocity.y;
float lift = hoverError * hoverForce - upwardSpeed * hoverDamp;
rigidbody.AddForce(lift * Vector3.up);
}
}
}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

public moveForce as single = 1.0F

public rotateTorque as single = 1.0F

public hoverHeight as single = 4.0F

public hoverForce as single = 5.0F

public hoverDamp as single = 0.5F

def Start():
rigidbody.drag = 0.5F
rigidbody.angularDrag = 0.5F

def FixedUpdate():
rigidbody.AddForce(((Input.GetAxis('Vertical') * moveForce) * transform.forward))
rigidbody.AddTorque(((Input.GetAxis('Horizontal') * rotateTorque) * Vector3.up))
hit as RaycastHit
downRay as Ray = Ray(transform.position, -Vector3.up)
if Physics.Raycast(downRay, hit):
hoverError as single = (hoverHeight - hit.distance)
if hoverError > 0:
upwardSpeed as single = rigidbody.velocity.y
lift as single = ((hoverError * hoverForce) - (upwardSpeed * hoverDamp))
rigidbody.AddForce((lift * Vector3.up))

See Also: Physics.Raycast, Physics.Linecast, Physics.RaycastAll.