Jump to content

Raycast / Clip between two blocks


Recommended Posts

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; }
            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; }
            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?

Link to comment
Share on other sites

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.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.