DOCS

How to detect when an entity is clicked on?

https://docs.3dverse.com/sdk/SDK3DVerse.engineAPI.html#castScreenSpaceRay

Add an event listener to the canvas element

1
2
const canvas = document.getElementById("display-canvas");
canvas.addEventListener("click", (event) => detectEntityOnClick (event));

Implement the event handler function

1
2
3
4
5
6
const detectEntityOnClick = async (event) => {
const target = await SDK3DVerse.engineAPI.castScreenSpaceRay(event.clientX, event.clientY);
if (!target.pickedPosition) return;
const entity = target.entity;
console.log(entity);
};
Previous
Toggle an entity's visibility