- Home /
Up vectors are really in world space?
Hi, I have an issue with up vectors. I am working on island demo scene and i have the following piece of code
GameObject cab = GameObject.Find("LevelObjects/Cabana2/cabanaRoof");
gt.text = cab.transform.up.ToString();
where Cabana2 is one of the two cabans in the scene and gt is a guitext element. As answer i get (0.0, 1.0, 0.0) which makes me think that the vector is in local space (while api reference says world space). So I tried this
GameObject cab = GameObject.Find("LevelObjects/Cabana2/cabanaRoof");
gt.text = (cab.transform.TransformDirection(cab.transform.up)).ToString();
But again I get (0.0, 1.0, 0.0). I get (0.0, 1.0, 0.0) even with
GameObject cab = GameObject.Find("LevelObjects/Cabana2/cabanaRoof");
gt.text = (cm.transform.TransformDirection(cab.transform.up)).ToString();
where cm is the main camera. So, what is the correct way to obtain the vector in world space?
I edited my post. I said it here so you get a blue envelope.
Answer by Peter G · Jul 23, 2010 at 12:55 PM
Unless you have rotated your cabana on either the x or z axis, then the up direction will be the same as the world direction. Transform.up will give you the world space Vector so if Transform.up
is Vector3(0,1, 0) and you use Transform.TransformDirection
, you will still get Vector3.up. So the number you are reading is correct. And yes, that number is in world space.
I don't think you understand the idea of a direction. Vector3's can be confusing when you first start because they are used for multiple things with different meanings.
They can be used for a position. In this case they will be something like your comment (1440, 35, 511).
They can represent eulerRotations. That is fairly simple to understand. You might have (0, 45, 0).
You can use Vector3's to store Direction. In this case you will get a normalized Vector that is centered at the origin. If you drew an arrow out from the origin through the point, you would get a direction. Hence they are called Vectors. So transform.up creates a Vector3 pointing in the same direction as the local y-axis of the object. If you call transform.forward, you will get something like (0, 0, 1) if your object has no rotation.
Unity makes no differential among the 3. Each function that takes a Vector3 self determines how it uses it. Some might take a Vector3 and use it as a position like transform.position while others take a direction such as Rigidbody.AddForce(). If you wanted to, you could take a Vector3 that was created with a certain use in mind and plug it into a different form.
mmm, i think i got where is the problem. cabanaRoof has coordinates (0,0,0) because it is a subobject of Cabana2 (which is located at 1440, 35, 511). So I think I have to add someway the two coordinates and then get the up vector. Any idea how i may do this cleanly?
yes, yes, I get this. $$anonymous$$aybe I didn't explain myself good enough. I need the up vector of an object using the global center as center, not the object's center. IE, Cabana2 is located at (1140, 35, 511), Cabana2's cabanaRoof is located at (0,0,0) (guess it is relative to its parent object) and finally Cabana2's cabanaRoof's up vecotr is (0,1,). So the up vector I really need is (1140,36, 511)
Ok. It sounds like you answered you own question. If you didn't then maybe you want transform.parent.position + transform.up; That might be the answer.
Your answer
Follow this Question
Related Questions
Keeping the player inside the screen? 2 Answers
camera movement in world space, but when the rotated on y axis 1 Answer
GUI in 3D world space "Monitors / Screens / Displays" 1 Answer
how to convert an objects local axis vector to 'world' vector. 1 Answer
Is there a local method for Rigidbody.constraints? 0 Answers