- Home /
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!
Answer by Bunny83 · Feb 28, 2020 at 01:22 AM
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.
So I just send a raycast out from the player. If it hits a wall, I use Vector3.Cross(raycasthit.normal, Vector3.up) to get the wallVector.
Well, that doesn't make much sense since the vector you calculate is purely based on the world up vector and the wall normal. As I said you should simply use ProjectOnPlane.
This will just project your camera's forward vector onto the plane surface: Vector3 fwd = camera.transform.forward; Vector3 normal = raycasthit.normal; Vector3 wallVector = Vector3.ProjectOnPlane(fwd, normal);
Of course in a 3d environment the vector could point along the surface in any direction on that plane. So if the player also looks up or down the vector would be tilted along the wall surface. If you want the vector to be horizontal, just do this
// [ ... ]
fwd.y = 0f;
Vector3 wallVector = Vector3.ProjectOnPlane(fwd, normal);
Note that this gives you a projection of the forward vector onto the wall. So the vector length will vary depending on the angle the user looks at the wall. You might want to normalize the resulting vector depending on your usecase.
Alternatively you can use two Vector3.Cross calls like that:
Vector3 refVec = Vector3.Cross(normal, fwd);
if (refVec.sqr$$anonymous$$agnitude < 0.0001f)
refVec = Vector3.up;
Vector3 wallVector = Vector3.Cross(refVec, normal);
Again if you want to have the resulting vector always being horizontal, do this:
// [ ... ] refVec.x = refVec.z = 0f; Vector3 wallVector = Vector3.Cross(refVec, normal);
I just tried ProjectOnPlane, and it seems to work! I just had to clamp the $$anonymous$$imum magnitude of the vector to 1.
Thanks!
Your answer
Follow this Question
Related Questions
How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers
How can I calculate the Vector3 b using Vector 3 a pos with an angle? 1 Answer
Vector math problem 1 Answer
Finding Nearest Object Both Positive And Negative? 1 Answer
Given a vector, how do i generate a random perpendicular vector? 5 Answers