x


other object's position?

Hello. How can I get the position of another object (rather than always using a specific vector 3)?

more ▼

asked Dec 23 '10 at 03:47 AM

Tyler 2 gravatar image

Tyler 2
1.1k 211 246 264

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I don't understand what you mean. Position in Unity is stored as a Vector3 so you must use a Vector3 somehow. There are plenty of ways to get the position. The easiest and probably best would be simply.

var somePos : Vector3 = otherObject.transform.position

  • or if you don't want to explicitly declare a variable (Vector3 is a value type so there is no worry about memory allocation) then you could also do:

FireLaserAtPos(otherObject.transform.position);

  • If you wanted to get fancy then you could get the objects bounds then go to the center of that.

var centerOfObject : Vector3 = someObject.renderer.bounds.center;

more ▼

answered Dec 23 '10 at 03:57 AM

Peter G gravatar image

Peter G
15k 16 44 136

That's what I thought, but didn't want to be wrong, haha.

Dec 23 '10 at 03:57 AM Justin Warner

Sorry to be unclear. I meant not having to always use the same position (like 100,0, 25) and would instead just have to select another object (so if that object moved, the position in the script would change to reflect that).

Dec 23 '10 at 04:12 AM Tyler 2
(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:

x1089
x884

asked: Dec 23 '10 at 03:47 AM

Seen: 1251 times

Last Updated: Dec 23 '10 at 03:47 AM