Raycasts

A Raycast is a method of projecting an imaginary line (a "ray) from a specific point in a given direction to detect intersections with objects. You can think of it as a quick way to figure out what object would be "hit" if you fired an infinitely thin laser beam from a certain point in the direction of another point.

If the raycast hit an object, it gives information on the object hit. In HYTOPIA a hit object will be a block or entity.

If the raycast does not hit an object, it will return null.

You can control the length of the cast ray, it's origin and direction, and a number of options to ignore certain intersections, and more.

Basic Raycast Example

Here's some basic code showing how we can perform a raycast. Raycasts are performed from the simulation instance of a world as follows.

const origin = { x: 10, y: 2, z: 0 }; 
const direction = { x: 0, y: 1, z: 0 }; // raycast straight up
const length = 5; // Max length in blocks the ray travels 
const raycastResult = world.simulation.raycast(origin, direction, length);
When debug raycasting is enabled, we can see our raycast in the world. The black arrow point up is the resulting example raycast.

Block Breaking & Placing Example

Let's make something more useful. How about we setup our players so they can use the left click of their mouse to break blocks in front of the direction their facing, and the right click to place a block.

We can do that as follows.

Here's a gif showcasing how our block breaking and placing code using raycasts works!

Diving Deeper

Raycasts make use of a few systems. To learn more and understand all of the features of raycasts, we recommend the following resources.

If there are features that we don't currently support for raycasts that you'd like to see added to the HYTOPIA SDK, you can submit a feature request here.

Last updated