- Home /
Problem with Vector3 direction and axises.
Hello! I'm getting an headache now with dealing with these Vectors and Axises. This is going to be hard to explain, but please bear with me.
What I'm trying to do, is to find the Vector3 direction to the right of the object, but ignoring the y-axis, somehow. My first thought was obviously:
transform.right
Though, this doesn't quite work:
As you can see, the y-axis is not ignored, and the Vector doesn't point to the right, but to the right AND down.
Rather, this is what I want: As you can see, it's pointing to the right, ignoring the y-axis. You might think of using:
Vector3.right
Which works when the object is NOT rotated around the y-axis, but when the object IS rotated around the y-axis it DOESN'T work.
So, anyone who can help me? I've run into this problem so many times, and I cannot find a solution. Help would be highly appreciated!
Thanks in advance!
Answer by Bunny83 · Apr 09, 2013 at 01:41 PM
I guess you simply want:
var dir = transform.right;
dir.y = 0;
dir.Normalize();
$$anonymous$$eep in $$anonymous$$d when you rotate the objects right vector towards the world up vector, the vector will become 0,0,0 since it consists only of an y-part which is stripped away.
Answer by AVividLight · Apr 09, 2013 at 01:24 PM
Hey Linus371,
I don't think I understand your question very well, so please forgive me if I ask something stupid here.
So, you want to know a direction in 1d space? If that is the case, I would need more information about what you're actually try to do to provide a good answer, but I will suggest thinking a bit more creatively. By that, I mean maybe make the object a child of an emptyGameObject, and get the direction from the parent. Maybe it would be as easy as using the objects X-valuse + 2?
I'm sorry if this isn't a good answer! -Gibson
Answer by LyanApps · Apr 09, 2013 at 06:50 PM
I'm thinking what you're saying is your object is rotated on the z-axis. You should look up some trigonometry functions or remember SOH CAH TOA. The other name for this is vector projection. http://www.youtube.com/watch?v=7o4b10xroEM
Also don't forget you have the difference between local coordinates and world coordinates.
//var x = hypotenuse * cos (angle)
var x = 1 * Mathf.Cos(transform. localEulerAngles.z * Mathf.Deg2Rad);
//Vector in local direction
Vector3 localv3 = new Vector3(x,0,0);
//Transform to World Coordinates
Vector3 worldv3 = transform.TransformPoint(localv3);