塔防游戏的相机
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class Cameracontroller : MonoBehaviour { public float mouseSpeed=0.05f; public float rotateSpeed=100; public float scrollwheelSpeed=200; public float moveSpeed=0.1f; public float lerpSpeed = 0.5f; public float lookSpeed=6; Vector3 mousePos; public static Action<GameObject> selectTower; public static Action<GameObject> exitSelectTower; [HideInInspector] public GameObject currSelectObj; void LateUpdate() { MoveRotation(); DragMove(); ScrollMove(); CheckTower(); MoveToTower(); RotateToTower(); } /// <summary> /// 鼠标右键控制移动旋转 /// </summary> private void MoveRotation() { if (Input.GetKeyDown(KeyCode.Mouse1)) { ResetTower(); } if (Input.GetKey(KeyCode.Mouse1)) { float mouseX = Input.GetAxis("Mouse X"); float mouseY = Input.GetAxis("Mouse Y"); transform.Rotate(Vector3.up, Time.deltaTime * mouseX * rotateSpeed, Space.World); transform.Rotate(transform.right, -Time.deltaTime * mouseY * rotateSpeed, Space.World); Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); Vector3 ms = new Vector3(input.x, 0, input.y) * moveSpeed; transform.Translate(ms, Space.Self); } } /// <summary> /// 拖拽中键移动 /// </summary> private void DragMove() { if (Input.GetKeyDown(KeyCode.Mouse2)) { ResetTower(); mousePos = Input.mousePosition; } if (Input.GetKey(KeyCode.Mouse2)) { Vector3 deltPos = Input.mousePosition - mousePos; deltPos = -deltPos; transform.Translate(deltPos * 0.1f, Space.Self); mousePos = Input.mousePosition; } } /// <summary> /// 滚动中键移动 /// </summary> private void ScrollMove() { float scrollwhell = Input.GetAxis("Mouse ScrollWheel"); transform.Translate(Vector3.forward * Time.deltaTime * scrollwhell * scrollwheelSpeed); } /// <summary> /// 选择塔 /// </summary> private void CheckTower() { RaycastHit hit = default(RaycastHit); if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000)) { if (Input.GetKeyUp(KeyCode.Mouse0)) { if (hit.collider.tag == "TowerBase") { currSelectObj = hit.collider.gameObject; if (selectTower != null) { selectTower(currSelectObj); } toMove = true; toRot = true; } } } } /// <summary> /// 移动到塔 /// </summary> bool toMove = false; private void MoveToTower() { if (!toMove) return; transform.position = Vector3.Lerp(transform.position, currSelectObj.transform.position, Time.deltaTime* lerpSpeed); if (Vector3.Distance(currSelectObj.transform.position, transform.position)<10) toMove = false; } /// <summary> /// 选择向塔 /// </summary> bool toRot = false; private void RotateToTower() { if (!toRot) return; Quaternion lookAt = Quaternion.LookRotation((currSelectObj.transform.position - transform.position).normalized); transform.rotation = Quaternion.Lerp(transform.rotation, lookAt, Time.deltaTime * lerpSpeed * 6); float angle = Quaternion.Angle(transform.rotation, lookAt); if (angle<1) toRot = false; } /// <summary> /// 设置选择塔为null /// </summary> /// <param name="go"></param> public void ResetTower() { if (exitSelectTower != null) exitSelectTower(currSelectObj); currSelectObj = null; } }