unity3d 多对象锁定问题:

olhwl3o2  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(174)

我正在为我的项目开发一个锁定系统。我目前正在实现一种锁定多个目标的方法,并根据用户的输入定向循环。
然而,在使用时。似乎目标对象右侧的目标检查从未更新,这阻碍了正确的顺序。我不知道为什么会发生这种情况,因为它与左侧更新的代码相同。无论如何,看一看是值得赞赏的,想法更是如此!第1部分:

void ManageTargets()
    {
        shortDistLeft = Mathf.Infinity;
        shortDistRight = Mathf.Infinity;

        for (int i = 0; i < availableTars.Count; i++)
        {

            float dist = Vector3.Distance(transform.position, availableTars[i].transform.position);
            

            if (dist <= shortestDist)
            {
                shortestDist = dist;
                closestLockOnTarget = availableTars[i];
                currentTarget = closestLockOnTarget;

            }
            if(dist >= maxLockDist)
            {
                lockOn = false;
                shortestDist = Mathf.Infinity;
                availableTars.Clear();
                currentTarget = null;
                closestLockOnTarget = null;
                PlayerStateManager.instance.camState = 0;
            }
            if (lockOn)
            {
                Vector3 relativeEnemyPos = currentTarget.transform.InverseTransformPoint(availableTars[i].transform.position);
                var distanceFromLeftTarget = currentTarget.transform.position.x - availableTars[i].transform.position.x;
                var distanceFromRightTarget = currentTarget.transform.position.x + availableTars[i].transform.position.x;

                if (relativeEnemyPos.x  > 0.00 && distanceFromLeftTarget < shortDistLeft)
                {
                    shortDistLeft = distanceFromLeftTarget;
                    leftLockTarget = availableTars[i];
                }
                if (relativeEnemyPos.x < 0.00 && distanceFromRightTarget < shortDistRight)
                {
                    shortDistRight = distanceFromRightTarget;
                    rightLockTarget = availableTars[i];
                }
            }
        }
    }

第二部分:

void lockOnLogic()
    {
        if (lockOn)
        {
            
            if (currentTarget == null)
            {

               camRecenter();
                lockOn = false;
            }
            else
            {
                PlayerStateManager.instance.camState = 1;

            }

            if (lockOnMoveL == true) {
                lockOnMoveL = false;
                
                if (leftLockTarget != null)
                {
                    
                    currentTarget = leftLockTarget;
                    
                }
                ManageTargets();
            }
            if (lockOnMoveR == true){
                lockOnMoveR = false;
                    
                if (rightLockTarget != null)
                    {
                        currentTarget = rightLockTarget;
                        

                    }
                ManageTargets();
            }
                    
            
            

        }
        else
        {
            shortestDist = Mathf.Infinity;
            availableTars.Clear();
            currentTarget = null;
            closestLockOnTarget = null;
            PlayerStateManager.instance.camState = 0;
            lockOnMoveR = false;
            lockOnMoveL = false;
        }

    }
kulphzqa

kulphzqa1#

好吧,我想我找到问题了。
对于这个解决方案的工作,我假设由于你的场景和项目的设置方式,你只需要检查敌人相对于彼此的X位置(射击场,2D游戏等),我得出这个结论是由于这些行:

var distanceFromLeftTarget = currentTarget.transform.position.x - availableTargets[i].transform.position.x;
var distanceFromRightTarget = currentTarget.transform.position.x + availableTargets[i].transform.position.x;

你的问题可能就出在这里。
我认为这个问题与你计算距离的方式有关,如果你在XY平面上试着用手做数学,你会发现你实际上并没有做你认为你在做的事情。
因此,考虑到这一点,我会尝试将您的逻辑更改为如下形式

Vector3 relativeEnemyPos = currentTarget.transform.InverseTransformPoint(availableTargets[i].transform.position);
if (relativeEnemyPos.x  <= 0.00) continue;

var distanceToNewTarget = availableTargets[i].transform.position.x - currentTarget.transform.position.x;

if (distanceToNewTarget < 0 && Mathf.Abs(distanceToNewTarget) < shortestDistanceLeft)
{
    shortestDistanceLeft = Mathf.Abs(distanceToNewTarget);
    leftLockTarget = availableTargets[i];
}
else if (distanceToNewTarget > 0 && distanceToNewTarget < shortestDistanceRight)
{
    shortestDistanceRight = distanceToNewTarget;
    rightLockTarget = availableTargets[i];
}

它的工作原理是获取您正在测试的新目标与当前目标的相对X位置,然后根据该值是负还是正,决定它是向左还是向右,然后如果它比该方向上的最短距离短,则设置该值。
希望我能帮上忙,让我知道它是否有效!

相关问题