避免光线投射击中没有图层蒙版的玩家 [英] Avoid raycast hitting player without layermask

查看:23
本文介绍了避免光线投射击中没有图层蒙版的玩家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一款 FPS 游戏,玩家可以朝自己瞄准的方向射击.如果我添加一个图层蒙版,它是固定的,但由于我计划制作多人游戏,每个玩家都会有相同的图层蒙版.如果可能的话,我宁愿不向其他玩家添加另一个图层蒙版.在不使用图层蒙版的情况下,是否有其他方法可以避免玩家射击自己?或者有什么实际方法可以让其他玩家拥有单独的图层蒙版?

I'm making a FPS game, and the player can shoot himself it he aims down. If I add a layermask, it's fixed, but since I plan on making this game multiplayer, every player would have that same layermask. I'd rather not add yet another layermask to other players, if that's possible. Is there some other way to avoid the player shooting himself, withot using layermasks? Or is there an actual way to make other players have a seperate layermask?

推荐答案

或者有什么实际方法可以让其他玩家拥有单独的图层蒙版?

Or is there an actual way to make other players have a seperate layermask?

由于需要事先定义并且限制为32层(8已经由 Unity 预定义)此解决方案是可行的,是的,但可扩展性不高,并且取决于应用其余部分的复杂性非常有限.

As Layers need to be defined beforehand and are limited to 32 layers (8 already predefined by Unity) this solution would be feasible to implement, yes, but not be very scalable and depending to the complexity of the rest of your app very limited.

您可以使用 RaycastAll,过滤out if(hit != player) 例如使用 Linq Where,排序通过距离,例如使用 Linq OrderBy 然后使用结果中的第一个命中.

You could use RaycastAll, filter out if(hit != player) e.g. using Linq Where, order by the distance e.g. using Linq OrderBy and then use the first hit from the results.

有点像

// Wherever you get these from
Ray ray; // the ray with origin amd direction for the cast
float distance; // if needed a maximum distance
LayerMask layers; // the general raycast layer - not different ones for each player ;)
GameObject player; // your player object


var hits = Physics.RaycastAll(ray, distance, layers);

if(hits.Length > 0)
{
    // Filter out e.g. using Linq
    hits = hits.Where(hit => hit.gameObject != player).ToArray();

    if(hits.Length > 1)
    {
        // Sort by distance from ray origin
        hits = hits.OrderBy(hit => hit.distance).ToArray();
    }

    // Finally get first hit
    var hit = hits[0];

    // ...
}

这篇关于避免光线投射击中没有图层蒙版的玩家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆