How ifs work?
Okay, I'm really tired of this damn if statments, 96 out 100 times it wont work -.- . Can please someone explain how if's work?
Because here I have an example. In this code the ray of my enemy detects if it collides with a player or not. If it collides, the enemy should stop moving. If it doesnt collide, he should continue his patrol... In this case, if the ray detects the player, the enemy stops moving, forever. It's in update, it sould be updated every frame and the condition should be checked every frame if its true or not but it still stops at the first if statement and totally ignores my else statement. cO (I put a return in out of frustration)?
RaycastHit2D _hitInfo = Physics2D.Raycast(transform.position, transform.right, distance, whatCollideWith);
Debug.DrawLine(transform.position, transform.position + transform.right * distance, Color.green);
RaycastHit2D _hitInfo_1 = Physics2D.Raycast(transform.position, -transform.right, distance);
Debug.DrawLine(transform.position, transform.position + (-transform.right) * distance, Color.red);
if (_hitInfo.collider != null){
if (_hitInfo.collider.CompareTag("Player")){
faceDirection = 0;
return;
}
else faceDirection *= -1;
}
Your answer
Follow this Question
Related Questions
If/else statement is always else 1 Answer
How to put an if statement inside an if statement? 3 Answers
So I'm very new at Unity and I tried to improvise the controllers with if statement. 0 Answers
Unity 5 OnCollisionEnter IF statement not working. 1 Answer
HELP ME PLEASE TO FIX MY PROBLEM.. 1 Answer