fix:代码提交
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class ControlMoveWithCollider : MonoBehaviour
|
||||
{
|
||||
public float moveSpeed = 50f; // 右键平移速度
|
||||
public float zoomSpeed = 10f; // 滚轮缩放速度
|
||||
public ClickToFocus inScript; // 引用焦点脚本,提供 core
|
||||
public float rotationSensitivity = 0.2f; // 左键拖拽旋转灵敏度
|
||||
//public float minPitch = -8f; // 最低俯仰角
|
||||
//public float maxPitch = 80f; // 最高俯仰角
|
||||
public float moveSpeed = 50f; // 右键平移速度
|
||||
public float zoomSpeed = 10f; // 滚轮缩放速度
|
||||
public ClickToFocus inScript; // 引用焦点脚本,提供 core
|
||||
public float rotationSensitivity = 0.2f; // 左键拖拽旋转灵敏度
|
||||
//public float minPitch = -8f; // 最低俯仰角
|
||||
//public float maxPitch = 80f; // 最高俯仰角
|
||||
|
||||
private Camera cam;
|
||||
private bool isRotating = false;
|
||||
@@ -15,51 +16,68 @@ public class ControlMoveWithCollider : MonoBehaviour
|
||||
private Vector3 reservoirCenter = new Vector3(1551f, 25f, 1119f);
|
||||
private Rigidbody rb;
|
||||
|
||||
private bool isCameraCtrl = true; // 相机是否可控制
|
||||
|
||||
//相机操作有效性判断
|
||||
private bool isClickUI = false;
|
||||
|
||||
void Start()
|
||||
{
|
||||
rb = GetComponent<Rigidbody>();
|
||||
cam = Camera.main;
|
||||
Vector3 core = inScript.core;
|
||||
if (inScript == null)
|
||||
Debug.LogError("请在 Inspector 中把挂有 ClickToFocus 的物体拖给 inScript");
|
||||
Debug.LogError("请在 Inspector 中把挂有 ClickToFocus 的物体拖给 inScript");
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
//if (SliderDragDetector.isSliderDragging)
|
||||
// return;
|
||||
if (!isCameraCtrl) return;
|
||||
if (inScript == null) return;
|
||||
//操作有效性判断
|
||||
if (EventSystem.current != null)
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))
|
||||
{
|
||||
isClickUI = EventSystem.current.IsPointerOverGameObject();
|
||||
}
|
||||
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(2))
|
||||
{
|
||||
isClickUI = false;
|
||||
}
|
||||
}
|
||||
if (isClickUI) return;
|
||||
Vector3 core = inScript.core;
|
||||
|
||||
// —— 右键平移 ——
|
||||
// —— 右键平移 ——
|
||||
if (Input.GetMouseButton(1))
|
||||
{
|
||||
moveSpeed = Vector3.Distance(core, transform.position) / 2f;//这里涉及到像素和实际空间米的关系转换
|
||||
moveSpeed = Vector3.Distance(core, transform.position) / 2f;//这里涉及到像素和实际空间米的关系转换
|
||||
float mx = Input.GetAxis("Mouse X");
|
||||
float my = Input.GetAxis("Mouse Y");
|
||||
Vector3 desiredPosition = cam.transform.position + (-cam.transform.right * mx + -cam.transform.up * my) * moveSpeed * Time.deltaTime;
|
||||
|
||||
// 判断是否与障碍物发生碰撞
|
||||
// 判断是否与障碍物发生碰撞
|
||||
if (!WillCollideWithObstacle(desiredPosition))
|
||||
{
|
||||
cam.transform.position = desiredPosition;
|
||||
}
|
||||
}
|
||||
|
||||
// —— 滚轮缩放 ——
|
||||
// —— 滚轮缩放 ——
|
||||
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
||||
if (Mathf.Abs(scroll) > 0.001f)
|
||||
{
|
||||
Vector3 desiredPosition = cam.transform.position + cam.transform.forward * scroll * zoomSpeed;
|
||||
|
||||
// 判断是否与障碍物发生碰撞
|
||||
// 判断是否与障碍物发生碰撞
|
||||
if (!WillCollideWithObstacle(desiredPosition))
|
||||
{
|
||||
cam.transform.position = desiredPosition;
|
||||
}
|
||||
}
|
||||
|
||||
// —— 左键开始/结束旋转 ——
|
||||
// —— 左键开始/结束旋转 ——
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
isRotating = true;
|
||||
@@ -70,7 +88,7 @@ public class ControlMoveWithCollider : MonoBehaviour
|
||||
isRotating = false;
|
||||
}
|
||||
|
||||
// —— 左键拖拽:RotateAround 实现 + 俯仰限制 ——
|
||||
// —— 左键拖拽:RotateAround 实现 + 俯仰限制 ——
|
||||
if (isRotating && Input.GetMouseButton(0))
|
||||
{
|
||||
Vector2 curr = Input.mousePosition;
|
||||
@@ -80,7 +98,7 @@ public class ControlMoveWithCollider : MonoBehaviour
|
||||
/*
|
||||
|
||||
*/
|
||||
// 1. 水平绕世界 Y 轴旋转
|
||||
// 1. 水平绕世界 Y 轴旋转
|
||||
//Vector3 desiredPosition = cam.transform.position;
|
||||
//cam.transform.RotateAround(core, Vector3.up, delta.x);
|
||||
|
||||
@@ -90,19 +108,19 @@ public class ControlMoveWithCollider : MonoBehaviour
|
||||
Vector3 newPosHorizonal = core + newLocalPosHorizonal;
|
||||
|
||||
|
||||
// 检查旋转后是否发生碰撞
|
||||
// 检查旋转后是否发生碰撞
|
||||
if (!WillCollideWithObstacle(newPosHorizonal))
|
||||
{
|
||||
// 如果没有碰撞,执行旋转
|
||||
// 如果没有碰撞,执行旋转
|
||||
cam.transform.RotateAround(core, Vector3.up, delta.x);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果碰撞了,就不动
|
||||
// 如果碰撞了,就不动
|
||||
|
||||
}
|
||||
|
||||
// 2. 俯仰旋转
|
||||
// 2. 俯仰旋转
|
||||
Vector3 dir = (cam.transform.position - core).normalized;
|
||||
Vector3 flat = new Vector3(dir.x, 0f, dir.z).normalized;
|
||||
float selfFlatX = cam.transform.forward.x;
|
||||
@@ -147,29 +165,45 @@ public class ControlMoveWithCollider : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
// 碰撞检测方法
|
||||
// 碰撞检测方法
|
||||
bool WillCollideWithObstacle(Vector3 targetPosition)
|
||||
{
|
||||
// 计算从当前位置到目标位置的方向和距离
|
||||
// 计算从当前位置到目标位置的方向和距离
|
||||
Vector3 moveDir = targetPosition - cam.transform.position;
|
||||
float moveDist = moveDir.magnitude;
|
||||
|
||||
// 使用 SphereCast 检查即将到达的位置是否会与障碍物发生碰撞
|
||||
// 使用 SphereCast 检查即将到达的位置是否会与障碍物发生碰撞
|
||||
RaycastHit hit;
|
||||
if (Physics.SphereCast(cam.transform.position, 0.5f, moveDir, out hit, moveDist, LayerMask.GetMask("Default"), QueryTriggerInteraction.Collide) )
|
||||
if (Physics.SphereCast(cam.transform.position, 0.5f, moveDir, out hit, moveDist, LayerMask.GetMask("Default"), QueryTriggerInteraction.Collide))
|
||||
{
|
||||
// 如果检测到碰撞,返回 true
|
||||
// 如果检测到碰撞,返回 true
|
||||
Debug.Log("collider");
|
||||
return true;
|
||||
}
|
||||
else if ( Mathf.Pow(targetPosition.x+120f, 2f)+ Mathf.Pow(targetPosition.z , 2f)<10000f && targetPosition.y<80f)
|
||||
else if (Mathf.Pow(targetPosition.x + 120f, 2f) + Mathf.Pow(targetPosition.z, 2f) < 10000f && targetPosition.y < 80f)
|
||||
{
|
||||
// 如果在范围内,返回 false
|
||||
// 如果在范围内,返回 false
|
||||
return false;
|
||||
}
|
||||
else
|
||||
Debug.Log("outRange");
|
||||
return true;//这个true是为了有返回值,实际上走不到这一步
|
||||
return true;//这个true是为了有返回值,实际上走不到这一步
|
||||
|
||||
}
|
||||
}
|
||||
public void GetInScriptCore(Vector3 position)
|
||||
{
|
||||
inScript.core = position;
|
||||
}
|
||||
|
||||
public void StartCameraControl()
|
||||
{
|
||||
isCameraCtrl = true;
|
||||
isClickUI = false;
|
||||
}
|
||||
|
||||
//停止相机控制
|
||||
public void StopCameraControl()
|
||||
{
|
||||
isCameraCtrl = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user