I'm seeing a curious problem with Raycast. The basic idea of the code below is: when a player runs into a wall, but is pressing the F key, they perform a wall flip. I raycast to check that they are indeed hitting a wall and not some other object, and close enough to the wall to do the flip.
So in the first level, everything works great. The next level, the flip doesn't work whatsoever. Initially I thought the walls in the second level must not have colliders or are set to isTrigger or something, triple checked everything. Then I added a ton of Debug statements to the relevant code to see what's going on, and to my surprise when on the second level, the Raycast never hits anything! Even when the Debug ray is clearly going through a wall that's setup the same way as walls in level 1. Here's an image of the magenta debug ray clearly going through a wall in level 2:
In the lower left is the inspector values of the wall shown in the screencap, which doesn't return a raycast hit. To the lower right is a wall from level 1 that does indeed work properly. You can see they are setup the same exact way (save their X and Z scales being flipped because they are different orientations, but this same bug occurs with walls of the same orientation too).
Basically, the only difference that I can discern between the walls of level 1 and level 2 is that the levels themselves are at different Y positions, which should not in any way affect raycasting. I'm pretty stumped why it works in level 1 but not in 2 or beyond.
if (Input.GetKey(KeyCode.F)){
// Raycast to see if we hit a wall
RaycastHit hit;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(this.transform.position, fwd, Color.magenta, 2.0f);
if (Physics.Raycast(this.transform.position, fwd, out hit, 2.0f)){
if (hit.collider.gameObject.name.Contains("Wall")){
Debug.LogWarning("WALLFLIP: Attempted wallFlip, raycast did indeed hit a wall!");
// Stuff happens here such as flip animation, not relevant
}
}else{
Debug.LogWarning("WALLFLIP: Raycast hit nothing");
}
}
UPDATE 1: Saw a SO thread that suggested using RaycastAll() instead of Raycast(). When implemented, RaycastAll() returns a list of length 1 in level 1, but returns an empty list in level 2 which confirms the ray doesn't hit anything even though the Debug ray clearly does.
UPDATE 2: Saw this SO thread about doing Raycast stuff in FixedUpdate() instead of Update(). So I moved the WallFlip code out of Update to a FixedUpdate, saw no change. When in FixedUpdate level 1 work, but in level 2 no raycast hits are returned.
UPDATE 3: Thanks to user Sharundaar, I now know that the floor of level 2 was interfering with the Raycast (see his Answer below). Still puzzled with this didn't occur in level 1, or why the floor doesn't return a hit on Raycast(). Below is the inspector for the floor gameObject of level 2 (the floor of level 1 is identical except for the xyz position and X Z scale). Pretty weird issue I've never encountered before, but hey Raycasting from the player's position offset by 0.5f Y works. *shrug*