x


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?

more ▼

asked Jul 23 '10 at 12:42 PM

Patrik gravatar image

Patrik
59 13 13 20

I edited my post. I said it here so you get a blue envelope.

Jul 23 '10 at 01:34 PM Peter G
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

  1. They can be used for a position. In this case they will be something like your comment (1440, 35, 511).

  2. They can represent eulerRotations. That is fairly simple to understand. You might have (0, 45, 0).

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

more ▼

answered Jul 23 '10 at 12:55 PM

Peter G gravatar image

Peter G
15.1k 16 44 137

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?

Jul 23 '10 at 01:07 PM Patrik

I edited my post.

Jul 23 '10 at 01:28 PM Peter G

yes, yes, I get this. Maybe 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)

Jul 23 '10 at 01:53 PM Patrik

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.

Jul 23 '10 at 02:21 PM Peter G
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x205
x186
x115
x70

asked: Jul 23 '10 at 12:42 PM

Seen: 2447 times

Last Updated: Jul 23 '10 at 12:42 PM