CameraTracer.cs

    xiaoxiao2021-04-15  55

    using UnityEngine; public class CameraTracer : MonoBehaviour { public GameObject PlayerObject;//设定玩家实体 public float MinViewScale = 0.5f;//最小缩放比例 public float MaxViewScale = 1.2f;//最大缩放比例 Vector2 m_lastTouchPos1 = Vector2.zero; Vector2 m_lastTouchPos2 = Vector2.zero; void Start() { m_lastTouchPos1 = Vector2.zero; m_lastTouchPos2 = Vector2.zero; } void FixedUpdate() { if (!PlayerObject.Equals(default(GameObject))) {//玩家存在时 transform.position = PlayerObject.transform.position;//调整相机位置 //鼠标滚轮调整 if (Input.GetAxis("Mouse ScrollWheel") != 0) { GetComponent<Camera>().orthographicSize -= Input.GetAxis("Mouse ScrollWheel"); resetZoom();//重置缩放以免超过限制 } //触屏缩放调整 //if (Input.touchCount > 1) //{ // if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved) // { // float distance = GetDistance(Input.GetTouch(0).position, Input.GetTouch(1).position); // if (Mathf.Abs(distance) > 300000) // { // if (distance > 0) // {//放大 // GetComponent<Camera>().orthographicSize -= 0.05f; // } // else // {//缩小 // GetComponent<Camera>().orthographicSize += 0.05f; // } // resetZoom();//重置缩放以免超过限制 // } // _lastTouchPos1 = Input.GetTouch(0).position; // _lastTouchPos2 = Input.GetTouch(1).position; // } // else // { // _lastTouchPos1 = Vector2.zero; // _lastTouchPos2 = Vector2.zero; // } //} } else {//死亡后镜头跟随 //鼠标滚轮调整 if (Input.mousePresent) {//支持鼠标 //Debug.Log(Input.mouseScrollDelta); if (Input.GetMouseButton(0)) { var aimPos = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y)); transform.position = aimPos / 3; } } else {//支持触屏 var aimPos = Camera.main.ScreenToWorldPoint(new Vector2(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y)); transform.position = aimPos / 3; } } } float getDistance(Vector2 pos1, Vector2 pos2) { if (m_lastTouchPos1 == Vector2.zero && m_lastTouchPos2 == Vector2.zero) { return 0.0f; } return (pos2 - pos1).sqrMagnitude - (m_lastTouchPos2 - m_lastTouchPos1).sqrMagnitude;//长度平方比较距离反应速度更快因为不需要算开根号 } void resetZoom() { var size = GetComponent<Camera>().orthographicSize; if (size < MinViewScale) { GetComponent<Camera>().orthographicSize = MinViewScale; } else if (size > MaxViewScale) { GetComponent<Camera>().orthographicSize = MaxViewScale; } } }
    转载请注明原文地址: https://ju.6miu.com/read-670896.html

    最新回复(0)