Skip to content

format

typescript
RaycastHit.distance

class: RaycastHit

description

(Read-only) The distance from the origin of the ray to the point of collision.

You can use this method to get the distance at which the ray intersects with an object, allowing you to perform operations such as interaction, click selection, ray weapon hit detection, collision detection, and more.

parameter

param_nametypedescription

reture

typedescription
numberThe distance from the origin to the point of collision.

code example

typeScript
class New_TypeScript
    extends Component {

        private boxCollider:BoxCollider;
        private ray:Ray;

    OnStart(): void {
        
        this.boxCollider = this.gameObject.GetComponent<BoxCollider>(BoxCollider);

    }

    OnUpdate(): void {

        // Create a ray from (0,0,0) towards the positive Y-axis
		this.ray = new Ray(new Vector3(0,0,0),new Vector3(0,1,0));
        let hit = this.boxCollider.Raycast(this.ray,10000)
        hit.distance;
       
    }
}