'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.
Now the only place in the code where it does change from true to false is in the 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. So why is only canMove = false; ran when the if statement is false, which is checked by that 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).
Also, yes, you read that right, "only"
For some reason, nothing else in that if statement gets called! That's why I put the Debug.LogWarning("Ledge!"); there, to see what causes the if statement to be true all of the sudden. 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. 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. The debugging proves it's always false (unless we come near a ledge) and prior testing fortifies the fact.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
