Unity 常用的几种相机跟随
来源:互联网 发布:c语言函数或 编辑:程序博客网 时间:2024/05/16 04:12
固定相机跟随
这种相机有一个参考对象,它会保持与该参考对象固定的位置,跟随改参考对象发生移动
using UnityEngine;using System.Collections;public class CameraFlow : MonoBehaviour{ public Transform target; private Vector3 offset; void Start() { offset = target.position - this.transform.position; } void Update() { this.transform.position = target.position - offset; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
固定相机跟随,带有角度旋转
这一种相机跟随是对第一种相机跟随的改进,在原有基础上面,添加了跟随角度的控制
using UnityEngine;using System.Collections;public class CameriaTrack : MonoBehaviour { private Vector3 offset = new Vector3(0,5,4); private Transform target; private Vector3 pos; public float speed = 2; void Start () { target = GameObject.FindGameObjectWithTag("Player").transform; } void Update () { pos = target.position + offset; this.transform.position = Vector3.Lerp(this.transform.position, pos, speed*Time.deltaTime); Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position); this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
第三人称相机
这种相机跟随,是第三人称角度看向对象的,也就是一直看向对象的后面,如一直显示玩家的后背
using UnityEngine;using System.Collections;public class CameraFlow : MonoBehaviour { public Transform target; public float distanceUp=15f; public float distanceAway = 10f; public float smooth = 2f; public float camDepthSmooth = 5f; void Start () { } void Update () { if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80) { Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime; } } void LateUpdate() { Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway; transform.position=Vector3.Lerp(transform.position,disPos,Time.deltaTime*smooth); transform.LookAt(target.position); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
相机跟随,鼠标控制移动和缩放
相机与观察对象保持一定距离,可以通过鼠标进行上下左右旋转,通过鼠标滚轮进行放大和缩小操作
using UnityEngine;using System.Collections;public class CameraFlow : MonoBehaviour{ public Transform target; Vector3 offset; void Start() { offset = transform.position - target.position; } void Update() { transform.position = target.position + offset; Rotate(); Scale(); } private void Scale() { float dis = offset.magnitude; dis += Input.GetAxis("Mouse ScrollWheel") * 5; Debug.Log("dis=" + dis); if (dis < 10 || dis > 40) { return; } offset = offset.normalized * dis; } private void Rotate() { if (Input.GetMouseButton(1)) { Vector3 pos = transform.position; Vector3 rot = transform.eulerAngles; transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * 10); transform.RotateAround(Vector3.zero, Vector3.left, Input.GetAxis("Mouse Y") * 10); float x = transform.eulerAngles.x; float y = transform.eulerAngles.y; Debug.Log("x=" + x); Debug.Log("y=" + y); if (x < 20 || x > 45 || y < 0 || y > 40) { transform.position = pos; transform.eulerAngles = rot; } offset = transform.position - target.position; } }}
0 0