首页 > 代码库 > unity 常用的几种相机跟随
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
unity 常用的几种相机跟随
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。