Advertisement
Guest User

Camera follow player

a guest
Nov 12th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraFollow : MonoBehaviour {
  5. public Transform target;
  6.  // The position that that camera will be following.
  7. public float smoothing = 5f;
  8.  // The speed with which the camera will be following.
  9. Vector3 offset;
  10.  // The initial offset from the target.
  11.  
  12. void Start ()
  13.   {
  14.    // Calculate the initial offset.
  15.    offset = transform.position - target.position;
  16.   }
  17.  
  18. void FixedUpdate ()
  19.   {
  20.    // Create a postion the camera is aiming for based on the
  21.   offset from the target.
  22.   Vector3 targetCamPos = target.position + offset;
  23.    // Smoothly interpolate between the camera's current position
  24.   and it's target position.
  25.  transform.position = Vector3.Lerp (transform.position,
  26.  targetCamPos, smoothing * Time.deltaTime);
  27.  }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement