x


Find transform.position of Another Game Object

I tried to make a Camera Follow script. The first thing i did was to set the position of the camera to the players position.

Here is the code i used in javascript:

 var follow : GameObject;

function Update () {
     var FollowTransform : Transform = (follow.gameObject.GetComponent(Transform));
     transform.position = FollowTransform.position;
}

But when the game started the camera changed position to where the player was at the beginning of the game but remained there. The FollowTransform.position only returned the initial position of the player. What did I do wrong?

more ▼

asked May 08 '12 at 04:10 PM

dht1kna gravatar image

dht1kna
74 8 8 12

"GetComponent(Transform)" - no. .transform - yes. gameObject is also useless; it IS a Game Object.

May 08 '12 at 04:18 PM Jessy

I did what you said but it gave the same result

May 08 '12 at 04:23 PM dht1kna

you get what you're actually doing here: you're transferring the values of the initial player position into a new variable (copy by value rather than creating a reference. They're not automatically linked - one won't update when the other one does.

Creating a real camera is more involved. Rather than having somebody reexplain it here, you should google a couple of tutorials on the subject first and see if you have questions afterwards.

May 08 '12 at 05:00 PM Drakestar
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can set the camera as a parent to your game object. That way it will "follow" your game object.

i.e. var FollowTransform : Transform = (follow.gameObject.GetComponent(Transform)); transform.parent = FollowTransform;

You can adjust the position and rotation of the camera, in relation to the game object, using localPosition and localRotation.

more ▼

answered May 08 '12 at 04:58 PM

centaurianmudpig gravatar image

centaurianmudpig
41 9 10 12

(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:

x3014
x2096
x1281
x316
x109

asked: May 08 '12 at 04:10 PM

Seen: 1637 times

Last Updated: May 08 '12 at 05:00 PM