Function
raycast
This function sends out a ray from a specified origin to detect target blocks within a given distance.
Syntax
Parameters
| Parameter | Description | Type | Default |
|---|---|---|---|
| shooter | The origin of the ray. Accepts selectors like @a, @s, or a player's name. | String | - |
| block | The target block(s) to detect. Can be a block ID (e.g., 'minecraft:stone') or a block tag. | String | - |
| step | Controls the ray's precision (smaller values provide finer detection). | Number | - |
| distance | Maximum ray length in blocks. | Number | - |
| callback | Minecraft command(s) to execute upon hitting the target block(s). | String | - |
Example
This example sends a ray from the executing player to detect stone blocks within 10 blocks distance. When a stone block is detected, it's replaced with a gold block.
Usage with onPlaceBlock
The raycast function is commonly used with @onPlaceBlock to detect and modify blocks that players have just placed:
In these examples, when a player places black wool or sand, the raycast function finds that block and transforms it into iron or gold.