diff --git a/Assets/Scripts/EnemyManager.cs b/Assets/Scripts/EnemyManager.cs index d37d571..b05cc9b 100644 --- a/Assets/Scripts/EnemyManager.cs +++ b/Assets/Scripts/EnemyManager.cs @@ -146,6 +146,8 @@ public class EnemyManager : MonoBehaviour //} enemy.position = pos; + + var enemyComp = enemy.GetComponent(); enemy.eulerAngles = new Vector3(enemy.eulerAngles.x + enemyComp.addEulerX, enemy.eulerAngles.y + 180, enemy.eulerAngles.z); //Debug.Log(enemy.eulerAngles); @@ -163,6 +165,7 @@ public class EnemyManager : MonoBehaviour var destPos = pathesLoc[enemyComp.ToPosIndex].position; destPos = new Vector3(enemy.position.x, destPos.y, destPos.z); + ToDestination(enemyComp, destPos); enemyComp.SetAlphaMaterial(); diff --git a/Assets/Scripts/TargetFlow.cs b/Assets/Scripts/TargetFlow.cs index 2d285c3..2633c66 100644 --- a/Assets/Scripts/TargetFlow.cs +++ b/Assets/Scripts/TargetFlow.cs @@ -5,6 +5,7 @@ using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.InputSystem; using UnityEngine.InputSystem.EnhancedTouch; +using UnityEngine.UI; using UnityEngine.UIElements; public class TargetFlow : MonoBehaviour @@ -19,6 +20,9 @@ public class TargetFlow : MonoBehaviour [Header("移动设置")] [Tooltip("缓动移动速度")] [SerializeField] private float moveSpeed = 10f; + [Tooltip("缓动距离乘积")] + [SerializeField] private float moveMul = 1f; + public GraphicRaycaster uiRaycaster; // 运行时状态 private Camera _cam; @@ -30,10 +34,35 @@ public class TargetFlow : MonoBehaviour private Vector3 _targetWorldPos; private Vector3? _beforeTargetWorldPos; private Vector3 _position = Vector3.zero; + private int _backgroundLayer = 0; + private float _radius = 0; + + private Vector3 offsetLimitPos1; + private Vector3 offsetLimitPos2; + private float beforeNearClipPlane = -1; private void Awake() { _cam = Camera.main; + _backgroundLayer = LayerMask.NameToLayer("Background"); + } + + private void Start() + { + var bounds = GetComponentInChildren().bounds; + _radius = Mathf.Max(bounds.extents.x, bounds.extents.y, bounds.extents.z); + } + + private void ResetLimitPos() + { + if (_cam.nearClipPlane != beforeNearClipPlane) + { + beforeNearClipPlane = _cam.nearClipPlane; + var limitRectPos1 = _cam.ScreenToWorldPoint(new Vector3(0, 0, _cam.nearClipPlane)); + var limitRectPos2 = _cam.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, _cam.nearClipPlane)); + offsetLimitPos1 = _cam.WorldToScreenPoint(new Vector3(limitRectPos1.x + _radius, limitRectPos1.y, limitRectPos1.z + _radius)); + offsetLimitPos2 = _cam.WorldToScreenPoint(new Vector3(limitRectPos2.x - _radius, limitRectPos2.y, limitRectPos2.z - _radius)); + } } private void OnEnable() @@ -90,11 +119,42 @@ public class TargetFlow : MonoBehaviour // 新增UI阻塞处理 private bool HandleUIBlocking() { + // 新输入系统:读取鼠标位置(返回Vector2,与Input.mousePosition的x、y值一致) + //Vector2 mousePos = moveAction.action.ReadValue(); + // 创建射线(ScreenPointToRay支持Vector2,内部会自动处理z轴为0) - if (EventSystem.current.IsPointerOverGameObject()) + + if (_controlPosition == null) { - return true; + return false; } + + if (uiRaycaster != null) + { + // 第一步:先检测UI + PointerEventData pointerData = new PointerEventData(EventSystem.current); + pointerData.position = _controlPosition.Value; + List uiResults = new List(); + uiRaycaster.Raycast(pointerData, uiResults); + + if (uiResults.Count > 0) + { + return true; // 有UI则不检测3D + } + } + + + Ray ray = _cam.ScreenPointToRay(_controlPosition.Value); + if (Physics.Raycast(ray, out RaycastHit hit, 100f)) + { + if (hit.collider.gameObject.layer == _backgroundLayer) + { + //enabled = false; + //StartCoroutine(EnableAfterDelay(0.1f)); + return false; + } + } + return false; } @@ -116,16 +176,19 @@ public class TargetFlow : MonoBehaviour // 转换为世界坐标并平滑移动 _targetWorldPos += _cam.ScreenToWorldPoint(new Vector3(_position.x, _position.y, _cam.nearClipPlane)) - _cam.ScreenToWorldPoint(new Vector3(_controlPosition.Value.x, _controlPosition.Value.y, _cam.nearClipPlane)); _controlPosition = _position; - _targetWorldPos.z = transform.position.z; + _targetWorldPos.y = transform.position.y; + //_targetWorldPos.z = transform.position.z; if (_beforeTargetWorldPos == null) { _beforeTargetWorldPos = _targetWorldPos; } - var pos = _currPosition + (_targetWorldPos - _beforeTargetWorldPos.Value); + ResetLimitPos(); + var pos = _currPosition + (_targetWorldPos - _beforeTargetWorldPos.Value) * moveMul; var screenPos = _cam.WorldToScreenPoint(pos); + //Debug.Log(offsetLimitPos1 + ":" + offsetLimitPos2); //限制对象移动范围在屏幕范围之内 - if (screenPos.x > 0 && screenPos.x < Screen.width && screenPos.y > 0 && screenPos.y < Screen.height) + if (screenPos.x > offsetLimitPos1.x && screenPos.x < offsetLimitPos2.x && screenPos.y > offsetLimitPos1.y && screenPos.y < offsetLimitPos2.y) { _currPosition = pos; } diff --git a/Assets/TowerDefence/Scenes/Tower.unity b/Assets/TowerDefence/Scenes/Tower.unity index ad864f8..06b1d7f 100644 Binary files a/Assets/TowerDefence/Scenes/Tower.unity and b/Assets/TowerDefence/Scenes/Tower.unity differ