Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. Unity UFO tutorial question about code
  2. 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:
  3.  
  4. using UnityEngine; using System.Collections;
  5.  
  6. public class CompleteCameraController : MonoBehaviour {
  7.  
  8. public GameObject player; //Public variable to store a reference to the player game object
  9. private Vector3 offset; //Private variable to store the offset distance between the player and camera
  10. // Use this for initialization
  11. void Start ()
  12. {
  13. //Calculate and store the offset value by getting the distance between the player's position and camera's position.
  14. offset = transform.position - player.transform.position;
  15. }
  16.  
  17. // LateUpdate is called after Update each frame
  18. void LateUpdate ()
  19. {
  20. // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
  21. transform.position = player.transform.position + offset;
  22. }
  23. }
  24.  
  25. ok so my question is why did we write : offset = transform.position - player.transform.position; and transform.position = player.transform.position + offset;
  26.  
  27. 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
  28.  
  29. yet , if i type the code that way
  30.  
  31. using UnityEngine; using System.Collections;
  32.  
  33. public class cameracontroller : MonoBehaviour {
  34.  
  35. public GameObject player;
  36.  
  37. // Update is called once per frame
  38. void LateUpdate () {
  39. transform.position = player.transform.position;
  40. }
  41. }
  42.  
  43. 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)
  44. thanks for all have a good day :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement