- Home /
Unity UFO tutorial question about code
Hello guys , I was going through tutorial of unity UFO 2D game here is the link : https://unity3d.com/learn/tutorials/projects/2d-ufo-tutorial/following-player-camera?playlist=25844 this is video part 5. ok so my question about all this is the code that the guy wrote , the goal is to make the camera move with the player without letting the camera rotate (while the player does) so that's why we didn't use parent-child and wrote this code instead:
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 so my question is why did we write : offset = transform.position - player.transform.position; and transform.position = player.transform.position + offset;
because i was thinking , the position of camera and player are both (0,0,0) at the beginning so offset = 0-0=0 then we move player consider it is now at position (3, 0 , 0) camera position(x) = 3-0=0 camera position(y) = 0-0=0 which means in other words camera position is equal to player position so i don't understand the reason for having an offset
yet , if i type the code that way
using UnityEngine; using System.Collections;
public class cameracontroller : MonoBehaviour {
public GameObject player;
// Update is called once per frame
void LateUpdate () {
transform.position = player.transform.position;
}
}
and hit play , both the camera and player are not visible on the scene can anyone help me understand what is going on and why we used offset and why my method didn't work (please use examples as i understand better with examples)
thanks for all have a good day :)
Your answer
Follow this Question
Related Questions
Unity 2020.3.21f1 running slow 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
cancle invokeReapeating not working 0 Answers
Roll a Ball Tutorial: Ball floats in the air when I enter play mode. 2 Answers