Shamba

Player follow

Aug 7th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerFollow : MonoBehaviour {
  6.  
  7.     public Transform PlayerTransform;
  8.  
  9.     private Vector3 _cameraOffset;
  10.  
  11.     private GameObject Player;
  12.  
  13.     [Range(0.01f, 1.0f)]
  14.     public float SmoothFactor = 0.5f;
  15.  
  16.     public bool LookAtPlayer = false;
  17.  
  18.     // Use this for initialization
  19.     void Start ()
  20.     {
  21.         _cameraOffset = transform.position - PlayerTransform.position; 
  22.     }
  23.    
  24.     // LateUpdate is called after Update
  25.     void Update () {
  26.         if(PlayerTransform == null)
  27.         {
  28.             Player = GameObject.FindGameObjectWithTag("Player");
  29.             PlayerTransform = Player.GetComponent<Transform>();
  30.             _cameraOffset = transform.position - PlayerTransform.position;
  31.         }
  32.  
  33.  
  34.         Vector3 newPos = PlayerTransform.position + _cameraOffset;
  35.  
  36.         transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
  37.  
  38.         if (LookAtPlayer)
  39.             transform.LookAt(PlayerTransform);
  40.  
  41.     }
  42. }
Add Comment
Please, Sign In to add comment