"Fake" Vector3.forward?

Okay, so I’ve been trying to find the most efficient and straight-forward way of setting an artificial “forward” movement that functions exactly the same way as Vector3.forward does, but in respect to the GameObject Y rotation. I don’t want to account for any other rotation, since this would function exactly like Vector3.forward, but in the relative direction that the object was facing.

So, I’m looking for tips, tricks, pointers or pseudo-code that I can use to piece together a better understanding of how this is accomplished so that I can get the function I need. I don’t necessarily want someone to write the code for me, since that defeats the purpose of learning with full understanding most of the time, but even an example would be good if it’s just a simple oversight that I’m making.

I know about transform.forward, however this takes all local rotation into account and moves in local forward direction. So either I need a way to define a “fake” Vector3.forward based on direction using Y rotation, or I need a way to remove X and Z rotation from the transform.forward direction so that my movement is always on the same plane, regardless of irrelevant rotations.

This should work:

Vector3 fakeForward = transform.forward;
fakeForward.y = 0.0f;

If you want your movement to “always be on the same plane” (and I’m assuming that you mean a flat horizontal plane), don’t you simply want to ignore the y component of the transform.forward, i.e.:

Vector3 fakeForward = new Vector3(transform.forward.x, 0, transform.forward.z);


Just an alternative solution for

Vector3 fakeForward = new Vector3(transform.forward.x, 0, transform.forward.z);

which I believe solves the “looking straight up / down” problem:

Vector3 fakeForward = Quaternion.Euler(0f, transform.eulerAngles.y, 0f) * Vector3.forward;

Basically I made a rotation similar to transform.rotation but without the “x” component (or “z”) and applied it to the Vector3.forward vector. I tested this and it worked even when I looked straight up or down.