User Tools

Site Tools


tutorial:pixel_raycast

This is an old revision of the document!


Pixel raycast

Suppose you want to know what block or entity corresponds to a pixel on the screen. This can be done with pixel raycast.

All of this is client side.

There are two types, center pixel (crosshair) and arbitrary pixel.

Special case: Center pixel

This can be done with:

  1. MinecraftClient client = MinecraftClient.getInstance();
  2. HitResult hit = client.crosshairTarget;
  3.  
  4. switch(hit.getType()) {
  5. case Type.MISS:
  6. //nothing near enough
  7. break;
  8. case Type.BLOCK:
  9. BlockHitResult blockHit = (BlockHitResult) hit;
  10. BlockPos blockPos = blockHit.getBlockPos();
  11. BlockState blockState = client.world.getBlockState(blockPos);
  12. Block block = blockState.getBlock();
  13. break;
  14. case Type.ENTITY:
  15. EntityHitResult entityHit = (EntityHitResult) hit;
  16. Entity entity = entityHit.getEntity();
  17. break;
  18. }
tutorial/pixel_raycast.1597423572.txt.gz · Last modified: 2020/08/14 16:46 by emmanuelmess