
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 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraController : MonoBehaviour { private float cameraMoveLerpTime = 8f; private float cameraRotateLerpTime = 8f; private Vector3 cameraPositionTemp; // Start is called before the first frame update void Start() { cameraPositionTemp = transform.position; } // Update is called once per frame void Update() { Move(); Zoom(); cameraPositionTemp = new Vector3(Mathf.Clamp(cameraPositionTemp.x, moveLimitX[0], moveLimitX[1]) , Mathf.Clamp(cameraPositionTemp.y, zoomMinPositionY, zoomMaxPositionY) , Mathf.Clamp(cameraPositionTemp.z, moveLimitZ[0], moveLimitZ[1])); float rotateX = (cameraPositionTemp.y - zoomMinPositionY) / (zoomMaxPositionY - zoomMinPositionY) * (zoomMaxRotateX - zoomMinRotateX) + zoomMinRotateX; transform.position = Vector3.Lerp(transform.position, cameraPositionTemp, cameraMoveLerpTime * Time.deltaTime); transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, new Vector3(rotateX, 0, 0), cameraRotateLerpTime * Time.deltaTime); } [SerializeField] private Vector2 moveLimitX = new Vector2(-200, 200); [SerializeField] private Vector2 moveLimitZ = new Vector2(-200, 200); private float moveCo = 0.1f; private Vector3 oldMousePos; private void Move() { #if !UNITY_EDITOR if (Input.touchCount != 1) { return; } Touch touch1 = Input.GetTouch (0); if (touch1.phase == TouchPhase.Moved) { Vector3 delta = touch1.deltaPosition * moveCo; cameraPositionTemp -= new Vector3(delta.x, 0, delta.y); } #else if (Input.GetMouseButtonDown(0)) { oldMousePos = Input.mousePosition; } if (Input.GetMouseButton(0)) { Vector3 deltaPosition = Input.mousePosition - oldMousePos; Vector3 delta = new Vector2(deltaPosition.x, deltaPosition.y) * moveCo; cameraPositionTemp -= new Vector3(delta.x, 0, delta.y); oldMousePos = Input.mousePosition; } #endif } [SerializeField] private float zoomMaxPositionY = 60; [SerializeField] private float zoomMinPositionY = 10; [SerializeField] private float zoomMaxRotateX = 70; [SerializeField] private float zoomMinRotateX = 50; Touch oldTouch1; //上次触摸点1(手指1) Touch oldTouch2; //上次触摸点2(手指2) private float zoomScaleCo = 0.3f; //缩放系数 private void Zoom() { #if !UNITY_EDITOR if (Input.touchCount <= 1) { return; } Touch touch1 = Input.GetTouch (0); Touch touch2 = Input.GetTouch (1); if (touch2.phase == TouchPhase.Began) { oldTouch2 = touch2; oldTouch1 = touch1; return; } if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved) { float oldDistance = Vector2.Distance (oldTouch2.position, oldTouch1.position); float curDistance = Vector2.Distance (touch2.position, touch1.position); float delta = (curDistance - oldDistance) * zoomScaleCo; cameraPositionTemp -= new Vector3(0, delta, 0); oldTouch1 = touch1; oldTouch2 = touch2; } #else cameraPositionTemp -= new Vector3(0, Input.mouseScrollDelta.y * 10f * zoomScaleCo, 0); #endif } } |
- 本文固定链接: http://www.u3d8.com/?p=2251
- 转载请注明: 网虫虫 在 u3d8.com 发表过