Questions about triggering raycast events

I think this is specifically a logic question in JS, I’m coming from OOP languages and I need help understanding how to handle events when raycastring / interacting with objects. Looking at all the examples of raycasting they are just changing the transforms of the object and don’t offer a solution to my question.

        if (hit.pickedMesh){
		   hit.originMesh.scaling.y += 0.01;
	    }

How do I fire off an event if a player was to raycast something and press E (say open a door) do I get a component on the door then hit.object.OpenDoor()? Or get the doors hit.name and preform some logic with the object name if(hit.name == "door1)OpenDoor()? This example can be used for all my questions (picking up objects in inventory, triggering events: timers, quick time events).

I might use a collection of object.ids of the types of things you can hit (eg. one for doors, one for things you can pick up). You cast a ray in the scene and if the picked mesh has an id that exists in the collection then do the action for that group. But any method you described in your question will likely work just as well and its up to you on what you prefer.

1 Like

Thanks so much quick question, how could I return a property from a hit object or fire an event on it?

Something like:
hit.object.fireEvent(SomeEvent)?

Would this work?

https://playground.babylonjs.com/#RTUDZD

1 Like

Thank you, this event listener (I’m assuming that’s what JS calls them and correct me if I’m wrong) is exactly what I was looking for!

1 Like

Yea they are observables Handle Events with Observables - Babylon.js Documentation