| origin | The point in 2D space where the ray originates. | 
| direction | Vector representing the direction of the ray. | 
| distance | Maximum distance over which to cast the ray. | 
| layerMask | Filter to detect Colliders only on certain layers. | 
| minDepth | Only include objects with a Z coordinate (depth) greater than this value. | 
| maxDepth | Only include objects with a Z coordinate (depth) less than this value. | 
Casts a ray against colliders in the scene.
// Float a rigidbody object a set distance above a surface.var floatHeight: float;     // Desired floating height.
var liftForce: float;       // Force to apply when lifting the rigidbody.
var damping: float;         // Force reduction proportional to speed (reduces bouncing).function FixedUpdate() {
	// Cast a ray straight down.
	var hit: RaycastHit2D = Physics2D.Raycast(transform.position, -Vector2.up);
	
	// If it hits something...
	if (hit != null) {
		// Calculate the distance from the surface and the "error" relative
		// to the floating height.
		var distance = Mathf.Abs(hit.point.y - transform.position.y);
		var heightError: float = floatHeight - distance;
		
		// The force is proportional to the height error, but we remove a part of it
		// according to the object's speed.
		var force = liftForce * heightError - rigidbody2D.velocity.y * damping;
		
		// Apply the force to the rigidbody.
		rigidbody2D.AddForce(Vector3.up * force);
	}
}
      using UnityEngine; using System.Collections; public class Example : MonoBehaviour { public float floatHeight; public float liftForce; public float damping; void FixedUpdate() { RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up); if (hit != null) { float distance = Mathf.Abs(hit.point.y - transform.position.y); float heightError = floatHeight - distance; float force = liftForce * heightError - rigidbody2D.velocity.y * damping; rigidbody2D.AddForce(Vector3.up * force); } } }
import UnityEngine import System.Collections public class Example(MonoBehaviour): public floatHeight as float public liftForce as float public damping as float def FixedUpdate() as void: hit as RaycastHit2D = Physics2D.Raycast(transform.position, -Vector2.up) if hit != null: distance as float = Mathf.Abs((hit.point.y - transform.position.y)) heightError as float = (floatHeight - distance) force as float = ((liftForce * heightError) - (rigidbody2D.velocity.y * damping)) rigidbody2D.AddForce((Vector3.up * force))