0
\$\begingroup\$

So I've been away from Unity and game development in general for a bit, so I'm working through Unity's UFO Tutorial and had a question about the camera set up. I don't understand why you need the offset for it? It follows the Player object directly it seems, and they use a Vector3? But it's a 2D game, so why not just use a Vector2? I was experimenting with the code just to see if I could figure it out, so I removed the offset variable completely but then when I ran the game it just showed a blank space in the game view. Any body who can help me understand this?

public class CompleteCameraController : MonoBehaviour
{
    public GameObject player; //Public variable to store a reference to the player game object
    private Vector3 offset; //Private variable to store the offset distance between the player and camera

    // Use this for initialization
    void Start ()
    {
        //Calculate and store the offset value by getting the distance between
        // the player's position and camera's position.
        offset = transform.position - player.transform.position;
    }

    // LateUpdate is called after Update each frame
    void LateUpdate ()
    {
        // Set the position of the camera's transform to be the same as
        // the player's, but offset by the calculated offset distance.
        transform.position = player.transform.position + offset;
    }
}
\$\endgroup\$
3
\$\begingroup\$

First: What is offset and why do we need it?

As comment goes in your script, offset is just a variable which will store a starting distance between player and camera. (We subtract player position from camera position, so we get the difference. basic vector algebra).

you can try making this variable public: public Vector3 offset; Remove it from start method completely (but don't remove from lateupdate). And launch the game.

You will see that it first, your camera will be inside player. This is because offset variable was 0,0,0 at first, so you are adding 0 to player position:

transform.position = player.transform.position + offset;

that obviously == player position.

You will be able to edit this variable in inspector, play around, you will see that camera starts moving, relative to player.

Second: Why Vector3 and not Vector2?

Yes, it is 2D game, and you can try using Vector2, but if you do, you will be able to move camera only, up, down and left, right, but not forwards and backwards.

Hope it helps.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ On the section about 3D, it might be useful to mention that even 2D Unity games still use the third axis for layering, and that cameras can't see anything behind their near plane, so backing the camera away in the third dimension helps ensure all the 2D content is in front of it where it can be seen. \$\endgroup\$
    – DMGregory
    Feb 11 '18 at 9:59
-1
\$\begingroup\$

You need an offset because otherwise the camera would be in the exact position as the player.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ This answer would be better if it explained why a camera in the exact same position as the player is generally undesirable, as it sounds like this conclusion is not obvious to the user asking the question. \$\endgroup\$
    – DMGregory
    Feb 11 '18 at 10:00
  • \$\begingroup\$ Isnt it obvious why you cant have the camera in the same position as the thing the camera is looking at? \$\endgroup\$
    – Owens
    Feb 11 '18 at 20:58
  • 2
    \$\begingroup\$ If it were obvious to OP, would they have taken the time to ask the question? It can be surprisingly illuminating to take the time to explain from first principles even "obvious" results in game development. Not everyone knows things that experienced gamedevs take for granted, like near planes and culling and z-ordering... \$\endgroup\$
    – DMGregory
    Feb 11 '18 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.