unity 常用的幾種相機跟隨


固定相機跟隨

這種相機有一個參考對象,它會保持與該參考對象固定的位置,跟隨改參考對象發生移動

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class CameraFlow : MonoBehaviour
 5 {
 6     public Transform target;
 7     private Vector3 offset;
 8     // Use this for initialization
 9     void Start()
10     {
11         offset = target.position - this.transform.position;
12 
13     }
14 
15     // Update is called once per frame
16     void Update()
17     {
18         this.transform.position = target.position - offset;
19     }
20 }

固定相機跟隨,帶有角度旋轉

這一種相機跟隨是對第一種相機跟隨的改進,在原有基礎上面,添加了跟隨角度的控制

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class CameriaTrack : MonoBehaviour {
 5     private Vector3 offset = new Vector3(0,5,4);//相機相對於玩家的位置
 6     private Transform target;
 7     private Vector3 pos;
 8 
 9     public float speed = 2;
10 
11     // Use this for initialization
12     void Start () {
13         target = GameObject.FindGameObjectWithTag("Player").transform;
14 
15     }
16 
17     // Update is called once per frame
18     void Update () {
19         pos = target.position + offset;
20         this.transform.position = Vector3.Lerp(this.transform.position, pos, speed*Time.deltaTime);//調整相機與玩家之間的距離
21         Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position);//獲取旋轉角度
22         this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime);
23 
24     }
25 }

第三人稱相機

這種相機跟隨,是第三人稱角度看向對象的,也就是一直看向對象的后面,如一直顯示玩家的后背

 1 using UnityEngine;
 2 using System.Collections;
 3 //相機一直拍攝主角的后背
 4 public class CameraFlow : MonoBehaviour {
 5 
 6     public Transform target;
 7 
 8 
 9     public float distanceUp=15f;
10     public float distanceAway = 10f;
11     public float smooth = 2f;//位置平滑移動值
12     public float camDepthSmooth = 5f;
13     // Use this for initialization
14     void Start () {
15 
16     }
17 
18     // Update is called once per frame
19     void Update () {
20        // 鼠標軸控制相機的遠近
21         if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80)
22         {
23             Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime;
24         }
25 
26     }
27 
28     void LateUpdate()
29     {
30        //相機的位置
31         Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway;
32         transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*smooth);
33         //相機的角度
34         transform.LookAt(target.position);
35     }
36 
37 
38 }

相機跟隨,鼠標控制移動和縮放

相機與觀察對象保持一定距離,可以通過鼠標進行上下左右旋轉,通過鼠標滾輪進行放大和縮小操作

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class CameraFlow : MonoBehaviour
 5 {
 6     public Transform target;
 7     Vector3 offset;
 8     // Use this for initialization
 9     void Start()
10     {
11         offset = transform.position - target.position;
12     }
13 
14     // Update is called once per frame
15     void Update()
16     {
17         transform.position = target.position + offset;
18         Rotate();
19         Scale();
20     }
21     //縮放
22     private void Scale()
23     {
24         float dis = offset.magnitude;
25         dis += Input.GetAxis("Mouse ScrollWheel") * 5;
26         Debug.Log("dis=" + dis);
27         if (dis < 10 || dis > 40)
28         {
29             return;
30         }
31         offset = offset.normalized * dis;
32     }
33     //左右上下移動
34     private void Rotate()
35     {
36         if (Input.GetMouseButton(1))
37         {
38             Vector3 pos = transform.position;
39             Vector3 rot = transform.eulerAngles;
40 
41             //圍繞原點旋轉,也可以將Vector3.zero改為 target.position,就是圍繞觀察對象旋轉
42             transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * 10);
43             transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * 10);
44             float x = transform.eulerAngles.x;
45             float y = transform.eulerAngles.y;
46             Debug.Log("x=" + x);
47             Debug.Log("y=" + y);
48             //控制移動范圍
49             if (x < 20 || x > 45 || y < 0 || y > 40)
50             {
51                 transform.position = pos;
52                 transform.eulerAngles = rot;
53             }
54             //  更新相對差值
55             offset = transform.position - target.position;
56         }
57 
58     }
59 }

轉載:http://blog.csdn.net/u011484013/article/details/51554745


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM