今天闲下来给大家实现个大型MMORPG项目中都会用到的功能
1.相机跟随玩家
2.左右滑动屏幕 旋转相机
3.上下滑动屏幕或使用鼠标滚轮 缩放相机及调整相机视野
上效果图:
角色移动脚本:
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 40 41 42 43 44 45 46 47 48 49 50 51 52 |
using UnityEngine; using System.Collections; public class MoveController : MonoBehaviour { Transform camera; Animator ani; NavMeshAgent nav; float h, v; Vector3 moveVec; // Use this for initialization void Start () { camera = Camera.main.transform; ani = GetComponent<Animator>(); nav = GetComponent<NavMeshAgent>(); } // Update is called once per frame void Update () { h = Input.GetAxis("Horizontal"); v = Input.GetAxis("Vertical"); moveVec = new Vector3(h, 0, v); if (h != 0 || v != 0) { ani.SetBool("Run", true); // 根据摄像机方向 进行移动 moveVec = Quaternion.Euler(0, camera.eulerAngles.y, 0) * moveVec; nav.Move(moveVec * Time.deltaTime * 5); RotatePlayer(); } else { ani.SetBool("Run", false); } } private void RotatePlayer() { Vector3 vec = Quaternion.Euler(0, 0, 0) * moveVec; Quaternion qua = Quaternion.LookRotation(vec); transform.rotation = Quaternion.Lerp(transform.rotation, qua, Time.deltaTime * 100); } } |
相机控制脚本:
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
using UnityEngine; public class ThirdCamera : MonoBehaviour { public Transform target = null; // 目标玩家 [SerializeField] [Range(0, 360)] float horizontalAngle = 270f; // 水平角度 [SerializeField] [Range(0, 20)] float initialHeight = 2f; // 人物在视野内屏幕中的位置设置 [SerializeField] [Range(10, 90)] float initialAngle = 40f; // 初始俯视角度 [SerializeField] [Range(10, 90)] float maxAngle = 50f; // 最高俯视角度 [SerializeField] [Range(10, 90)] float minAngle = 35f; // 最低俯视角度 float initialDistance; // 初始化相机与玩家的距离 根据角度计算 [SerializeField] [Range(1, 100)] float maxDistance = 20f; // 相机距离玩家最大距离 [SerializeField] [Range(1, 100)] float minDistance = 5f; // 相机距离玩家最小距离 [SerializeField] [Range(1, 100)] float zoomSpeed = 50; // 缩放速度 [SerializeField] [Range(1f, 200)] float swipeSpeed = 50; // 左右滑动速度 float scrollWheel; // 记录滚轮数值 float tempAngle; // 临时存储摄像机的初始角度 Vector3 tempVector = new Vector3(); void Start() { InitCamera(); } void Update() { ZoomCamera(); SwipeScreen(); } void LateUpdate() { FollowPlayer(); RotateCamera(); } /// <summary> /// 初始化 相机与玩家距离 /// </summary> void InitCamera() { tempAngle = initialAngle; initialDistance = Mathf.Sqrt((initialAngle - minAngle) / Calculate()) + minDistance; initialDistance = Mathf.Clamp(initialDistance, minDistance, maxDistance); } /// <summary> /// 相机跟随玩家 /// </summary> void FollowPlayer() { float upRidus = Mathf.Deg2Rad * initialAngle; float flatRidus = Mathf.Deg2Rad * horizontalAngle; float x = initialDistance * Mathf.Cos(upRidus) * Mathf.Cos(flatRidus); float z = initialDistance * Mathf.Cos(upRidus) * Mathf.Sin(flatRidus); float y = initialDistance * Mathf.Sin(upRidus); transform.position = Vector3.zero; tempVector.Set(x, y, z); tempVector = tempVector + target.position; transform.position = tempVector; tempVector.Set(target.position.x, target.position.y + initialHeight, target.position.z); transform.LookAt(tempVector); } /// <summary> /// 缩放相机与玩家距离 /// </summary> void ZoomCamera() { scrollWheel = GetZoomValue(); if (scrollWheel != 0) { tempAngle = initialAngle - scrollWheel * 2 * (maxAngle - minAngle); tempAngle = Mathf.Clamp(tempAngle, minAngle, maxAngle); } if (tempAngle != initialAngle) { initialAngle = Mathf.Lerp(initialAngle, tempAngle, Time.deltaTime * 10); initialDistance = Mathf.Sqrt((initialAngle - minAngle) / Calculate()) + minDistance; initialDistance = Mathf.Clamp(initialDistance, minDistance, maxDistance); } } float Calculate() { float dis = maxDistance - minDistance; float ang = maxAngle - minAngle; float line = ang / (dis * dis); return line; } bool isMousePress = false; Vector2 oldMousePos; Vector2 newMousePos; Vector2 mousePosOffset; /// <summary> /// 滑动屏幕 旋转相机和缩放视野 /// </summary> public void SwipeScreen() { if (Input.GetMouseButtonDown(0)) { oldMousePos = Vector2.zero; isMousePress = true; } else if(Input.GetMouseButtonUp(0)) { mousePosOffset = Vector2.zero; isMousePress = false; } if (!isMousePress) return; newMousePos = Input.mousePosition; if(oldMousePos != Vector2.zero) { mousePosOffset = newMousePos - oldMousePos; } oldMousePos = newMousePos; } /// <summary> /// 获取缩放视野数值 1.鼠标滚轮 2.屏幕上下滑动 /// </summary> /// <returns></returns> float GetZoomValue() { float zoomValue = 0; // 使用鼠标滚轮 if (Input.GetAxis("Mouse ScrollWheel") != 0) { zoomValue = Input.GetAxis("Mouse ScrollWheel"); } else if (mousePosOffset != Vector2.zero) { zoomValue = mousePosOffset.y * Time.deltaTime * zoomSpeed * 0.01f; } return zoomValue; } float xVelocity = 0; /// <summary> /// 旋转相机 /// </summary> void RotateCamera() { horizontalAngle = Mathf.SmoothDamp(horizontalAngle, horizontalAngle + mousePosOffset.x * Time.deltaTime * swipeSpeed, ref xVelocity, 0.1f); } } |
相机参数已测试参考值:
项目使用版本:Unity5.3.4 GitHub下载地址:
https://github.com/654306663/CameraOperate
- 本文固定链接: http://www.u3d8.com/?p=1235
- 转载请注明: 网虫虫 在 u3d8.com 发表过