The distance from the ray's origin to the impact point.
In the case of a ray, the distance represents the magnitude of the vector from the ray's origin to the impact point.
In the case of a swept volume or sphere cast, the distance represents the magnitude of the vector from the origin point to the translated point at which the volume contacts the other collider.
Note that RaycastHit.point represents the point in space where the collision occurs.
// 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;
// Rigidbody component. var rb: Rigidbody;
function Start () { rb = GetComponent.<Rigidbody>();
// Fairly high drag makes the object easier to control. rb.drag = 0.5; rb.angularDrag = 0.5; }
function FixedUpdate () { // Push/turn the object based on arrow key input. rb.AddForce(Input.GetAxis("Vertical") * moveForce * transform.forward); rb.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 = rb.velocity.y; var lift = hoverError * hoverForce - upwardSpeed * hoverDamp; rb.AddForce(lift * Vector3.up); } } }
no example available in C#
See Also: Physics.Raycast, Physics.Linecast, Physics.RaycastAll.