Tutorial by: ethan


This tutorial is for raycasting, specifically for use with an item like a gun or magic wand. This is my first tutorial so feel free to leave feedback:)

What we are going to be doing is creating a line then drawing along that line with particles and implementing a collision detection for blocks and entities.
First we need a function with a player, a number and another number. The player is where the projectile is based around. The first number is the length of the shot and the second number is the damage this projectile will do.
function raycast(p: player, length: number, dmg: number):

We are going to need a way to test our function for now, so lets make a command.

command /raycast:
    permission: *
    trigger:
        raycast(player, 50, 1)

This draws a line with 50 length and will deal 1 damage.

Next we will need to save every location that is along the path of the projectile at once.

function raycast(p: player, length: number, dmg: number):
    set {_x} to 0
    set {_l} to 0
    loop {_length} times:   #loops as many times as long as the length from the function input
        add 1 to {_x}
        add 0.25 to {_l}
        set {_rg%{_x}%} to location {_l} meters infront of {_p}
        set {_rg%{_x}%} to location 1.25 meters above {_rg%{_x}%} #aligns with where player is looking better
       

If all of the values aren't saved at once and a wait is added, the line can be messed up by the player moving their head as the line is based around where they are looking.
Now that the line is completed, we need a visual for this line.

function raycast(p: player, length: number, dmg: number):
    set {_x} to 0
    set {_l} to 0
    loop {_length} times:   #loops as many times as long as the length from the function input
        add 1 to {_x}
        add 0.25 to {_l}
        set {_rg%{_x}%} to location {_l} meters infront of {_p}
        set {_rg%{_x}%} to location 1.25 meters above {_rg%{_x}%} #aligns with where player is looking better
    # all of the locations are saved as soon as the function is called, effects such as particles can be applied afterwards.
    set {_x} to 0
    loop {_length} times:
        add 1 to {_x}
        wait 1 tick #change this value based on if you want it faster or slower
        show 4 blue dust at {_rg%{_x}%} #change color or particle type

We now have a visual for our line! you can change how fast or slow the line/projectile is drawn based on how long you let it wait inside the loop. The next step is adding collision and damage.

function raycast(p: player, length: number, dmg: number):
    set {_x} to 0
    set {_l} to 0
    loop {_length} times:   #loops as many times as long as the length from the function input
        add 1 to {_x}
        add 0.25 to {_l}
        set {_rg%{_x}%} to location {_l} meters infront of {_p}
        set {_rg%{_x}%} to location 1.25 meters above {_rg%{_x}%} #aligns with where player is looking better
    # all of the locations are saved as soon as the function is called, effects such as particles can be applied afterwards.
    set {_x} to 0
    loop {_length} times:
        add 1 to {_x}
        show 4 blue dust at {_rg%{_x}%} #change color or particle type
        loop entities in radius 0.75 around {_rg%{_x}%}:
            loop-entity is not {_p}
            damage loop-entity by {_dmg}
            stop #if you want your projectile to pierce entities, delete this line
        loop blocks in radius 0.5 around {_rg%{_x}%}:
            if loop-block is not tall grass, short grass, snow layer, air or water:
                stop #projectile does not go through blocks, but passes through expected blocks.

The line is now a projectile that has proper collision with mobs and blocks. Blocks such as grass and snow do not collide.
If you need help on how this could be applied in your own skript, you can create a rifle that shoots a bullet.

on right click:
    if name of player's tool contains "&7Rifle":
        if {reloadrifle.%player's uuid%} is not set:
            set {reloadrifle.%player's uuid%} to 1
            raycast(player, 50, 10)
            wait 1 second
            clear {reloadrifle.%player's uuid%}

And that should be it! A working projectile shooting item! You can change the color of of the dust or change the particle entirely for a different visual effect.
entire skript:

function raycast(p: player, length: number, dmg: number):
    set {_x} to 0
    set {_l} to 0
    loop {_length} times:   #loops as many times as long as the length from the function input
        add 1 to {_x}
        add 0.25 to {_l}
        set {_rg%{_x}%} to location {_l} meters infront of {_p}
        set {_rg%{_x}%} to location 1.25 meters above {_rg%{_x}%} #aligns with where player is looking better
    # all of the locations are saved as soon as the function is called, effects such as particles can be applied afterwards.
    set {_x} to 0
    loop {_length} times:
        add 1 to {_x}
        show 4 blue dust at {_rg%{_x}%} #change color or particle type
        loop entities in radius 1 around {_rg%{_x}%}:
            loop-entity is not {_p}
            damage loop-entity by {_dmg}
            stop #if you want your projectile to pierce entities, delete this line
        loop blocks in radius 0.5 around {_rg%{_x}%}:
            if loop-block is not tall grass, short grass, snow layer, air or water:
                stop #projectile does not go through blocks, but passes through expected blocks 
command /raycast:
    permission: *
    trigger:
        raycast(player, 50, 1)
on right click:
    if name of player's tool contains "&7Rifle":
        if {reloadrifle.%player's uuid%} is not set:
            set {reloadrifle.%player's uuid%} to 1
            raycast(player, 50, 10)
            wait 1 second
            clear {reloadrifle.%player's uuid%}

ty for using my first tutorial


Did you find ethan's tutorial helpful?


You must be logged in to comment

  • Nov. 18, 2022, 4:51 a.m. - inoob24  

    how do i make only one type of item work?

    |     
    July 3, 2023, 12:51 p.m. - Llama-MC  

    use: if player's tool is item
    and with name or/and lore: if player's tool is item named "name" with lore "lore" "lore 2" and "lore 3"

    |     
  • Nov. 26, 2022, 1:11 p.m. - ZenZoyaUDR  

    Thanks, i have been serching for so long to do this :)

    |     
  • Jan. 4, 2023, 8:10 p.m. - DjDisaster  

    vector go brrrr >>

    |     
  • July 8, 2023, 11:50 a.m. - zycong  

    Good tutorial but my ray is going trough every entity whtout damaging them how to fix this?

    |     
  • July 8, 2023, 11:51 a.m. - zycong  

    Good tutorial only my ray is going through every entity how to fix this?

    |     
  • Aug. 13, 2023, 3:33 a.m. - Azureas  

    how do you change the damage?

    |     
    Aug. 13, 2023, 3:37 a.m. - Azureas  

    nvm im stupid

    |     
  • Aug. 13, 2023, 9:23 p.m. - firephrog89  

    doesent work for me

    |     
  • Nov. 3, 2023, 6:02 p.m. - Shando_  

    is it possible to make 2 of those scripts? whenever i make a second skript with raycasting i get an error saying "command /raycasting is already used"

    |     
  • Nov. 28, 2023, 2:48 p.m. - dj_dogos  

    It need function and its not working

    |