Skip to main content

RayCastHit

Class

Hit results of a ray-cast provided to script.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

Since Lens Scripting v164

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

Index

Example

// 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);
}
});
});

Extends

Properties

collider

readonly collider: ColliderComponent

The collider that was hit.

Since

Lens Scripting Version 164


distance

readonly distance: number

Distance from the ray origin to the point of intersection.

Since

Lens Scripting Version 164


normal

readonly normal: vec3

Surface normal on the collider at the point of intersection.

Since

Lens Scripting Version 164


position

readonly position: vec3

Position at the point of intersection.

Since

Lens Scripting Version 164


skipRemaining

skipRemaining: boolean

Set to true to skip remaining results, if any.

Since

Lens Scripting Version 164

Default Value

false

t

readonly t: number

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

Since

Lens Scripting Version 164


triangle

readonly triangle: TriangleHit

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

Since

Lens Scripting Version 181

Methods

getTypeName()

static getTypeName(): TypeName<RayCastHit>

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<RayCastHit>

Since

Lens Scripting Version 164

Overrides

ScriptObject.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

ScriptObject.getTypeName


isOfType()

isOfType(type): boolean

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

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

ScriptObject.isOfType


isSame()

isSame(other): boolean

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

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

ScriptObject.isSame