Camera follow on an offset?

I have a simple, one line script (if you don’t include all the generic things like variable and method declarations and stuff), which takes in 3 parameters for the x, y and z positions, for following the player.

Currently the camera is following the y axis by setting its y position to that of the player Transform, but I need it to float slightly above the player, as right now it feels like it’s stuck to the ground and the angle is very unplesant. So I need some way of it following the player along the y axis, while still having an offset.

The current line I’m using can only take a maximum of 3 parameters.

Here’s the line:

transform.position = new Vector3 (player.position.x, player.position.y, offset.z);

Any idea how I would do it? I can’t think of anything.

I understand that currently I’m just setting a new position for the camera. I have various ideas I think would work, but they all involve adding more parameters to the line, which currently is giving me errors.

like : player.position.y + 2 (still 1 parameter, because it’s before the comma) Is that what you meant?

1 Like

Yes, thank you! :smile:
I was doing stuff in parameters of other code which involved math, but it gave me errors so I assumed this would also be the case.

:slight_smile:

A cleaner way is to do

transform.position = player.position + offset;

Then you can just change your offset to be whatever you need it to be. Later you can even drive offset dynamically.

2 Likes

public GameObject player;//(set this in the inspector)
private Vector3 offset = new Vector3 (x, y, z);
transform.position = player.transform.position + offset;

1 Like

ok now how do I change this offset to a new vector 3 whenever I press a button that will change the view to look backwards?