最簡單的一種就是先設置好攝像機跟物體的相對距離,
在腳本里就可以由物體的位置,跟相對距離,就可以求出攝像機的位置,
用插值的方法可以讓攝像機平滑跟隨。
1 public class CamFollow1 : MonoBehaviour 2 { 3 4 private Vector3 offset; 5 public Transform player; 6 7 void Start() 8 { 9 offset = player.position - transform.position; 10 } 11 12 void Update() 13 { 14 transform.position = Vector3.Lerp(transform.position, player.position - offset,Time.deltaTime*5); 15 } 16 }
實現一種注視的效果,可以對rotation做下處理
void Update() { transform.position = Vector3.Lerp(transform.position, player.position - offset,Time.deltaTime*5); Quaternion rotation = Quaternion.LookRotation(player.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation,Time.deltaTime*3f); }
這樣子,人物在走動的時候,攝像機就是一邊注視着人物,一邊跟着移動。
但是,當有牆體,或其他高大的物體遮擋住人物,

攝像機就無法看到人物,玩家自然也看不到,
在上面的基礎上做更近一步的擴展
在人物頭頂往下看,肯定可以看得到人物,
所以我們要做的就是把攝像機的位置向人物頭頂去調整,直到可以看到人物。
1 using UnityEngine; 2 using System.Collections; 3 4 public class CamFollow3 : MonoBehaviour 5 { 6 7 8 private Transform player;//英雄位置 9 private Vector3 offset; //攝像機和英雄相對位置 10 private float distance;//距離 11 private Vector3[] points = new Vector3[5];//5個點作為攝像機位置的選擇 12 13 private Vector3 targetPos;//篩選后的攝像機位置 14 15 void Awake() 16 { 17 player = GameObject.FindWithTag("Player").transform; 18 offset = transform.position - player.position; 19 distance = offset.magnitude; 20 } 21 22 void FixedUpdate() 23 { 24 //更新5個點的位置 25 points[0] = player.position + offset; 26 points[4] = player.position + Vector3.up*distance; 27 28 points[1] = Vector3.Lerp(points[0], points[4], 0.25f); 29 points[2] = Vector3.Lerp(points[0], points[4], 0.5f); 30 points[3] = Vector3.Lerp(points[0], points[4], 0.75f); 31 points[4] = Vector3.Lerp(points[0], points[4], 0.9f); 32 33 targetPos = FindCameraTarget(); 34 35 AdjustCamera(); 36 } 37 38 private Vector3 FindCameraTarget() 39 { 40 Vector3 result = points[points.Length - 1];//頭頂位置 41 42 //從低到高遍歷 43 for (int i = 0; i < points.Length; ++i) 44 { 45 if (IsHitPlayer(points[i], player.position)) 46 { 47 result = points[i]; 48 break; 49 } 50 } 51 52 return result; 53 } 54 55 private Ray ray; 56 private RaycastHit hit; 57 /// <summary> 58 /// 從origin發射一條射線檢測是否碰到player, 59 /// 碰到則表示攝像機在此位置可以看到player 60 /// </summary> 61 /// <param name="origin"></param> 62 /// <param name="target"></param> 63 /// <returns></returns> 64 bool IsHitPlayer(Vector3 origin, Vector3 target) 65 { 66 bool result = false; 67 68 Vector3 dir = target - origin; 69 ray = new Ray(origin,dir); 70 if (Physics.Raycast(ray, out hit)) 71 { 72 if (hit.transform.tag == "Player") 73 { 74 result = true; 75 } 76 } 77 return result; 78 } 79 80 /// <summary> 81 /// 調整攝像機位置 82 /// </summary> 83 void AdjustCamera() 84 { 85 86 transform.position = Vector3.Slerp(transform.position, targetPos, Time.deltaTime * 6); 87 88 Quaternion rotation = Quaternion.LookRotation(player.position - transform.position); 89 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 33f); 90 91 } 92 }
