"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.

Vector3 fake = transform.forward; fake.x = 0; fake.z = 0; Is this what you were looking for?

3 Answers

3

This should work:

Vector3 fakeForward = transform.forward;
fakeForward.y = 0.0f;
fakeForward.Normalize();

I don't know UnityScript/JavaScript that well, but this looks like it may do about what tanoshimi does below, just in a different way? Any idea which would be more efficient in terms of calculation?

I doubt there is any noticeable performance difference. It will all come down to the pcode and/or machine code produced. The one difference is the 'Normalize()'. If you want to use the resulting vector for movement, you likely want to normalize it.

Alright, thank you much.

This sure will cause many troubles, especially when you look straight up

Straight up is an issue, but it is the only one I see. What are the other 'many troubles'. Assuming straight up is an issue (often it is not), you can test and use a default value or the previous valid value depending on the need. if (transform.forward != Vector3.up) { Vector3 fakeForward = transform.forward; fakeForward.y = 0.0f; fakeForward.Normalize(); } else { // Whatever }

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.