FollowPlayer.cs 878 B

12345678910111213141516171819202122232425262728293031
  1. using UnityEngine;
  2. public class FollowPlayer : MonoBehaviour
  3. {
  4. private GameObject player;
  5. // Start is called once before the first execution of Update after the MonoBehaviour is created
  6. void Start()
  7. {
  8. player = GameObject.Find("Player");
  9. if (!player) return;
  10. SetLocation();
  11. SetRotation();
  12. }
  13. // Update is called once per frame
  14. void Update()
  15. {
  16. SetLocation();
  17. SetRotation();
  18. }
  19. private void SetLocation()
  20. {
  21. gameObject.transform.position = new Vector3(player.transform.position.x, gameObject.transform.position.y, player.transform.position.z);
  22. }
  23. private void SetRotation()
  24. {
  25. gameObject.transform.rotation = Quaternion.Euler(gameObject.transform.rotation.eulerAngles.x, player.transform.rotation.eulerAngles.y, player.transform.rotation.eulerAngles.z);
  26. }
  27. }