0
votes

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:

Debug ray goes through wall!

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*

Floor interferes

1
Do you have the message "WALLFLIP: Raycast hit nothing" ? Is it possible to see the full source somewhere ? I can't conclude anything from here.Sharundaar
Yup. On level 2 I see a ton of "raycast hit nothing" as I hold the F key down even though the ray clearly hit the wallMurkantilism
Full source is available here, relevant code starts at line 197. You'll notice that I simplified the wallFlip code for this SO post, so if anything in the source is confusing LMK. =) ty for your time btwMurkantilism
Hey Murk ... trivial possibility, are you simply casting too short?? Just increase the distance to 1000 or something and see what happens.Fattie
hope that's all it is!Fattie

1 Answers

1
votes

So from your source I managed to make it work on the other level. The thing is that your transform.position is sligthly underground so the raycast hit the floor and not the wall (I don't quite know why it works specifically on the first level).

What I did is as follow :

Vector3 raycastOffset = new Vector3(0, 0.5f, 0);
if (Physics.Raycast(this.transform.position + raycastOffset, fwd, out hit, 2.0f))

This is of course a quick fix, you'll need to investigate further.

I also changed

Vector3 fwd = transform.TransformDirection(Vector3.forward);

by

Vector3 fwd = transform.forward;

which should be cleaner.

After a little bit of investigation your problem effectively lies into the floor entity, I think it is essentially the MeshCollider that can't compute the collision well, try replacing it with a correctly ajusted box collider and see where it leads you.