- Home /
Rotate Character to match Wall Normals
I am attempting to make my character wall run. It is already set up to know when it can and cant wall run, the issue now is telling it the direction the wall is going.
I am assuming for the time being that wall running can only be done on straight walls (the walls are tagged so there is no extra coding required for that), however I would prefer to have it coded so I can convert to curved walls down the road.
How would I have my character match the normals of the wall with its Y axis (rotate so it is perpendicular to the wall)? And in turn Know the direction the wall is from the character so I may add force towards it (or away from it, in the case of a wall jump)
I don't think it is important.. but I am using a Character Conroller, not a physics controlled character.
Answer by robertbu · Aug 25, 2013 at 03:25 AM
Without the inclusion of your code, it is hard to be specific. The typical solution is to Raycast. The RaycastHit structure has a normal of the object hit. If you've isolated the object to the wall, then you can Collider.Raycast() rather than Physics.Raycast(), but both return a RaycastHit structure. So assuming you have a RaycastHit structure called 'hit' initialized in the Raycast() call, and a pivot point at the feet of your character, you the code to do the alignment might look like:
transform.position = hit.point;
var q = Quaternion.FromToRotation(transform.up, hit.normal);
transform.rotation = q * transform.rotation;
Note often code like needs to be done over time with a Quaternion.Lerp(), or a Quaternion.Slerp() to avoid sharp transitions.