Unity 控制摄像机跟随运动物体

把以下代码绑定到摄像机

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class FollowTarget : MonoBehaviour {  
  5.   
  6.     public Transform playerTransform;  //把运动物体拖放到此处  
  7.     private Vector3 offset;  
  8.   
  9.     // Use this for initialization  
  10.     void Start () {  
  11.   
  12.       offset =  transform.position - playerTransform.position;  //计算初始物体与相机的偏移距离  
  13.   
  14.     }  
  15.       
  16.     // Update is called once per frame  
  17.     void Update () {  
  18.         transform.position =  playerTransform.position + offset;   //运动物体当前位置 加上 原始偏移  
  19.     }  

你可能感兴趣的:(Unity,游戏开发)