#How do I raycast?
1 messages · Page 1 of 1 (latest)
I know it can be done with command blocks, but is there an easy way to do it with a script?
player.getEntitiesFromViewDirection()
Do I do a returndata or something?
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
you need to use system.runInterval, you will need to use a for loop to loop through all players
I have that
But how do I make it return the type of entity that the player is looking at?
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]
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
player.getEntitiesFromViewDirection() has an interface to query things.
so...
player.getEntitiesFromViewDirection({ type: 'your:entity' })
Since there is an interface, is there a way to make it run a if statement?
Documentation for @minecraft/server
?
I dont know what that has to do with the interface
Bad image, but better than I can describe it
Like direct line of sight
okay so
instead of using the interface just check it's typeId in an if statement
then use an else statement
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
And it just said hi constantly
oh
Im so confused... Sorry for a lot of questions
^
show me the code
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') } } }); }
you didnt do what I said
I used else
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
if (player.getEntitiesFromViewDirection()) { } The stuff the I removed in the interface stuff, right?
Yes.
And now when you say for each, do you mean something like forEach(({ type: 'test:name' }))
look at what you did for getAllPlayers
do the same thing with getEntitiesFromViewDirection
Okay, Im still confused and stupid when it comes to this
Do I put it in the interface?
...
cause there is no world.getAllEntities
Im not telling you do anything with any interfaces anymore
I told you to remove the interface within the getEntitiesFromViewDirection
And i did
where is this coming from?
I didnt say anything about doing this
You say to you the same thing that I did for getAllPlayers
...
But like, what how tf does that work?
So something like forEach(({ type: 'test:name' }))
no-
Then what do you mean for each
did you code the function up there yourself
Yes
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
I removed the interface stuff in getEntitiesFromViewDirection
yeah but you keep suggesting to put it into forEach
` world.forEach((type: 'test:name' ) => {
})`
Is even near the right thing
I dont if im explaining poorly, if I am, im sorry.
I need to go now
I recommend looking at the api documents and maybe some basic js docs.
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
Documentation for @minecraft/server
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.
@woeful pollen Thanks for the help. My brain turned on for once and I got it working! Thanks man!