Hi.
I am following a tutorial and currently writing script to make the camera follow the player.
The code obviously works, considering that it is a tutorial, but there is a part of the code that doesn’t seem to make sense to me:
[Original Code]
public class CameraFollow : MonoBehaviour
{
private Transform player;
private Vector3 tempPos;
void Start()
{
player = GameObject.FindWithTag("Player").transform;
}
// Update is called once per frame
void LateUpdate()
{
tempPos = transform.position;
tempPos.x = player.position.x;
transform.position = tempPos;
}
Just to make sure my understanding, I have tried replacing the private Transform player to private Vector3 player and adjusting the corresponding parts of the script, but when I run the game, the x position of the player seems to be assigned only once at the beginning.
[Changed that I have made]
public class CameraFollow : MonoBehaviour
{
private Vector3 player;
private Vector3 tempPos;
void Start()
{
player = GameObject.FindWithTag("Player").transform.position;
}
// Update is called once per frame
void Update()
{
tempPos = transform.position;
tempPos.x = player.x;
transform.position = tempPos;
}
My question is, how come player.position.x is automatically updated to the current position, when player.x is not?
It seemed to make sense, as I am assigning a Vector3 to another Vector3 in the Update() function.
Any help would be great!