原创,转载请注明出处@JeroChan
学习了siki的黑暗之光,在学习老师的实现代码之余自己网上还查找了其他的实现方法,为了以后项目方便自己独立封装了旋转、拉伸的方法,使耦合度尽量降低。
定义
private GameObject playerObject;
private Camera cameraObject;
private Vector3 offSetPos; //偏移(相机与目标的差)
public float fieldViewMax = 90f; //拉伸距离最远距离
public float fieldViewMin = 10f; //拉伸距离最近距离
public float scrollSpeed = 40f; //拉伸速度
public float rotateSpeed = 10f; //旋转速度
private bool isRotate = false; //旋转状态标识符
跟随及主代码
使用LateUpdate控制相机,在Update处理完角色逻辑后LateUpdate处理相机逻辑,防止相机跟丢角色。
void Start () { cameraObject = this.GetComponent<Camera>(); playerObject = GameObject.FindGameObjectWithTag("Player"); offSetPos = this.transform.position - playerObject.transform.position; transform.LookAt(playerObject.transform); } void LateUpdate () { //相机跟随(使用固定偏移方法) this.transform.position = offSetPos + playerObject.transform.position; //视野拉伸(使用Camera.fieldOfView方法) ScrollOfView(cameraObject,fieldViewMin, fieldViewMax, scrollSpeed); //视野旋转(使用Transform.RotateAround方法) RotateOfView(cameraObject.transform, playerObject.transform,ref offSetPos,ref isRotate,rotateSpeed); }
相机视野拉伸代码
滚轮控制相机视野拉伸(挂载相机上) _camera ---->相机 _fieldViewMin ---->最近视野 _fieldViewMax ---->最大视野 _scrollSpeed ---->拉伸速度(默认40)
void ScrollOfView(Camera _camera, float _fieldViewMin, float _fieldViewMax,float _scrollSpeed=40) { _camera.fieldOfView -= Input.GetAxis("Mouse ScrollWheel") * _scrollSpeed; _camera.fieldOfView = Mathf.Clamp(_camera.fieldOfView, _fieldViewMin, _fieldViewMax); }
相机视野旋转代码
右键拖动旋转视野(挂载相机上) _cameraTransform ---->相机的transform _targetTransform ---->目标的transform _offSetPos ---->相机与目标的偏移位置(可传递) _isRotate ---->旋转状态标识符(可专递) _rotateSpeed ---->旋转速度(默认为10)
void RotateOfView(Transform _cameraTransform, Transform _targetTransform,ref Vector3 _offSetPos,ref bool _isRotate,float _rotateSpeed=10) { //当鼠标右键按下切换旋转状态,抬起表示结束旋转状态 if (Input.GetMouseButtonDown(1)) _isRotate = true; if (Input.GetMouseButtonUp(1)) _isRotate = false; //旋转状态 if (_isRotate) { Vector3 originalPosition = _cameraTransform.position; Quaternion originalRotation = _cameraTransform.rotation; _cameraTransform.RotateAround(_targetTransform.position, _targetTransform.up, _rotateSpeed * Input.GetAxis("Mouse X")); _cameraTransform.RotateAround(_targetTransform.position, _cameraTransform.right, -_rotateSpeed * Input.GetAxis("Mouse Y")); //限制上下旋转角度 if (_cameraTransform.eulerAngles.x > 80 || _cameraTransform.eulerAngles.x < 10) { _cameraTransform.position = originalPosition; _cameraTransform.rotation = originalRotation; } //更新偏移 _offSetPos = _cameraTransform.position - _targetTransform.position; } }
函数写的相对直观,就不多做笔记表明了,欢迎大神有更好的方法请指教。
最后附上PlayerFollow.cs源码
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class FollowPlayer : MonoBehaviour { 6 7 8 private GameObject playerObject; 9 private Camera cameraObject; 10 private Vector3 offSetPos; //偏移(相机与目标的差) 11 public float fieldViewMax = 90f; //拉伸距离最远距离 12 public float fieldViewMin = 10f; //拉伸距离最近距离 13 public float scrollSpeed = 40f; //拉伸速度 14 public float rotateSpeed = 10f; //旋转速度 15 private bool isRotate = false; //旋转状态标识符 16 17 18 void Start () { 19 20 cameraObject = this.GetComponent<Camera>(); 21 playerObject = GameObject.FindGameObjectWithTag("Player"); 22 offSetPos = this.transform.position - playerObject.transform.position; 23 transform.LookAt(playerObject.transform); 24 } 25 26 27 void LateUpdate () 28 { 29 //相机跟随(使用固定偏移方法) 30 this.transform.position = offSetPos + playerObject.transform.position; 31 //视野拉伸(使用Camera.fieldOfView方法) 32 ScrollOfView(cameraObject,fieldViewMin, fieldViewMax, scrollSpeed); 33 //视野旋转(使用Transform.RotateAround方法) 34 RotateOfView(cameraObject.transform, playerObject.transform,ref offSetPos,ref isRotate,rotateSpeed); 35 } 36 37 38 39 40 41 /// <summary> 42 /// 滚轮控制相机视野拉伸(挂载相机上) 43 /// </summary> 44 /// <param name="_camera">相机</param> 45 /// <param name="_fieldViewMin">最近视野</param> 46 /// <param name="_fieldViewMax">最大视野</param> 47 /// <param name="_scrollSpeed">拉伸速度(默认40)</param> 48 void ScrollOfView(Camera _camera, float _fieldViewMin, float _fieldViewMax,float _scrollSpeed=40) 49 { 50 _camera.fieldOfView -= Input.GetAxis("Mouse ScrollWheel") * _scrollSpeed; 51 _camera.fieldOfView = Mathf.Clamp(_camera.fieldOfView, _fieldViewMin, _fieldViewMax); 52 } 53 54 55 56 57 /// <summary> 58 /// 右键拖动旋转视野(挂载相机上) 59 /// </summary> 60 /// <param name="_cameraTransform">相机的transform</param> 61 /// <param name="_targetTransform">目标的transform</param> 62 /// <param name="_offSetPos">相机与目标的偏移位置(可传递)</param> 63 /// <param name="_isRotate">旋转状态标识符(可专递)</param> 64 /// <param name="_rotateSpeed">旋转速度(默认为10)</param> 65 void RotateOfView(Transform _cameraTransform, Transform _targetTransform,ref Vector3 _offSetPos,ref bool _isRotate,float _rotateSpeed=10) 66 { 67 //当鼠标右键按下切换旋转状态,抬起表示结束旋转状态 68 if (Input.GetMouseButtonDown(1)) 69 _isRotate = true; 70 if (Input.GetMouseButtonUp(1)) 71 _isRotate = false; 72 //旋转状态 73 if (_isRotate) 74 { 75 Vector3 originalPosition = _cameraTransform.position; 76 Quaternion originalRotation = _cameraTransform.rotation; 77 _cameraTransform.RotateAround(_targetTransform.position, _targetTransform.up, _rotateSpeed * Input.GetAxis("Mouse X")); 78 _cameraTransform.RotateAround(_targetTransform.position, _cameraTransform.right, -_rotateSpeed * Input.GetAxis("Mouse Y")); 79 //限制上下旋转角度 80 if (_cameraTransform.eulerAngles.x > 80 || _cameraTransform.eulerAngles.x < 10) 81 { 82 _cameraTransform.position = originalPosition; 83 _cameraTransform.rotation = originalRotation; 84 } 85 //更新偏移 86 _offSetPos = _cameraTransform.position - _targetTransform.position; 87 } 88 } 89 90 }
PlayerFollow.cs
时间: 2024-10-10 04:46:43