I'm trying to cast a ray to check for collision when "W" is pressed. I figured out how it should work but I can't figure out why it's working in my first case and not in the second.
1) In this case "hit" is printed
RaycastHit hit = new RaycastHit();
Debug.DrawRay (this.transform.position, this.transform.up, Color.green);
if (Physics.Raycast(this.transform.position, this.transform.up, out hit)) {
print("hit");
}
// Check for W
if(Input.GetKeyDown(KeyCode.W))
{
MoveForward();
}
2) Doing exactly the same raycast, but only when W is pressed, "hit" is not printed.
RaycastHit hit = new RaycastHit();
Debug.DrawRay (this.transform.position, this.transform.up, Color.green);
// Check for W
if(Input.GetKeyDown(KeyCode.W))
{
if (Physics.Raycast(this.transform.position, this.transform.up, out hit)) {
print("hit");
}
MoveForward();
}
Can someone explain why it would detect the hit in the first case and not in the second? The situation in the game is exactly the same and I'm pressing "W".
↧