简体   繁体   English

C# Unity Raycasting / BoxCast / SphereCast 半球形

[英]C# Unity Raycasting / BoxCast / SphereCast in a half sphere shape

播客

The image above shows a boxcast, for what im trying to achieve this wont work.上图显示了一个 boxcast,因为我试图实现这一点是行不通的。 Essentially what would be best is to cast a half sphere or aa quarter sphere like I drew on top of the boxcast screenshot.基本上,最好的方法是像我在 boxcast 屏幕截图上绘制的那样投射半个球体或四分之一球体。

Im trying to make a system for the cannons to aim at enemies and shoot them, but the cannons on the right side of the ship should never try to target enemies on the left side of the ship, hence i figured this is a reasonable approach: boxcast a single side and go from there.我试图为大炮建立一个瞄准敌人并射击他们的系统,但是船右侧的大炮不应该尝试瞄准船左侧的敌人,因此我认为这是一个合理的方法: boxcast 单面和 go 从那里。

Alas, the boxcast is extremely limited for this problem as enemies can easily be outside the box, but still on the right side of the ship.唉,对于这个问题,boxcast 非常有限,因为敌人很容易在盒子外面,但仍然在船的右侧。

Is there a better way to do this?有一个更好的方法吗? The boxcast is also limited in that you can only set 2 dimensions, its cubic size and then its length. boxcast 也受到限制,因为您只能设置 2 个维度,即它的立方体大小,然后是它的长度。 I dont think I can use a SphereCast because that would also target enemies of the left side.我不认为我可以使用 SphereCast,因为那也会瞄准左侧的敌人。 Or is there a way to determine which enemies are in front, which are on the right, which are behind and which are to the left, and put those in Lists (rightEnemiesList, frontEnemiesList, leftEnemiesList, backEnemisList )?或者有没有办法确定哪些敌人在前面,哪些在右边,哪些在后面,哪些在左边,并将它们放在Lists (rightEnemiesList, frontEnemiesList, leftEnemiesList, backEnemisList )?

How do i solve this?我该如何解决这个问题? I dont want to be using 50 line raycasts on each side either, for performance reasons.出于性能原因,我也不想在每一侧使用 50 行光线投射。 Not to mention im actually calculating 4 sides, so we have cannons on each side but also on the front and back of the ship to shoot forwards and backwards.更不用说我实际上计算了 4 面,所以我们在每一面都有大炮,而且在船的前后都有大炮来向前和向后射击。

The code for the boxcast, although it doesnt add much to this question: boxcast 的代码,虽然它并没有给这个问题增加太多:

        var half = ship.mainGunCaliber.range * .5f;
    var rotationY = hullParent.localEulerAngles;
    rotationY = new Vector3(0, rotationY.y, 0);
    Quaternion boxRot = Quaternion.Euler(rotationY);
    RaycastHit[] hits = Physics.BoxCastAll(hullParent.transform.position + hullParent.transform.right,
                                           Vector3.one * ship.length,
                                           hullParent.transform.right,
                                           boxRot,
                                           half);

Thank you!谢谢!

Thanks to Daevin for pointing me in the right direction, This is what I got so far.感谢 Daevin 为我指明了正确的方向,这就是我到目前为止所得到的。 it will add an enemy to the right List according to which direction they are in relation to our ship, Its not perfect because the angle is calculated from the center of the ship.它会根据他们相对于我们船的方向将敌人添加到正确的列表中,它并不完美,因为角度是从船的中心计算的。 which gives some weird results if the targets get too close, Also I have a feeling this isnt the most optimized way to do it, so Please if you have a better way tell us!如果目标太接近,这会产生一些奇怪的结果,而且我觉得这不是最优化的方法,所以如果你有更好的方法,请告诉我们!

foreach (Transform target in enemyTargets)
        {

            if (Vector3.Distance(transform.position, target.position) > ship.mainGunCaliber.range)
                continue;

            Vector3 toTarget = target.position - transform.position;
            print(Vector3.SignedAngle(transform.forward, toTarget, Vector3.up));

            if (Vector3.SignedAngle(transform.forward, toTarget, Vector3.up) >= bowMinAngle &&
                Vector3.SignedAngle(transform.forward, toTarget, Vector3.up) <= bowMaxAngle)
            {
                if (!bowTargets.Contains(target))
                {
                    RemoveFromOthers(target);
                    bowTargets.Add(target);
                    print("added target to Bow");
                }
            }
            if (Vector3.SignedAngle(transform.forward, toTarget, Vector3.up) >= sbMinAngle &&
                Vector3.SignedAngle(transform.forward, toTarget, Vector3.up) <= sbMaxAngle)
            {
                if (!sbTargets.Contains(target))
                {
                    RemoveFromOthers(target);
                    sbTargets.Add(target);
                    print("added target to SB");

                }
            }
            if (Vector3.SignedAngle(-transform.forward, toTarget, Vector3.up) >= aftMinAngle &&
                Vector3.SignedAngle(-transform.forward, toTarget, Vector3.up) <= aftMaxAngle)
            {
                if (!aftTargets.Contains(target))
                {
                    RemoveFromOthers(target);
                    aftTargets.Add(target);
                    print("added target to Aft");

                }
            }
            if (Vector3.SignedAngle(transform.forward, toTarget, Vector3.up) >= psMinAngle &&
                Vector3.SignedAngle(transform.forward, toTarget, Vector3.up) <= psMaxAngle)
            {
                if (!psTargets.Contains(target))
                {
                    RemoveFromOthers(target);
                    psTargets.Add(target);
                    print("added target to PS");

                }
            }

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

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