How does this part of the script make sense?

Sorry I’m new to programming. Please forgive me if this question is stupid.

I found this in the 2D UFO Tutorial.

using UnityEngine;
using System.Collections;

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;


Ok if, offset = transform.position - player.transform.position; AND transform.position = player.transform.position + offset;

which means transform.position = player.transform.position + transform.position - player.transform.position;

simplify it we got player.transform.position=player.transform.position;

Yes… Equation is correct, but what??

When you take one vector from another what you get is a directional vector from that vector to the other. For example

offset = transform.position - player.transform.position;

Means that “offset” is a directional vector from “player.transform.position” to “transform.position”. Keep in the mind that this is a direction and can be used anywhere in space (not dependent on starting point).

So, by using the code:

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

You are taking the player’s position and adding that direction (offset variable) to get the new position for the transform (transform.position).

You cannot “simplify” the code the way that you tried. It doesn’t work that way.

When offset is first calculated in Start() the player is in a certain position. In the LateUpdate() method the player might already moved so its position is different then the one used in the Start() method so player.transform.position in the LateUpdate() method is not the same as it was in the Start() method.

The offset is used to move the camera so it will always be at the same distance and direction from the player.

(if the player does not move at all then you are right but most likely the player will move and change its position in the game)

Let’s take a visual approach to this:

First, you have your character and camera and Vector2(5, -3) and Vector2(16, 2) respectively.

The difference between those two points is a subtraction of one from the other.

A ----------------> B

To get the Vector going from point A to point B, you subtract A from B. In other words, AtoB = B - A.

In the context of the drawing, that means your camera’s offset from the player is (16, 2) - (5, -3), resulting in a difference of (11, 5). So, at all times, this camera should remain 11 units to the right of, and 5 units above the player.

The Start() function in Unity runs once per object, when it first enters the game. This gives the opportunity to generate that offset based on the player’s and camera’s starting positions without being influenced by their locations later.

The LateUpdate() function, much like the Update() function, takes place once per frame of gameplay. Because of this, your player’s position will likely be changing rapidly. However, you want the camera to remain that same distance away from the player at all times. That’s why we determine what its offset should be in the Start() function.

All in all, it’s actually nothing more than a simple oversight.

transform.position = player.transform.position + transform.position - player.transform.position;

is not “wrong”, but it’s not right either. What’s actually happening is more like this:

transform.position = player.transform.position + (transform.originalPosition + player.transform.originalPosition);

It would be silly and ineffective to recalculate the camera’s offset on every frame of gameplay. Likewise, because it’s NOT updating every frame, it is a constant value to make use of instead.

I hope this answered your question adequately. If not, there’s always room for more doodles!