|
@@ -39,12 +39,18 @@ 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;
|
|
|
}
|
|
@@ -71,6 +77,7 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -79,28 +86,35 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
bool specialUI = false;
|
|
|
if (GetPointerOverUIElement(out GameObject uiElement))
|
|
|
{
|
|
|
-
|
|
|
// 获取 UI 元素的类型
|
|
|
if (uiElement.TryGetComponent<Button>(out Button button))
|
|
|
{
|
|
|
-
|
|
|
- if (uiElement.transform.TryGetComponent<RuntimePoint>(out RuntimePoint rp)) {
|
|
|
+ if (uiElement.transform.TryGetComponent<RuntimePoint>(out RuntimePoint rp))
|
|
|
+ {
|
|
|
specialUI = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
// 检查当前鼠标位置是否在 UI 上
|
|
|
return EventSystem.current.IsPointerOverGameObject() && !specialUI;
|
|
|
}
|
|
|
+
|
|
|
void LateUpdate()
|
|
|
{
|
|
|
onUI = false;
|
|
|
if (IsPointerOverUIElement()) onUI = true;
|
|
|
|
|
|
- translateSpeed = currentDistance / 1000.0f;
|
|
|
+ translateSpeed = currentDistance / 1000.0f;
|
|
|
// 鼠标滚轮控制摄像头远近
|
|
|
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;
|
|
|
+
|
|
|
if (detailLod)
|
|
|
{
|
|
|
scrollSensitivity = math.clamp(currentDistance, 0, 10);
|
|
@@ -116,30 +130,8 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
{
|
|
|
OnDistanceChange?.Invoke(Input.GetAxis("Mouse ScrollWheel"));
|
|
|
}
|
|
|
-
|
|
|
- if (Input.GetAxis("Mouse ScrollWheel") > 0)
|
|
|
- {
|
|
|
- //Debug.Log($"{Input.mousePosition} {Screen.width*0.5f}:{Screen.height*0.5f}");
|
|
|
- float offSetX = (Screen.width * 0.5f-Input.mousePosition.x) / Screen.width;
|
|
|
- float offSetY = (Screen.height * 0.5f-Input.mousePosition.y) / Screen.height;
|
|
|
- Vector3 moveDirection = Vector3.down * offSetY*translateSpeed * 110 * 9.0f / 16.0f + Vector3.left * offSetX* translateSpeed * 110;
|
|
|
- // 平滑移动摄像头到目标位置
|
|
|
- float cameraDistanceRange = Mathf.Clamp(currentDistance * 1.0f / maxDistance * 1.0f, 0.5f, 0.9f);
|
|
|
- target.position += moveDirection*cameraDistanceRange;
|
|
|
- //限制移动范围
|
|
|
- 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;
|
|
|
- }
|
|
|
- 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);
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 鼠标左键拖拽平移摄像头
|
|
|
if (Input.GetMouseButtonDown(1) && !onUI)
|
|
|
{
|
|
@@ -157,28 +149,29 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
// 计算鼠标移动的偏移量
|
|
|
Vector3 mouseOffset = Input.mousePosition - lastMousePosition;
|
|
|
// 根据偏移量计算摄像头的目标位置
|
|
|
- Vector3 moveDirection = Vector3.down * mouseOffset.y * translateSpeed + Vector3.left * mouseOffset.x * translateSpeed;
|
|
|
+ Vector3 moveDirection = Vector3.down * mouseOffset.y * translateSpeed +
|
|
|
+ Vector3.left * mouseOffset.x * translateSpeed;
|
|
|
// 平滑移动摄像头到目标位置
|
|
|
target.position += moveDirection;
|
|
|
//限制移动范围
|
|
|
if (fixMoveRange)
|
|
|
{
|
|
|
- Vector3 finalPos=target.localPosition;
|
|
|
+ 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;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 计算目标位置
|
|
|
- Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, 0, 0)) * offset.normalized * currentDistance;
|
|
|
+ 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();
|
|
@@ -186,16 +179,35 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
else
|
|
|
{
|
|
|
// 计算目标位置
|
|
|
- Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
|
|
|
-
|
|
|
+ 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 = 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;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|
|
@@ -205,27 +217,27 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
public void Blink()
|
|
|
{
|
|
|
// 计算目标位置
|
|
|
- Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
|
|
|
+ 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)
|
|
|
+ public void SetRange(float _minX, float _maxX, float _minY, float _maxY)
|
|
|
{
|
|
|
min_X = _minX;
|
|
|
max_X = _maxX;
|
|
|
min_Y = _minY;
|
|
|
max_Y = _maxY;
|
|
|
-
|
|
|
+
|
|
|
//限制移动范围
|
|
|
if (fixMoveRange)
|
|
|
{
|
|
|
- Vector3 finalPos=target.localPosition;
|
|
|
+ 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;
|
|
@@ -233,26 +245,21 @@ public class CameraBirdSec : MonoBehaviour
|
|
|
}
|
|
|
|
|
|
|
|
|
- public void SetCameraToCenterFade(Vector3 centerPos,float distance = 300)
|
|
|
+ public void SetCameraToCenterFade(Vector3 centerPos, float distance = 300)
|
|
|
{
|
|
|
MeshRenderer fader = this.transform.GetChild(0).GetComponent<MeshRenderer>();
|
|
|
fader.gameObject.SetActive(true);
|
|
|
fader.material.DOColor(Color.white, 0.1f).onComplete = () =>
|
|
|
{
|
|
|
- fader.material.DOColor(Color.clear, 1.5f).onComplete = () =>
|
|
|
- {
|
|
|
- fader.gameObject.SetActive(false);
|
|
|
- };
|
|
|
-
|
|
|
+ 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);
|
|
|
+ DOTween.To(() => this.currentDistance, x => this.currentDistance = x, distance, 0.5f);
|
|
|
this.Blink();
|
|
|
}
|
|
|
|
|
|
void Update()
|
|
|
{
|
|
|
-
|
|
|
}
|
|
|
-}
|
|
|
+}
|