#How do I raycast?

1 messages · Page 1 of 1 (latest)

grand vapor
#

I asked this in a different way ealier because I didnt know what it was called. I need the raycast to detect if the player is looking at an entity. Should I do commands or script?

#

I know it can be done with command blocks, but is there an easy way to do it with a script?

woeful pollen
grand vapor
woeful pollen
grand vapor
# woeful pollen what

Sorry, im new to scripting. Ive been doing command blocks for a long time, but not this. How do I get the entity type that the player is looking at

#

world.getAllPlayers().forEach((player) => { if (player.hasTag("Player")) { player.getEntitiesFromViewDirection() }

#

That is what I have, but it does nothing yet

woeful pollen
grand vapor
#

I have that

woeful pollen
#

forEach

#

okay, that works too

grand vapor
#

But how do I make it return the type of entity that the player is looking at?

woeful pollen
#

this returns an array too, so you could use forEach for the entities aswell.

#

or you could just index it, by getting the first entity in the array.
player.getEntitiesFromViewDirection()[0]

grand vapor
#

I see what you are doing, and it kinda works. But lets say that I need the game to test if the player is looking at an entity with the identifier of test:name, how do I make it look for that specific entity, and then run something like a function if it does?

#

Sorry for the butt load of questions btw, Ive been researching raycasting, but really found nothing

woeful pollen
#

player.getEntitiesFromViewDirection() has an interface to query things.

#

so...

#
player.getEntitiesFromViewDirection({ type: 'your:entity' })
grand vapor
#

Since there is an interface, is there a way to make it run a if statement?

woeful pollen
#

I dont know what that has to do with the interface

grand vapor
#

Like direct line of sight

woeful pollen
#

okay so

#

instead of using the interface just check it's typeId in an if statement

#

then use an else statement

grand vapor
#

Ima try something real fast

#

function raycast () { world.getAllPlayers().forEach((player) => { if (player.hasTag("Player")) { if (player.getEntitiesFromViewDirection({ type: 'test:name' })) { player.runCommandAsync('say hi') } } }); }

#

That is what I did

woeful pollen
#

I was going to say

#

you could probably do that

grand vapor
#

And it just said hi constantly

woeful pollen
#

oh

grand vapor
#

Im so confused... Sorry for a lot of questions

woeful pollen
grand vapor
#

Just did else command

#

Nothing changed

woeful pollen
#

show me the code

grand vapor
#

function raycast () { world.getAllPlayers().forEach((player) => { if (player.hasTag("Player")) { if (player.getEntitiesFromViewDirection({ type: 'test:name' })) { player.runCommandAsync('say hi') } else { player.runCommandAsync('say no') } } }); }

woeful pollen
#

you didnt do what I said

grand vapor
#

I used else

woeful pollen
#

thats not all I said

#

lmao

#

use forEach for the entities aswell

#

then make an if statement checking its typeId

#

then use an else statement

#

and remove the interface stuff

grand vapor
#

if (player.getEntitiesFromViewDirection()) { } The stuff the I removed in the interface stuff, right?

woeful pollen
#

Yes.

grand vapor
#

And now when you say for each, do you mean something like forEach(({ type: 'test:name' }))

woeful pollen
#

look at what you did for getAllPlayers

#

do the same thing with getEntitiesFromViewDirection

grand vapor
#

Okay, Im still confused and stupid when it comes to this

#

Do I put it in the interface?

woeful pollen
#

...

grand vapor
#

cause there is no world.getAllEntities

woeful pollen
#

Im not telling you do anything with any interfaces anymore

#

I told you to remove the interface within the getEntitiesFromViewDirection

grand vapor
#

And i did

woeful pollen
#

I didnt say anything about doing this

grand vapor
#

You say to you the same thing that I did for getAllPlayers

woeful pollen
#

...

grand vapor
#

But like, what how tf does that work?

woeful pollen
#

forEach

#

thats what you used

grand vapor
#

So something like forEach(({ type: 'test:name' }))

woeful pollen
#

no-

grand vapor
#

Then what do you mean for each

woeful pollen
#

did you code the function up there yourself

grand vapor
#

Yes

woeful pollen
#

I dont understand how you dont know what I mean then

#

putting the interface from getEntitiesFromViewDirection into forEach does not make sense

#

and also I said we are not going to be working with interfaces in general so remove it

grand vapor
#

I removed the interface stuff in getEntitiesFromViewDirection

woeful pollen
#

yeah but you keep suggesting to put it into forEach

grand vapor
#

` world.forEach((type: 'test:name' ) => {

        })` 

Is even near the right thing

woeful pollen
#

I dont if im explaining poorly, if I am, im sorry.
I need to go now

grand vapor
#

Alr

#

Thanks for trying

#

Sorry

woeful pollen
#

I recommend looking at the api documents and maybe some basic js docs.

grand vapor
#

Alr

woeful pollen
# grand vapor Alr

here's the site ive been using often for minecraft's script api: https://stirante.com/script/server/1.13.0/
you could look at this for basic js: https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics

MDN Web Docs

JavaScript is a programming language that adds interactivity to your website. This happens in games, in the behavior of responses when buttons are pressed or with data entry on forms; with dynamic styling; with animation, etc.
This article helps you get started with JavaScript and furthers your understanding of what is possible.

grand vapor
#

Ill look at it

#

Thanks

grand vapor
#

@woeful pollen Thanks for the help. My brain turned on for once and I got it working! Thanks man!