|
@@ -11,7 +11,6 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
{
|
|
|
public Action beginDrag;
|
|
|
|
|
|
- public Transform target; // 目标对象,摄像头将朝向此对象
|
|
|
public float smoothSpeed = 0.125f; // 摄像头平滑移动的速度
|
|
|
public float scrollSensitivity = 5f; // 鼠标滚轮的灵敏度
|
|
|
public float rotateSpeed = 0.25f;
|
|
@@ -20,7 +19,6 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
public float maxDistance = 50f; // 摄像头与目标对象的最大距离
|
|
|
|
|
|
public float currentDistance; // 当前摄像头与目标对象的距离
|
|
|
- private Vector3 offset; // 摄像头与目标对象的偏移量
|
|
|
private Vector3 velocity = Vector3.zero; // 摄像头移动的速度
|
|
|
private bool isDragging = false; // 是否正在拖拽
|
|
|
private Vector3 lastMousePosition; // 上一帧鼠标位置
|
|
@@ -40,19 +38,13 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
public bool fixMoveRange = false;
|
|
|
public bool detailLod = false;
|
|
|
|
|
|
- bool isRotate = false;
|
|
|
-
|
|
|
bool onUI = false;
|
|
|
|
|
|
- private Vector3 lastMouseMapPoint;
|
|
|
private Camera _camera;
|
|
|
- private bool scrollTargetLock = false;
|
|
|
|
|
|
void Start()
|
|
|
{
|
|
|
_camera = this.GetComponent<Camera>();
|
|
|
- // 计算偏移量
|
|
|
- offset = transform.position - target.position;
|
|
|
}
|
|
|
|
|
|
private bool GetPointerOverUIElement(out GameObject uiElement)
|
|
@@ -110,11 +102,18 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
if (Input.GetAxis("Mouse ScrollWheel") != 0 && !onUI)
|
|
|
{
|
|
|
//定一个鼠标在地图上的位置
|
|
|
- var mousePos = Input.mousePosition;
|
|
|
- mousePos.z = math.abs(target.position.z - this.transform.position.z);
|
|
|
- lastMouseMapPoint = _camera.ScreenToWorldPoint(mousePos);
|
|
|
- scrollTargetLock = true;
|
|
|
-
|
|
|
+ var mousePosOnMap = Input.mousePosition;
|
|
|
+ mousePosOnMap.z = math.abs(-582 - this.transform.position.z);
|
|
|
+ mousePosOnMap = _camera.ScreenToWorldPoint(mousePosOnMap);
|
|
|
+
|
|
|
+ var cameraPosOnMap = this.transform.position;
|
|
|
+ cameraPosOnMap.z = -582;
|
|
|
+
|
|
|
+ var distance_y = Vector3.Distance(cameraPosOnMap, this.transform.position);
|
|
|
+ var distance_x = Vector3.Distance(cameraPosOnMap, mousePosOnMap);
|
|
|
+ var dir_x = (mousePosOnMap - cameraPosOnMap).normalized;
|
|
|
+
|
|
|
+
|
|
|
if (detailLod)
|
|
|
{
|
|
|
scrollSensitivity = math.clamp(currentDistance, 0, 10);
|
|
@@ -130,6 +129,17 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
{
|
|
|
OnDistanceChange?.Invoke(Input.GetAxis("Mouse ScrollWheel"));
|
|
|
}
|
|
|
+ Vector3 targetPosition = transform.position;
|
|
|
+ targetPosition.z = -582 - currentDistance;
|
|
|
+
|
|
|
+ var distance_y_after = Vector3.Distance(targetPosition, cameraPosOnMap);
|
|
|
+ var targetScale = distance_y_after / distance_y;
|
|
|
+
|
|
|
+ var moveDistance = (distance_x * targetScale)-distance_x;
|
|
|
+
|
|
|
+ var finalPos = targetPosition - dir_x * moveDistance;
|
|
|
+
|
|
|
+ transform.position = finalPos;
|
|
|
}
|
|
|
|
|
|
// 鼠标左键拖拽平移摄像头
|
|
@@ -143,7 +153,7 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
{
|
|
|
isDragging = false;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (isDragging)
|
|
|
{
|
|
|
// 计算鼠标移动的偏移量
|
|
@@ -151,82 +161,21 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
// 根据偏移量计算摄像头的目标位置
|
|
|
Vector3 moveDirection = Vector3.down * mouseOffset.y * translateSpeed +
|
|
|
Vector3.left * mouseOffset.x * translateSpeed;
|
|
|
- // 平滑移动摄像头到目标位置
|
|
|
- target.position += moveDirection;
|
|
|
+
|
|
|
+ Vector3 targetPosition=transform.position+ moveDirection;
|
|
|
//限制移动范围
|
|
|
if (fixMoveRange)
|
|
|
{
|
|
|
- Vector3 finalPos = target.localPosition;
|
|
|
+ Vector3 finalPos = targetPosition;
|
|
|
finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
|
|
|
finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
|
|
|
- target.localPosition = finalPos;
|
|
|
- }
|
|
|
-
|
|
|
- // 计算目标位置
|
|
|
- Vector3 targetPosition = target.position +
|
|
|
- Quaternion.Euler(new Vector3(rotateXAngle, 0, 0)) * offset.normalized *
|
|
|
- currentDistance;
|
|
|
-
|
|
|
- // 摄像头平滑移动到目标位置
|
|
|
- transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed * 2);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (isRotate)
|
|
|
- {
|
|
|
- Blink();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- // 计算目标位置
|
|
|
- Vector3 targetPosition = target.position +
|
|
|
- Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) *
|
|
|
- offset.normalized * currentDistance;
|
|
|
- // 摄像头平滑移动到目标位置
|
|
|
- //transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed);
|
|
|
- transform.position = targetPosition; //Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed);
|
|
|
-
|
|
|
- //if (Vector3.Distance(transform.position, targetPosition) < 1) {
|
|
|
- // transform.position = targetPosition;
|
|
|
- //}
|
|
|
- if (scrollTargetLock)
|
|
|
- {
|
|
|
- //现在鼠标位置在地图上的点
|
|
|
- var mousePos = Input.mousePosition;
|
|
|
- mousePos.z = math.abs(target.position.z - this.transform.position.z);
|
|
|
- var nowMouseMapPoint = _camera.ScreenToWorldPoint(mousePos);
|
|
|
- var mouseMoveDir = (nowMouseMapPoint - lastMouseMapPoint).normalized;
|
|
|
- var mouseMoveDistance = Vector3.Distance(nowMouseMapPoint, lastMouseMapPoint);
|
|
|
- target.position -= mouseMoveDir * mouseMoveDistance;
|
|
|
- if (fixMoveRange)
|
|
|
- {
|
|
|
- Vector3 finalPos = target.localPosition;
|
|
|
- finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
|
|
|
- finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
|
|
|
- target.localPosition = finalPos;
|
|
|
- }
|
|
|
- scrollTargetLock = false;
|
|
|
- }
|
|
|
+ targetPosition = finalPos;
|
|
|
}
|
|
|
+ transform.position = targetPosition;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
lastMousePosition = Input.mousePosition;
|
|
|
}
|
|
|
-
|
|
|
- public void Blink()
|
|
|
- {
|
|
|
- // 计算目标位置
|
|
|
- Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) *
|
|
|
- offset.normalized * currentDistance;
|
|
|
-
|
|
|
- // 摄像头平滑移动到目标位置
|
|
|
- transform.position = targetPosition;
|
|
|
-
|
|
|
- //摄像头朝向目标对象
|
|
|
- transform.rotation = Quaternion.LookRotation(target.position - this.transform.position, Vector3.up);
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
public void SetRange(float _minX, float _maxX, float _minY, float _maxY)
|
|
|
{
|
|
|
min_X = _minX;
|
|
@@ -237,10 +186,10 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
//限制移动范围
|
|
|
if (fixMoveRange)
|
|
|
{
|
|
|
- Vector3 finalPos = target.localPosition;
|
|
|
+ Vector3 finalPos = transform.position;
|
|
|
finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
|
|
|
finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
|
|
|
- target.localPosition = finalPos;
|
|
|
+ transform.position = finalPos;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -253,10 +202,7 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
{
|
|
|
fader.material.DOColor(Color.clear, 1.5f).onComplete = () => { fader.gameObject.SetActive(false); };
|
|
|
};
|
|
|
-
|
|
|
- this.target.localPosition = centerPos;
|
|
|
- DOTween.To(() => this.currentDistance, x => this.currentDistance = x, distance, 0.5f);
|
|
|
- this.Blink();
|
|
|
+ centerPos.z = -582 - currentDistance;
|
|
|
+ transform.position = centerPos;
|
|
|
}
|
|
|
-
|
|
|
}
|