- Home /
"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?
Answer by robertbu · Oct 07, 2014 at 07:59 PM
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.
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'. Assu$$anonymous$$g 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
}
Answer by tanoshimi · Oct 07, 2014 at 08:02 PM
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);
?
I believe what you have here is what I'm looking for. Basically, Vector3.forward moves in a direction in world space in the Z direction without regard to object rotation. There is no X axis movement or Y axis movement, it's simply "forward in world space."
This is what I want to accomplish, but rather than a "strict forward in world space without regard to rotation" I want to define a new "fake forward" that operates the same way, but ins$$anonymous$$d accounts for relative direction (the GameObject Y rotation)...so that no matter whether I'm looking down or up, all movement is happening on a horizontal plane in whatever X/Z ratio translates to a "forward in relative direction" without affecting the Y value at all.
Edit works perfectly fine, but I'm using robertbu's code as it's cleaner and more technically precise in my opinion, and since his answer was provided first, his is the one I will accept as well.
Answer by EuSuntNoob · Jul 13, 2020 at 05:22 PM
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.
Your answer
Follow this Question
Related Questions
iTween Path Editor: ignore Y position in Vector3 1 Answer
How to detect if player is facing towards enemy's back 2 Answers
How to find the direction to translate an object, to move towards a point, regardless of rotation? 1 Answer
Calculate direction of travel. 3 Answers
Touch GUITexture and Game object moves left or right, how can i do that? 0 Answers