Raycasts in opposite directions only returning RaycastHits on right

Hello,

I have a wallrunning script which fires Raycasts directly left, directly right, and some diagonals. Upon hitting a wall, that Hit is returned as RaycastHit wallrunWall;, and, should no wall be detected, wallrunWall = new RaycastHit();, as shown here (removed some things that shouldn’t matter, and is in FixedUpdate):

if ( (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out wallrunWall, WallRunDetectionRange) ||
            Physics.Raycast(transform.position, transform.TransformDirection(RaycastLeftDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunLeft) {
            
            wallrunningLeft = true;

        } else {
            
            wallrunWall = new RaycastHit();

            wallrunningLeft = false;

        }
        
        
        if ( (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out wallrunWall, WallRunDetectionRange) ||
            Physics.Raycast(transform.position, transform.TransformDirection(RaycastRightDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunRight) {

            wallrunningRight = true;

        } else {

            wallrunWall = new RaycastHit();

            wallrunningRight = false;

        } 

Here, when Debug.Log(wallrunWall.normal); for both left and right sides, I receive the proper normal vectors. However, in my Update method, where I calculate the parallel to the wall for wallrunning (shown here), wallrunWall is correct on the right side, but undefined on the left side.

inputVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

        // CHANGE VELOCITY BY MOVEMENT INPUT

        if (!wallrunningLeft && !wallrunningRight) {
            
            velocity = new Vector3(inputVector.x * PlayerMoveSpeed, velocity.y, inputVector.z * PlayerMoveSpeed);

        } else {
            
            // weird vector stuff

            wallParallel = Vector3.Cross(wallrunWall.normal, Vector3.up);
            
            if (wallrunningRight) {
                
                tempVelocity = new Vector3(wallParallel.x * -inputVector.z * PlayerWallrunSpeed, 0, wallParallel.z * -inputVector.z * PlayerWallrunSpeed);

            } else {
                
                tempVelocity = new Vector3(wallParallel.x * inputVector.z * PlayerWallrunSpeed, 0, wallParallel.z * inputVector.z * PlayerWallrunSpeed);

            }


            velocity = transform.InverseTransformDirection(tempVelocity);

        }

The result effect is that wallrunning works perfectly on the right side, but the player doesn’t move when on the left side. I’ve been stuck on this for a while, and any help is greatly appreciated!
Thanks :slight_smile:

Hi,
It is becaouse after making RaycastHit for the left side and storing it in wallrunWall variable, you are overriding it in next if statement for the right side. So wallrunWall can only be either RaycastHit for right side or empty new RaycastHit(). To avoid it you can store them in two separate vairables or
change last “else” to "else if(!wallrunningLeft) ".

 if ( (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out wallrunWall, WallRunDetectionRange) ||
             Physics.Raycast(transform.position, transform.TransformDirection(RaycastLeftDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunLeft) {
             
             wallrunningLeft = true;
 
         } else {
             
             wallrunWall = new RaycastHit();
 
             wallrunningLeft = false;
 
         }
         
         
         if ( (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out wallrunWall, WallRunDetectionRange) ||
             Physics.Raycast(transform.position, transform.TransformDirection(RaycastRightDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunRight) {
 
             wallrunningRight = true;
 
         } else if(!wallrunningLeft) {
 
             wallrunWall = new RaycastHit();
 
             wallrunningRight = false;
 
         } 

or shorter:

wallrunningLeft  = (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out wallrunWall, WallRunDetectionRange) ||
             Physics.Raycast(transform.position, transform.TransformDirection(RaycastLeftDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunLeft;
         
if(!wallrunningLeft) {
         wallrunningRight = (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out wallrunWall, WallRunDetectionRange) ||
             Physics.Raycast(transform.position, transform.TransformDirection(RaycastRightDiagonal.normalized), out wallrunWall, WallRunDetectionRange)) && testingForWallRunRight;
}

if (!wallrunningLeft && ! wallruningRight) {
             wallrunWall = new RaycastHit();
         } 

Ideally you would want to separate your code into separate functions for easier future code maintenance. Also try to have as few member variables as possible. :wink: