I am trying to – as the title suggests – fire two rays from the direction that the player is facing but at a 45 degree angle along the x axis. I have managed to get this to work in some instances but not in others.
I know that as soon as I am told what the answer is I’ll feel like an idiot but this is what I have managed to do so far.
<code>LivingEntity livingEntity = (LivingEntity) event.getPlayer();
World world = livingEntity.getWorld();
Vector lookDir = livingEntity.getEyeLocation().getDirection();
Vector leftDir = lookDir.clone();
Vector rightDir = lookDir.clone();
float hookDirectionFromEyeDirection = 45f / 100f;
if(lookDir.getX() >= 0) {
leftDir.add(new Vector(-hookDirectionFromEyeDirection, 0, 0));
rightDir.add(new Vector(hookDirectionFromEyeDirection, 0, 0));
} else if(lookDir.getX() < 0){
leftDir.add(new Vector(hookDirectionFromEyeDirection, 0, 0));
rightDir.add(new Vector(-hookDirectionFromEyeDirection, 0, 0));
RayTraceResult rayTraceResult = world.rayTraceBlocks(livingEntity.getEyeLocation(), leftDir, MAX_DISTANCE, FluidCollisionMode.NEVER, true);
MyPlugin.getLoggerInstance().info("LookDir: " + lookDir + " | " + leftDir);
if(rayTraceResult != null) {
livingEntity.sendMessage("You just hit a: " + rayTraceResult.getHitBlock().getType());
ArmorStand hook = world.createEntity(rayTraceResult.getHitPosition().toLocation(world), ArmorStand.class);
livingEntity.getWorld().addEntity(hook);
livingEntity.sendMessage("You didn't just hit a block...");
<code>LivingEntity livingEntity = (LivingEntity) event.getPlayer();
World world = livingEntity.getWorld();
Vector lookDir = livingEntity.getEyeLocation().getDirection();
Vector leftDir = lookDir.clone();
Vector rightDir = lookDir.clone();
float hookDirectionFromEyeDirection = 45f / 100f;
if(lookDir.getX() >= 0) {
leftDir.add(new Vector(-hookDirectionFromEyeDirection, 0, 0));
rightDir.add(new Vector(hookDirectionFromEyeDirection, 0, 0));
} else if(lookDir.getX() < 0){
leftDir.add(new Vector(hookDirectionFromEyeDirection, 0, 0));
rightDir.add(new Vector(-hookDirectionFromEyeDirection, 0, 0));
}
RayTraceResult rayTraceResult = world.rayTraceBlocks(livingEntity.getEyeLocation(), leftDir, MAX_DISTANCE, FluidCollisionMode.NEVER, true);
MyPlugin.getLoggerInstance().info("LookDir: " + lookDir + " | " + leftDir);
if(rayTraceResult != null) {
livingEntity.sendMessage("You just hit a: " + rayTraceResult.getHitBlock().getType());
ArmorStand hook = world.createEntity(rayTraceResult.getHitPosition().toLocation(world), ArmorStand.class);
hook.setGravity(false);
livingEntity.getWorld().addEntity(hook);
return;
} else {
livingEntity.sendMessage("You didn't just hit a block...");
return;
}
</code>
LivingEntity livingEntity = (LivingEntity) event.getPlayer();
World world = livingEntity.getWorld();
Vector lookDir = livingEntity.getEyeLocation().getDirection();
Vector leftDir = lookDir.clone();
Vector rightDir = lookDir.clone();
float hookDirectionFromEyeDirection = 45f / 100f;
if(lookDir.getX() >= 0) {
leftDir.add(new Vector(-hookDirectionFromEyeDirection, 0, 0));
rightDir.add(new Vector(hookDirectionFromEyeDirection, 0, 0));
} else if(lookDir.getX() < 0){
leftDir.add(new Vector(hookDirectionFromEyeDirection, 0, 0));
rightDir.add(new Vector(-hookDirectionFromEyeDirection, 0, 0));
}
RayTraceResult rayTraceResult = world.rayTraceBlocks(livingEntity.getEyeLocation(), leftDir, MAX_DISTANCE, FluidCollisionMode.NEVER, true);
MyPlugin.getLoggerInstance().info("LookDir: " + lookDir + " | " + leftDir);
if(rayTraceResult != null) {
livingEntity.sendMessage("You just hit a: " + rayTraceResult.getHitBlock().getType());
ArmorStand hook = world.createEntity(rayTraceResult.getHitPosition().toLocation(world), ArmorStand.class);
hook.setGravity(false);
livingEntity.getWorld().addEntity(hook);
return;
} else {
livingEntity.sendMessage("You didn't just hit a block...");
return;
}
At the moment I am using an Armor Stand to indicate the position that the ray is hitting but for some reason, at certain angles, they are swapping over to the other side of the player.