Skip to content

Instantly share code, notes, and snippets.

@agrancini-sc
Last active April 25, 2025 05:14
Show Gist options
  • Save agrancini-sc/db2023f503918812f1947f1fa319ec4e to your computer and use it in GitHub Desktop.
Save agrancini-sc/db2023f503918812f1947f1fa319ec4e to your computer and use it in GitHub Desktop.
SimpleRaycast
This bypasses the interactable system
let me know what you think
remember that you need to set up the ray start and end
you can create 2 scene objects and put them under the main camera
1. at 0,0,0
2. at 0,0,-100 (forward)
then assign a 3rd object that is the one that is instantiated on the hitPoint of the raycast
remember that the geometry receiving the raycast needs to have a collider and a physics body
if you want the receiver to not be dynamic uncheck the dynamic option so it won' react to physics
remember that if you want to impact any world surface use the worldQueryModule in the AssetLibrary
this below is a simplify approach without interactors/worldQueryModule
// Given an object with a BodyComponent
// If the object intersects with the ray described
// by rayStart and rayEnd, print a message.
@component
export class SimpleRaycast extends BaseScriptComponent {
@input rayStart: SceneObject;
@input rayEnd: SceneObject;
@input trail: SceneObject;
onAwake() {
// Check if trail is defined
print("Trail object defined: " + (this.trail !== undefined));
this.createEvent("OnStartEvent").bind(() => {
this.onStart();
});
this.createEvent("UpdateEvent").bind(() => {
this.updateObjectMovement();
});
}
onStart() {
// Check if trail is defined at start
print("Trail object at start: " + (this.trail !== undefined));
if (this.trail) {
print("Trail object name: " + this.trail.name);
}
}
updateObjectMovement() {
// Create a probe to raycast through all worlds.
var globalProbe = Physics.createGlobalProbe();
// Check if trail is defined before raycasting
print("Trail object before raycast: " + (this.trail !== undefined));
// Store 'this' reference to use inside the callback
const self = this;
globalProbe.rayCast(this.rayStart.getTransform().getWorldPosition(),
this.rayEnd.getTransform().getWorldPosition(), function (hit) {
if (hit) {
var position = hit.position;
print("Raycast hit: " + hit.collider.getSceneObject().name);
// Add safety check for trail
if (self.trail) {
print("Trail exists in callback, setting position");
self.trail.getTransform().setWorldPosition(position);
} else {
print("ERROR: Trail is undefined in callback");
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment