So in the tutorial we had a rolling ball, and were supposed to attach the camera to it. Doing it via the hierarchy menu made it mirror every movement, so it spun around, so we had to write a script, the script looks as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camer : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
void Start()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
transform.position = player.transform.position + offset;
}
}
Now heres my rather noob question: Why or How exactly does this work? Its not really explained in the tutorial or I did not really get it
In my understanding, we define a vector called offset that is supposed to mark the camera position, then we define it to be the transform.position, which I assume is just the initial position of the camera, minus the player.transform.position, which is the position of the ball.
Then we update out camera transform.position with another player.transform.position + offset
Now in my understanding this shouldnt do anything? Since we just got the initial transform.position of the camera, then subtracted the player position, and then added it again? In my mind this would work out to be a camera that should not move at all, but it is moving focused on the ball. Can anyone help me with understanding this better?