简体   繁体   English

我如何获得玩家的视线

[英]How do i get a players line of sight

Im trying to get the Line of sight of a Player and set the block at the end to ice, but whenn i get the Vector of the line of sight it Counts the air as a block and sets it to ice how can i exclude the air?我试图获得玩家的视线并将最后的方块设置为冰,但是当我得到视线的矢量时,它会将空气视为一个方块并将其设置为冰我如何排除空气? (Sorry for bad english xD) (抱歉英语不好xD)

Here is my Code这是我的代码

Vector lineOfSight = player.getEyeLocation().getDirection().normalize();
Block block = player.getLocation().add(lineOfSight).getBlock();
block.setType(Material.BLUE_ICE);

I'm not sure whether or not that code is in a loop, but you could try the following:我不确定该代码是否处于循环中,但您可以尝试以下操作:

if(!block.getType().equals(Material.AIR)) // I am not sure about the actual enum declaration of air, but your IDE will suggest it.
    block.setType(Material.BLUE_ICE);

Assuming that you don't want the line of sight check to run indefinitely, here's how you could do it with a maximum distance - you could change this to a while loop, but I wouldn't advise it.假设您不希望视线检查无限期地运行,以下是您如何以最大距离进行检查 - 您可以将其更改为 while 循环,但我不建议这样做。

Vector lineOfSight = player.getEyeLocation().getDirection().normalize();
double maxDistance = 40;
Block finalBlock = null;
for(double i = 0; i < maxDistance; ++i){
    Block block = player.getEyeLocation().add(lineOfSight.clone().multiply(i)).getBlock();
    if(!block.getType().isSolid())
        continue;
    finalBlock = block;
    break;
}
if(finalBlock != null)
    finalBlock.setType(Material.ICE);

Simply, this will iterate along the players line of sight up until the specified distance.简单地说,这将沿着玩家的视线迭代直到指定的距离。 If a solid block is found at any point, the loop breaks and the block is set to ICE .如果在任何点找到一个实心块,则循环中断并将该块设置为ICE You can change the isSolid() condition to check specifically for AIR if you wish, as this will not count;如果您愿意,您可以更改isSolid()条件以专门检查AIR ,因为这不会计算在内; for example, WATER as solid.例如, WATER是固体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM