Hi there!
Ive just started out programming in unity, and i have a pretty simple question.
I have a ship moving in the xy plane, and i want to have my camera follow its position.
Now, i have this code connected to my camera:
var target:Transform;
function Update()
{
transform.position = new Vector3(target.transform.position.x, target.transform.position.y, -10);
}
Where the transform variable is connected to the ship-prefab.
The camera gets the initial position of my ship, but it fails to update as i move my ship.
Any idea what im doing wrong?
You don’t have to use code to achieve this. If you make your ship the parent of your camera, whenever your ship moves, the camera will move and rotate with it. Simply click and drag the camera onto your ship object in your hierarchy.
Make sure your camera is positioned where you want it before the game starts.
You can also “link” the camera to your ship in the Hierarchy by dragging the camera object’s name on top of your ship-prefab’s name in the list. Now where ever your ship goes, the camera goes too; no need for a code.
Thanks for the answer, allthough it didnt quite solve my problem. Ill try to illustrate it in a simpler way.
I have this script attached to a game object:
function Update ()
{
transform.Translate(0,0.1,0);
}
And then i have this script attached to another object.
var target:Transform;
function Update ()
{
print(target.position.y);
}
All i get printed out now is the targets position when i start the game. Im looking for getting the updated position as its moving.
Hope i explained myself good enough