Posted April 2, 20241 yr I created an item that i can use to check if the player can see the center of a specific block like this: public static boolean lineOfSight(int maxRange, Entity player, BlockPos pos) { Vec3 vec3 = player.getEyePosition(); Vec3 vec31 = pos.getCenter(); if (vec31.distanceTo(vec3) > maxRange) { return false; } else { BlockHitResult result = player.level().clip(new ClipContext(vec3, vec31, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, player)); return result.getBlockPos().getCenter().equals(pos.getCenter()); } } This works as far as i need it. Now i wanted to create a block that can do the same. In this case: If the block can see the center of a specific block. I tried it in a similar way: public static boolean lineOfSight(int maxRange, Level pLevel, Vec3 posFrom, BlockPos posTo) { Vec3 vec31 = posTo.getCenter(); if (vec31.distanceTo(posFrom) > maxRange) { return false; } else { BlockHitResult result = pLevel.clip(new ClipContext(posFrom, vec31, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, null)); return result.getBlockPos().getCenter().equals(posTo.getCenter()); } } But obviously this does not work, because this method always returns false. The explanation is simple: The block only can see itself, because i raycast/clip from inside of my block. Is there a simple way to use clip and skip this initial block where the raycast is coming from?
April 8, 20241 yr I'd advise moving the starting position to just outside the block in the direction of the target position.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.