多对象锁定问题:

问题描述 投票:0回答:0

我正在为我的项目锁定系统。我目前正在实施一种方法来锁定多个目标并根据用户的输入定向循环。

但是,在使用的时候。似乎对目标对象右侧的目标的检查永远不会更新,这使正确的顺序停滞不前。我不知道为什么会这样,因为它与更新的左侧代码相同。无论如何,看一看是值得赞赏的,想法更是如此! 第 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];
                }
            }
        }
    }

和第 2 部分:

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;
        }

    }

c# unity3d game-development
© www.soinside.com 2019 - 2024. All rights reserved.