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