简体   繁体   English

即使不应该运行代码行

[英]Line of code is ran even tho it shouldn't

I'm trying to make my own moving solution for unity.我正在尝试为统一制定自己的移动解决方案。 Here's the code:这是代码:

private void Update()
    {
        //Movement
        HandleMouseLooking(canLook); //HandleMouseLooking() should go before HandleMoving()   
        HandleMoving(canMove); //HandleMoving() and HandleCrouching() have the same importance so it doesn't matter
        HandleCrouching(canCrouch);
        HandleLedgeClimbing(canClimb);
        HandleSprinting(canSprint); //HandleSprinting() must be the last Handle of the Movement category
    }


bool[] DetectLedge()
    {
        /*
         * table of truth:
         * indexes:
         *   0       1        2
         * upper | lower | canClimb
         *   0   |   0   |    0     /0
         *   1   |   0   |    0     /1
         *   0   |   1   |    1     /2
         *   1   |   1   |    0     /3
        */

        bool[] ledgeInfo = new bool[3] { false, false, false }; //0
        ledgeInfo[0] = Physics.CheckBox(upperLedgeDetector.position, upperLedgeDetector.lossyScale, upperLedgeDetector.rotation, 128);
        ledgeInfo[1] = Physics.CheckBox(lowerLedgeDetector.position, lowerLedgeDetector.lossyScale, lowerLedgeDetector.rotation, 128);
        ledgeInfo[2] = ledgeInfo[1] && !ledgeInfo[0] ? true : false;

        return ledgeInfo;
    } 

    void HandleLedgeClimbing(bool isEnabled)
    {
        if(isEnabled)
        {
            Debug.Log(DetectLedge()[0] + " " + DetectLedge()[1] + " " + DetectLedge()[2]);
            if(DetectLedge()[2])
            {
                Debug.LogWarning("Ledge!");

                canMove = false; //<--------------------Here it is!

                Vector3 calculatedPos = Vector3.zero;
                RaycastHit hit;
                Physics.Raycast(upperLedgeDetector.position, Vector3.down, out hit, 3f, 128);
                calculatedPos = new Vector3(lowerLedgeDetector.position.x, upperLedgeDetector.position.y - hit.distance, lowerLedgeDetector.position.z);
                if(transform.position != calculatedPos)
                    transform.position = Vector3.Lerp(transform.position, calculatedPos, Time.deltaTime);
            }
        }
    }

That's all the code that I believe is causing the issue.这就是我认为导致问题的所有代码。 Okay, so now to explain what's not working.好的,现在解释什么不起作用。 Whenever I run the scene my player stops moving.每当我运行场景时,我的播放器都会停止移动。 This is not expected at all because the only reason why it should stop moving is if the canMove variable is false, but I never set it to be false.这根本不是预期的,因为它应该停止移动的唯一原因是canMove变量是否为 false,但我从未将其设置为 false。 Now the only place in the code where it does change from true to false is in the HandleLedgeClimbing() void.现在代码中唯一会从 true 变为 false 的地方是在HandleLedgeClimbing() void 中。 But it should never come to it being set to false.但它永远不应该被设置为假。

I marked it's location with a big arrow.我用一个大箭头标记了它的位置。

Now, I'm not an expert, but I'm pretty sure that everything in an if statement only runs if the condition in the if statement is true.现在,我不是专家,但我很确定if statement中的所有内容只有在 if 语句中的条件为真时才会运行。 So why is only canMove = false;那么为什么只有canMove = false; ran when the if statement is false, which is checked by that Debug.Log(DetectLedge()[0] + " " + DetectLedge()[1] + " " + DetectLedge()[2]);当 if 语句为 false 时运行,由Debug.Log(DetectLedge()[0] + " " + DetectLedge()[1] + " " + DetectLedge()[2]); (which when I run the scene return false for all three variables; note: only the last variable is being checked in the if statement) . (当我运行场景时,所有三个变量都返回 false;注意:if 语句中只检查最后一个变量)

Also, yes, you read that right, " only "还有,是的,你没看错,“只有

For some reason, nothing else in that if statement gets called!出于某种原因,该 if 语句中没有其他内容被调用! That's why I put the Debug.LogWarning("Ledge;");这就是为什么我把Debug.LogWarning("Ledge;"); there, to see what causes the if statement to be true all of the sudden.在那里,看看是什么导致 if 语句突然为真。 But it was never called!但它从未被调用过! Kind of creepy haha-有点毛骨悚然哈哈-

TL/DR: A part of the code is called when it shouldn't be possible for it to ever be called under the conditions we are presented in the scene. TL/DR:当我们在场景中呈现的条件下不可能调用代码的一部分时,它就会被调用。 Best part is: only a specific line is called.最好的部分是:只调用特定的行。 Not even the whole block.甚至不是整个街区。 I want to eliminate this from happening.我想消除这种情况的发生。

Condition: DetectLedge[2] must be true for the if statement to be run.条件:对于要运行的 if 语句,DetectLedge[2] 必须为真。 The debugging proves it's always false (unless we come near a ledge) and prior testing fortifies the fact.调试证明它总是错误的(除非我们靠近壁架)并且之前的测试强化了这一事实。

UHM I fixed it... in the weirdest possible way?,我修好了……以最奇怪的方式? I'm not sure at all what caused the issue, but if anybody knows it, please share.我完全不确定是什么导致了这个问题,但如果有人知道,请分享。 Some people could find it interesting.有些人会觉得很有趣。

Anyways, here is the fix:无论如何,这里是修复:

EdgeDetectInformation DetectLedge()
    {
        /*
         * table of truth:
         * indexes:
         *   0       1        2
         * upper | lower | canClimb
         *   0   |   0   |    0     /0
         *   1   |   0   |    0     /1
         *   0   |   1   |    1     /2
         *   1   |   1   |    0     /3
        */

        bool[] ledgeInfo = new bool[3] { false, false, false }; //0
        ledgeInfo[0] = Physics.CheckBox(upperLedgeDetector.position, upperLedgeDetector.lossyScale, upperLedgeDetector.rotation, 128);
        ledgeInfo[1] = Physics.CheckBox(lowerLedgeDetector.position, lowerLedgeDetector.lossyScale, lowerLedgeDetector.rotation, 128);
        ledgeInfo[2] = ledgeInfo[1] && !ledgeInfo[0] ? true : false;

        return new EdgeDetectInformation(ledgeInfo[0], ledgeInfo[1], ledgeInfo[2]);
    } 

Basically, instead of a bool[] I used a struct.基本上,我使用的是结构而不是bool[] This was sort of @derHugo 's idea, so thank you for that!这是@derHugo的想法,所以谢谢你!

And in the other function, instead of calling it over and over in the Debug.Log() I did this:在另一个 function 中,我没有在Debug.Log()中一遍又一遍地调用它,而是这样做的:

 void HandleLedgeClimbing(bool isEnabled)
    {
        if(isEnabled)
        {
            EdgeDetectInformation EDI = DetectLedge();
            Debug.Log(EDI.upper + " " + EDI.lower + " " + EDI.canClimb);
            if(EDI.canClimb)
            ...

And... that fixed it.并且...修复它。

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

相关问题 为什么即使不应该显示此代码也会显示错误? - Why would this code display error even when it shouldn't? 区域中的视图无法识别“ ActionLink”,“ Parital”,“ ViewBag”,即使其他区域也可以使用 - View in an Area gives doesn't recognize 'ActionLink', 'Parital', 'ViewBag' even tho other areas work 我的玩家正在穿过墙壁(即使它们是刚体)并且下面的脚本不起作用(C#) - My player is going through walls(Even tho they are rigid bodies) and the script below doesn't work(C#) 在我的代码中,如果重复但不应该 - in the my code if repeats repeatedly but but it shouldn't be 代码优先迁移的外键不应该存在 - Foreign Key on Code First Migration That Shouldn't Be There 我的代码不应该跳转到函数 - My code is jumping to a function when it shouldn't be 控制台应用程序接受“ /”作为密码,即使该密码不应该接受 - Console Application accepting '/' into password even though it shouldn't 如何使用正则表达式获取整个部分(直到一行不以空格开头) - How tho get the whole section with regex (until a line doesn't start with whitespace ) 球体重叠,甚至在我检查碰撞的情况下 - Sphere are overlapping even tho I check for collisions SqlCommand即使数据库中有数据也返回null - SqlCommand returns null even tho there is data in the database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM