#how to raycast

1 messages · Page 1 of 1 (latest)

frozen pecan
#

I am trying to restart my 3x3 mining pickaxe goal for which I got to know that I need raycasting which I know nothin about

umbral acornBOT
#

<@&1201956957406109788>

Someone will come and help soon!

💬 While you wait, take this time to provide more context and details.

                        🙇 After a while, hit the Summon Helpers button to ping the helper team. They'll be happy to help you


                        
                        ✅ Once your question has been resolved (or you no longer need it), please click Resolve Question or run /resolve
frozen pecan
#

the raycasting is for detecting blocks mineable by the (netherite) pickaxe as it can only mine blocks in the pickaxe_mineable tag

wicked shadow
#

soo

#

basically

#

step first

#

type /method raycast

#

second

#

read

elder jewelBOT
#
⚙️Raycast

If you've not made many datapacks, you might not be familiar with raycasting. It's a really useful tool which we can use to essentially find where the player is looking and then do stuff (such as spawn explosions 😎).

To create a raycast, we can use a recursive function. A recursive function is a function which will run itself over and over again. In our case, the function will run itself 0.1 blocks forward (^ ^ ^0.1) each time, and repeat until it hits something. It will do this all within the same tick - but bear in mind that the bigger the distance, the more laggy it will be. We can stop this unwanted behaviour by putting a distance limit on the raycast - each time it runs itself, it will count down on a scoreboard, and only repeat if it hasn't hit the limit.

start_raycast function:

# Set the distance limit on the raycast. (10 x limit in blocks, so 1000 would be 100 blocks)
scoreboard players set .limit <any objective> 1000

# Start the raycast
execute at @s anchored eyes positioned ^ ^ ^.1 run function <namespace>:raycast

raycast function:

# Remove one from the limit
scoreboard players remove .limit <objective> 1

# Optional - place a particle, to make the raycast leave a trail
particle minecraft:dust 1 0 0 1 ~ ~ ~

# If the raycast has hit a block, do something
execute unless block ~ ~ ~ #<namespace>:pass_through run setblock ~ ~ ~ diamond_block

# If the raycast hasn't hit a block, continue, but only if the limit is 1 or more (1..)
execute if block ~ ~ ~ #<namespace>:pass_through positioned ^ ^ ^0.1 if score .limit <objective> matches 1.. run function <namespace>:raycast```

namespace/tags/blocks/**pass_through**.json:```
{
    "replace": false,
    "values": [
        "minecraft:air",
        "minecraft:cave_air",
        "minecraft:void_air"
    ]
}

This should be it! Make sure you add all the functions, and fill in all the blanks (indicated by <this>). Then, you can run the start_raycast function as a player, and it should work.

wicked shadow
#

look

#

there it is

frozen pecan
#

oh

#

thank you

wicked shadow
frozen pecan
#

ok

#

what do you mean by: 0.1 block forward" ?

#

@wicked shadow

wicked shadow
#

literally 0.1 blocks forward

frozen pecan
#

so it runs every tick?

wicked shadow
#

raycast is essentially like a beam that first checks current position whether its a block, if its not it will move forward on 0.1 blocks it will repeat that over and over until it reaches a block and then you type your code like destroy the block or whatever

wicked shadow
frozen pecan
#

woow

#

wait so can I use raycasting for a pickaxe eto detect blocks

wicked shadow
#

"eto" ?

frozen pecan
#

like if the player is looking at a block, it signals the pickaxe if it is mineable with a tag from where the raycasting runs

#

am I right?

frozen pecan
frozen pecan
#

so can it sense blocks around that

wicked shadow
#

around what

frozen pecan
#

as in the player looks at the centre block of the 3x3, how does it detect remaining blocks under the mineable tag

wicked shadow
#

how?

wicked shadow
#

ig

frozen pecan
#

oh

#

so I gotta setup 8 predicates linked to the raycast

#

so let me get it straight, If the block looking at by the player is under the tag, it runs the raycast which results in 8 more predicates and if not under the tag, doesn't at all.

#

lemme quickly go through the wiki to actually learn to input commands or functions

#

@wicked shadow can you suggest a tutorial or site to learn coding

frozen pecan
#

well it has to detect blocks in every single direction to the mined block so actually more than 8

#

so more like 27 predicates cuz a 3x3 miner mines in a cube of 3x3x3 so 27

#

am I correct? @wicked shadow

wicked shadow
#

hell no

#

0 predicates

#

tf you mean

frozen pecan
#

huh

wicked shadow
#

where you get these numbers from

frozen pecan
#

my small brain

wicked shadow
#

execute if block

#

24 times actually

#

or maybe 27 if you have that big

frozen pecan
wicked shadow
# frozen pecan how?

yea well usually when people mean 3x3 pickaxe it mines a 3x3x2 space 99% of the time, but if yours is 3x3x3 then ig yea its 27 blocks

frozen pecan
#

yeah you're right cuz of air blocks

frozen pecan
wicked shadow
#

you never used relative coordinates

#

?

#

i am pretty sure there is a video on relative coordinates on youtube

frozen pecan
#

ok

frozen pecan
#

yeh I m not getting how to code it

wicked shadow
frozen pecan
#

give me a min

#

@wicked shadow how do I code it in?

#

I know nothin about coding

wicked shadow
#

just code the raycast first

#

divide your main goal into small tasks

frozen pecan
#

ok

wicked shadow
#

so its manageable

#

1st raycast
2nd block detection

frozen pecan
#

kk

frozen pecan
wicked shadow
wicked shadow
#

dont ask too many questions, sooner you get to work - the sooner you will realise

#

raycast as i explained is a beam, and block detection is... well a block detection

frozen pecan
#

kk

frozen pecan
#

so what now @wicked shadow

wicked shadow
#

fr?

#

so fast?

frozen pecan
#

mhm

wicked shadow
#

what did you test it on

frozen pecan
#

wdym

wicked shadow
#

like

#

what did you use to test it

#

you shoot a block

#

or entity

#

or what

frozen pecan
#

block

#

just copied the code

#

like you told

frozen pecan
wicked shadow
#

i never in my life told you to copy nothing

#

i said

#

do exactly like here

wicked shadow
#

you are implying i told you?

#

when?

frozen pecan
#

you just told me to copy what's written in the tutoril

#

tutorial*

wicked shadow
#

i never

#

told you that

frozen pecan
#

bruh ok

#

so let me reset it

wicked shadow
#

i said
"do exactly like here"

frozen pecan
#

ahh ok

#

I misunderstood it

#

my bad

wicked shadow
#

okk so you shoot a block and then what happens

frozen pecan
#

I am lost

wicked shadow
#

so you said raycast is working right

#

you tested it on a block

frozen pecan
#

I did not say tested

#

rather done codign it

#

coding*

wicked shadow
frozen pecan
#

give me a minute

wicked shadow
#

here you agreed that you tested it on a block

frozen pecan
#

uhm no

wicked shadow
frozen pecan
#

sorry

wicked shadow
#

no need to sorry

#

i am sorry for making you feel like you should apologize lmao

#

you wanna vc rn

frozen pecan
#

online to be exact

#

uhh @wicked shadow

#

can you please tell me my mistake in the code

#

it ain't detecting

wicked shadow
wicked shadow
#

or just copy the code

frozen pecan
#

kk

frozen pecan
wicked shadow
#

oh alright

frozen pecan
#

here

#

but the thing I don't get is how does the raycast link up with the block detection?

wicked shadow
#

what in the who

#

did you make this by tutorial?

frozen pecan
#

mhm

#

some minor adjustments but yep, it's from a tutorial

#

It detects an entity

wicked shadow
frozen pecan
#

no

#

I just went through the code and understood that it detects an creeper

#

but it did not load in for me

#

it didn't even detect my datpack

wicked shadow
#

ohhhhhhhhhh

#

let me open up my neovim..

frozen pecan
#

k

wicked shadow
#

who tf made the tutorial

#

@deep pelican can you ask ppl to replace current raycast tutorial with a better one? maybe i will make a pr myself

p.s. i am sorry for ping, not relevant anymore

frozen pecan
wicked shadow
#

oh bro

frozen pecan
#

oh

wicked shadow
frozen pecan
#

2 yrs

wicked shadow
#

bro

#

that raycast

frozen pecan
wicked shadow
#

is bad

frozen pecan
#

yeh 2 yrs old too

#

imma get to that 10-15minutes, I am getting a call, excuse me please

wicked shadow
#

no prob

frozen pecan
#

back

#

any fixes?

wicked shadow
#

@frozen pecan what do you mean not loading up

wicked shadow
#

@frozen pecan whats not loading up

frozen pecan
#

sorry, the datapack is not being recognized @wicked shadow

wicked shadow
wicked shadow
#

@frozen pecan

frozen pecan
#

KK

wicked shadow
#

silabear set offline status

frozen pecan
wicked shadow
#

whats this

#

@frozen pecan send content of your datapacks folder

#

as a screenshot

frozen pecan
#

ah

#

@wicked shadow

craggy obsidian
#

I think they meant the datapacks folder of your world

#

Where that data pack is placed into

#

Also - if you /datapack list - does it show up?

wicked shadow
#

i am pretty damn sure your datapack is inside another folder and thats why it doesnt show up

#

because why elsse woudl there be extra folder in the zip you sent me

craggy obsidian
#

To be recognized and loaded, just the pack.mcmeta needs to be there and in the correct format

wicked shadow
#

it is

#

datapck is perfect, its just in the wrong location

frozen pecan
#

ah yes

#

got it

wicked shadow
frozen pecan
#

gimme a sec

#

my pc is dying

wicked shadow
#

is dead right

frozen pecan
#

I m panicking rn

wicked shadow
upper gorge
#

and odd…

#

sorry idk why that might be 😭

frozen pecan
#

yr

#

@wicked shadow I fixed it

#

I add to close bacground applications in task manager, my cpu as at 118% lol

#

lemme try it out

wicked shadow
#

@frozen pecan down to vc like in 3 hours

frozen pecan
#

I don think I can

#

It's like 7AM rn and I got school by 8:30-9 ish

#

@wicked shadow

#

the pack ain't working for some reason

#

it should send out a red beam wherever I looked and turn every block I run into to a diamond

wicked shadow
#

bruh

#

does it still not show up ?

#

the folder structure should look like this

#

.minecraft/saves/YOUR WORLD/datapacks/raycasting/pack.mcmeta and it should show up

#

i am pretty sure what you have looks more like
.minecraft/saves/YOUR WORLD/datapacks/raycasting/raycasting/pack.mcmeta

#

because thats how you sent it here

deep pelican
frozen pecan
#

uhh

frozen pecan
frozen pecan
wicked shadow
wicked shadow
unique pelican
# frozen pecan wdym

If you never run the command to initialise the scoreboard, it won't exist and therefore won't store any values

wicked shadow
#

ohhh

#

why didnt you tell me

#

💀

frozen pecan
wicked shadow
unique pelican
frozen pecan
#

I should've said that, mb

wicked shadow
#

yea so ig just create the scoreboard in the load function

frozen pecan
#

nope

#

ight

#

@wicked shadow

unique pelican
#

You need to put that in the file data/ray_demo/functions/load.mcfunction

frozen pecan
#

Oh bruh

#

I kept it in load.json

unique pelican
#

load.json tells the game which functions to run on load, the commands then need to go in the functions themselves

frozen pecan
#

ohk

deep pelican
#

You also need to create data/ray_demo/functions/load.mcfunction first

#

Then in that mcfunction you put the scoreboard command

wicked shadow
frozen pecan
#

OK

frozen pecan
#

so now what? @deep pelican

unique pelican
#

Does it work now?

frozen pecan
#

nope

unique pelican
#

Lemme take a look at the files for a second, then I'll try and figure out what's wrong

frozen pecan
#

kk

unique pelican
#

So does the function not run at all?

frozen pecan
#

no

#

it should replace a block I hit with a diamond block I assume but doesn;t do it

unique pelican
#

Do you correctly run the start_raycast function? Like what prints in chat when you run the command

frozen pecan
#

it runs nothin

unique pelican
#

/function ray_demo:raycast_start

frozen pecan
#

ok

unique pelican
#

That means the function hasn't loaded properly

#

Show me the chat output of /datapack list

#

I have to go now, I'll be back later. Maybe someone else can help you in the mean time

frozen pecan
#

k

#

uhh @wicked shadow please help

unique pelican
# frozen pecan

Looks like your datapack is disabled. Run /datapack enable raycasting

frozen pecan
#

ok

unique pelican
#

Now try the raycast function again

frozen pecan
#

k

wicked shadow
#

oh yea

frozen pecan
#

done*

unique pelican
frozen pecan
#

it works

#

It places a diamond block herever I look

unique pelican
#

ok cool

frozen pecan
#

soo uhm how doesit help me make a3x3mining pickaxe?

#

@wicked shadow so what do I do from here?

wicked shadow
#

welp

#

now you test it

frozen pecan
#

did

#

it works

wicked shadow
#

NICE

#

FINALLY

#

HOLY CRAP

unique pelican
#

Basically, you want to detect the player using the pickaxe, then run a raycast to find the block they broke, and then destroy the 3 blocks around it

wicked shadow
#

NO WAY

#

LETS GO

frozen pecan
unique pelican
wicked shadow
#

pls now ask help from the neovim guy

#

im playing valorant

frozen pecan
frozen pecan
unique pelican
#

The only problem is, the block they just broke is now broken

#

Which means you can't detect it with raycast

frozen pecan
#

hmm

#

so block detector

unique pelican
#

So there's probably two ways that you could do this

frozen pecan
#

hmm

unique pelican
#

One is to look for an item that is newly created, and assume that it came from the block they broke

#

The other is to check what block they are looking at each tick when holding that pickaxe, and then you know which block it is when they use it

frozen pecan
#

hmm

unique pelican
#

Those are the two that I can think of, anyway

unique pelican
#

But I haven't really made something like this before, so maybe someone else has a better way

#

Sorry, I have to go now. I can help you later

frozen pecan
#

np

frozen pecan
#

umm

#

help

umbral acornBOT
# umbral acorn <@&1201956957406109788>

<@&935561184806060073> <@&1202694677766348840>

**🙇 Helpers Arise!**

Please note that you still might not immediately get a response since all helpers are human beings and volunteers (and also might be sleeping right now)

fiery stag
#

isnt this resolved already

wicked shadow
fiery stag
#

whats the problem then

#

all i see is how to raycast

swift pike
frozen pecan
swift pike
#

what do you mean

#

do you mean blocks around it should only break if the correct tool is used?

frozen pecan
#

so umm how do I implement the code? @swift pike

swift pike
#

you need to init a scoreboard that tracks when the player uses his pickaxe

#

if they have 1 or more score they'll get it removed and the adjacent blocks will get broken (if they are a part of #minecraft:mineable/pickaxe)

#

and there loot table will be dropped on the floor

frozen pecan
#

uhm

#

I am new to coding @swift pike

swift pike
#

ohh wait i'm dumb

#

that doesn't work

#

cuz you don't have the block coords

frozen pecan
#

mhm the hitboxes

#

so I'd need the relative coords?

swift pike
#

you'd need the block coords of the recently mined block

#

raycast can't get you that info (unless you do it every tick)

frozen pecan
#

hmm

#

So how do I do it

frozen pecan
#

so I need to setup block detection now right?

#

...

swift pike
frozen pecan
#

so how do I get the coordinates of the block? @swift pike

swift pike
#

The only solution I can think of is to store the targeted block of the last tick somewhere

#

but that'll be really slow

frozen pecan
#

that would lag the gaem

#

game*

swift pike
#

on multiplayer, defintly

frozen pecan
#

this seems more complicated than I thought

swift pike
#

on singlplayer, maybe okay

frozen pecan
swift pike
#

why don't you consider making a plugin instead

frozen pecan
#

what do you mean?

swift pike
#

a plugin

frozen pecan
#

how would it help?

swift pike
#

is something you put on a server

#

you code it with an api made to code

#

not to debug

frozen pecan
#

oh

#

I use aternos for hosting which doesn't allow to import plugins

#

I think I have to give a pause to this idea

#

and work on other concepts

#

I think I'll work on some basic things before reaching such stages like this

#

Time to close this ticket and open a different one

#

Thanks for the help!!

umbral acornBOT
#
Question Closed

Your question, #1204314612095524864 (how to raycast), was resolved!

Original Message

I am trying to restart my 3x3 mining pickaxe goal for which I got to know that I need raycasting which I know nothin about

Duration open

1d5h37m

umbral acornBOT
#
🗑️ Recycling Thread

This thread has been inactive for some time, so I'm going to archive it.

If you're still using the thread, just send a message and it'll pop back on the thread list.