#How do I get the position of the block I'm looking at?
1 messages ยท Page 1 of 1 (latest)
const block = player.getBlockFromViewDirection({ maxDistance: 20 })?.block
if (block) {
player.sendMessage(block.x + ' ' + block.y + ' ' + block.z)
}
Use try{} catch{} otherwise will return error if looking in air ("Out of boundaries error")
Uhh no? He already used ?. and he also used if (block) {} too
const head = player.getHeadLocation();
const view = player.getViewDirection();
const block = player.dimension.getBlockFromRay({ ...head, y: head.y + 0.1 }, view, { maxDistance: 20 })?.block;
console.error(JSON.stringify(block?.location));```
Ohh yeah, that method has a small problem... This one right here...
And to get the x, y and z position of an entity I'm looking at, what do I do?
I try with entity but it generates error
const entity = player?.getEntitiesFromViewDirection()[0].entity;
if (entity) {
const { x, y, z } = entity?.location;
}```
๐
That's exactly what I did, but it generates an error
hm?
show code please
?!
what the fuck?
It's quite long, but I'll summarize it
Just a minute
@timber cave
world.afterEvents.itemStartUse.subscribe(data => {
const player = data.source
const item = data.itemStack
const entityInView = player.getEntitiesFromViewDirection({ maxDistance: 60 })[0]
const blockInView = player.getBlockFromViewDirection({ maxDistance: 60 })?.block
if (item.typeId == "sakuta:test") {
const px = player.location.x
const py = player.location.y
const pz = player.location.z
let newDist = 0
if (blockInView != undefined) {
newDist = Math.sqrt(Math.pow(blockInView.x - px, 2) + Math.pow(blockInView.y - py, 2) + Math.pow(blockInView.z - pz, 2))
}
if ((currentDist <= 8 && newDist >= 4) || hasTarget == false) {
if (entityInView == undefined && blockInView == undefined) return
hasTarget = true
if (entityInView) {
player.setDynamicProperty("target_position_x", entityInView.location.x)
player.setDynamicProperty("target_position_y", entityInView.location.y)
player.setDynamicProperty("target_position_z", entityInView.location.z)
} else if (blockInView) {
player.setDynamicProperty("target_position_x", blockInView.x)
player.setDynamicProperty("target_position_y", blockInView.y)
player.setDynamicProperty("target_position_z", blockInView.z)
}
} else {
useTrigger = true
}
}
})
const entityInView = player.getEntitiesFromViewDirection({ maxDistance: 60 })[0]?.entity
I'll test it ๐
Now the code is working perfectly
Thanks
serty is the best
-# || i know ||
lol
||I also know||
By the way, you misspelled "the"
okay
I can find mistakes in others, but I can't even write myself....
well technically, he didnt even spell it