My App
Function

raycast

This function sends out a ray from a specified origin to detect target blocks within a given distance.

Syntax

raycast(shooter, block, step, distance, callback)

Parameters

ParameterDescriptionTypeDefault
shooterThe origin of the ray. Accepts selectors like @a, @s, or a player's name.String-
blockThe target block(s) to detect. Can be a block ID (e.g., 'minecraft:stone') or a block tag.String-
stepControls the ray's precision (smaller values provide finer detection).Number-
distanceMaximum ray length in blocks.Number-
callbackMinecraft command(s) to execute upon hitting the target block(s).String-

Example

raycast("@s", "minecraft:stone", 0.01, 10, "setblock ~ ~ ~ minecraft:gold_block")

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:

@onPlaceBlock("minecraft:black_wool")
func onBlackWool {
    tellraw @a [{"selector":"@s"},{"text":" just placed a black wool."}]
    raycast("@s", "minecraft:black_wool", 0.01, 10, "setblock ~ ~ ~ iron_block")
}

@onPlaceBlock("minecraft:sand")
func onSand {
    tellraw @a [{"selector":"@s"},{"text":" just placed a sand."}]
    raycast("@s", "minecraft:sand", 0.01, 10, "setblock ~ ~ ~ gold_block")
}

In these examples, when a player places black wool or sand, the raycast function finds that block and transforms it into iron or gold.

On this page