Advertisement
LeeMace

Camera follow player

Nov 29th, 2022 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | Gaming | 0 0
  1. //From Unity Learn
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class FollowPlayer : MonoBehaviour
  8. {
  9.     public GameObject player;
  10.     [SerializeField] Vector3 offset = new Vector3(0, 5, -10);
  11.  
  12.     void LateUpdate()
  13.     {
  14.         transform.position = player.transform.position + offset;
  15.     }
  16. }
  17.  
  18. //From Ferrone H. Learning C# by Developing Games with Unity...7ed 2022
  19. using System.Collections;
  20. using System.Collections.Generic;
  21. using UnityEngine;
  22.  
  23. public class CameraBehaviour : MonoBehaviour
  24. {
  25.  
  26.     public Vector3 camOffset = new Vector3(0, 1.2f, -2.6f);
  27.     private Transform target;
  28.     void Start()
  29.     {
  30.         target = GameObject.Find("Player").transform;
  31.     }
  32.  
  33.     void LateUpdate()
  34.     {
  35.         transform.position = target.TransformPoint(camOffset);
  36.         transform.LookAt(target);
  37.     }
  38. }
Tags: camera
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement