Change direction of vector based on which way player is facing

Hi,

I have a vector along a surface. Because I derive it from a normal vector, it’s always facing in the same direction. I need to change it to go in the direction the player is facing, but I’m having trouble getting it to work.

Here’s a picture:


What I have right now is this (wallVector is the vector along the wall):

bool wallErrorX = (camera.transform.forward.x >= 0 && wallVector.x <= 0) || (camera.transform.forward.x <= 0 && wallVector.x >= 0);
bool wallErrorZ = (camera.transform.forward.z >= 0 && wallVector.z <= 0) || (camera.transform.forward.z <= 0 && wallVector.z >= 0);            

 if (wallErrorX)
 {
     wallVector.x *= -1;
 }
 if (wallErrorZ)
 {
     wallVector.z *= -1;
  }

This works fine for any vectors at 90 degrees, but not for anything else.

Hopefully someone can help. Thanks!

You said you derive your “wallVector” from “a normal” but you haven’t mentioned how. Since this code is the root of your issue it’s the important information we have to know. Also I guess you mean the surface normal of your wall? If that’s the case you would simply use Vector3.ProjectOnPlane with your forward vector of your player and the normal vector of your wall.

ProjectOnPlane is not black magic but simple vector math. So I’m just wondering how you currently “derive” your vector from the normal.