unity implements camera follow

  • 2021-01-18 06:37:03
  • OfStack

The code is very simple, so I won't talk too much here, but I'll just give you the code


using UnityEngine;
using System.Collections;
 
public class FllowTarget : MonoBehaviour {
 
  public Transform character;  // The person the camera is going to follow 
  public float smoothTime = 0.01f; // The time the camera moves smoothly 
  private Vector3 cameraVelocity = Vector3.zero;
  private Camera mainCamera; // Main Camera (Sometimes there are multiple cameras in a project, but only one 1 A main camera.) 
  
  void Awake () 
  { 
   mainCamera = Camera.main;
  }
 
  void Update()
  {
    transform.position = Vector3.SmoothDamp(transform.position, character.position + new Vector3(0, 0, -5), ref cameraVelocity, smoothTime);
  }
  
}

That's the end of this article. I hope you enjoy it and find it helpful for you to learn unity.


Related articles: