Hit results of a ray-cast provided to script. See Probe.

// Given an object with a BodyComponent
// If the object intersects with the ray described
// by rayStart and rayEnd, print a message.

var rayStart = new vec3(0.0, -100.0, 0.0);
var rayEnd = new vec3(0.0, 100.0, 0.0);

// Create a probe to raycast through all worlds.
var globalProbe = Physics.createGlobalProbe();
globalProbe.rayCast(rayStart, rayEnd, function (hit) {
if (hit) {
print("Raycast hit: " + hit.collider.getSceneObject().name);
}
});

// Create a probe to raycast through only the implicit root world.
var rootProbe = Physics.createRootProbe();
rootProbe.rayCast(rayStart, rayEnd, function (hit) {
if (hit) {
print("Raycast hit all: " + hit.collider.getSceneObject().name);
}
});
// On Tap, raycast through the scene at the tapped position and print out a message for each object hit.

// @input Component.Camera camera

var cameraTransform = script.camera.getTransform();
var camera = script.camera;

script.createEvent("TapEvent").bind(function(touchArgs) {
var touchPos = touchArgs.getTapPosition();
var nearPos = camera.screenSpaceToWorldSpace(touchPos, 0);
var farPos = camera.screenSpaceToWorldSpace(touchPos, camera.far - camera.near);
Physics.createGlobalProbe().rayCastAll(nearPos, farPos, function(hitResults) {
print("Hit " + hitResults.length + " objects:")
for(var i=0; i<hitResults.length; i++) {
var hit = hitResults[i];
print(hit.collider.getSceneObject().name + " at " + hit.position);
}
});
});
interface RayCastHit {
    collider: ColliderComponent;
    distance: number;
    normal: vec3;
    position: vec3;
    skipRemaining: boolean;
    t: number;
    triangle: TriangleHit;
    getTypeName(): string;
    isOfType(type: string): boolean;
    isSame(other: ScriptObject): boolean;
}

Hierarchy (view full)

Properties

The collider that was hit.

distance: number

Distance from the ray origin to the point of intersection.

normal: vec3

Surface normal on the collider at the point of intersection.

position: vec3

Position at the point of intersection.

skipRemaining: boolean

Set to true to skip remaining results, if any.

t: number

Ray interpolant at the point of intersection, in the range [0, 1].

triangle: TriangleHit

The hit triangle, if it exists. This is set on intersection with mesh-based colliders, and null otherwise.

Methods

  • Returns true if the object matches or derives from the passed in type.

    Parameters

    • type: string

    Returns boolean

  • Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

    Parameters

    Returns boolean