can anyone find the issue with this code?

this code is called as the player is wall running, it turns the player as the normal they are attached too changes for use on round surfaces. the issue is /not/ in the “_left” portion, that works entirely as expected. the issue is when the player tries to run around a curved surface on the right side. the player does not rotate, aside from the initial rotation on impact with the surface which uses the same exact rotation code, the difference being that this is in update so it happens for the duration of the wallrun.

if (_wallRunning)
        {
            if (_left)
            {
                Debug.Log("left");
                RaycastHit hit;
                Ray ray = new Ray(transform.position,-transform.right);
                if (Physics.Raycast(ray, out hit))
                {
                    transform.rotation = Quaternion.FromToRotation(Vector3.right, hit.normal);
                }
            }
            else if (_right)
            {
                Debug.Log("right");
                RaycastHit hit;
                Ray ray = new Ray(transform.position, transform.right);
                Debug.DrawRay(transform.position, transform.right, Color.red); //draws ray being cast
                Debug.Log(Physics.Raycast(ray, out hit));//returns true
                if (Physics.Raycast(ray, out hit))
                {
                    Debug.Log(transform.rotation);//check rotation
                    transform.rotation = Quaternion.FromToRotation(-Vector3.right, hit.normal);
                    Debug.Log(transform.rotation);//no change

                }

            }
        }

Here is a link to a (very bad) video showing the problem on youtube.

also:

this has my current hierarchy and the script attached to the player

Well, have you made sure that:

  • “_right” is true all the time while you wall run? Where and how is it set to true? In short: are you sure your code actually runs every frame?
  • You should limit your raycast length. Otherwise you could “wallrun” a wall that’s 100m away to your right / left
  • Are you sure the object your raycast hits is actually the wall? Maybe you hit something on your player?

When using Debug.Log you should always add some description to be able to identify which log comes from which line in code.

Debug.Log("R object hit: " + hit.gameObject.name);
Debug.DrawRay(hit.point, hit.normal, Color.yellow); // vizualize hit point and hit normal
Debug.Log("R old rotation: " + transform.eulerAngles);
transform.rotation = Quaternion.FromToRotation(-Vector3.right, hit.normal);
Debug.Log("R new rotation: " + transform.eulerAngles);

Note that i prefixed the messages with “R” so we know those come from the “right” part and not the “left”. Seeing what you actually hit and where should help finding out what’s wrong.

Trying to understand your logic, but on lines 10 and 23, did you not mean:

transform.rotation = Quaternion.FromToRotation(transform.right, hit.normal);

and

transform.rotation = Quaternion.FromToRotation(-transform.right, hit.normal);

?