This commit is contained in:
wuyanchen 2025-12-10 19:33:44 +08:00
parent 61f310ae0d
commit 762c4751fc
3 changed files with 71 additions and 5 deletions

View File

@ -146,6 +146,8 @@ public class EnemyManager : MonoBehaviour
//} //}
enemy.position = pos; enemy.position = pos;
var enemyComp = enemy.GetComponent<Enemy>(); var enemyComp = enemy.GetComponent<Enemy>();
enemy.eulerAngles = new Vector3(enemy.eulerAngles.x + enemyComp.addEulerX, enemy.eulerAngles.y + 180, enemy.eulerAngles.z); enemy.eulerAngles = new Vector3(enemy.eulerAngles.x + enemyComp.addEulerX, enemy.eulerAngles.y + 180, enemy.eulerAngles.z);
//Debug.Log(enemy.eulerAngles); //Debug.Log(enemy.eulerAngles);
@ -163,6 +165,7 @@ public class EnemyManager : MonoBehaviour
var destPos = pathesLoc[enemyComp.ToPosIndex].position; var destPos = pathesLoc[enemyComp.ToPosIndex].position;
destPos = new Vector3(enemy.position.x, destPos.y, destPos.z); destPos = new Vector3(enemy.position.x, destPos.y, destPos.z);
ToDestination(enemyComp, destPos); ToDestination(enemyComp, destPos);
enemyComp.SetAlphaMaterial(); enemyComp.SetAlphaMaterial();

View File

@ -5,6 +5,7 @@ using UnityEngine;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
using UnityEngine.InputSystem.EnhancedTouch; using UnityEngine.InputSystem.EnhancedTouch;
using UnityEngine.UI;
using UnityEngine.UIElements; using UnityEngine.UIElements;
public class TargetFlow : MonoBehaviour public class TargetFlow : MonoBehaviour
@ -19,6 +20,9 @@ public class TargetFlow : MonoBehaviour
[Header("移动设置")] [Header("移动设置")]
[Tooltip("缓动移动速度")] [Tooltip("缓动移动速度")]
[SerializeField] private float moveSpeed = 10f; [SerializeField] private float moveSpeed = 10f;
[Tooltip("缓动距离乘积")]
[SerializeField] private float moveMul = 1f;
public GraphicRaycaster uiRaycaster;
// 运行时状态 // 运行时状态
private Camera _cam; private Camera _cam;
@ -30,10 +34,35 @@ public class TargetFlow : MonoBehaviour
private Vector3 _targetWorldPos; private Vector3 _targetWorldPos;
private Vector3? _beforeTargetWorldPos; private Vector3? _beforeTargetWorldPos;
private Vector3 _position = Vector3.zero; 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() private void Awake()
{ {
_cam = Camera.main; _cam = Camera.main;
_backgroundLayer = LayerMask.NameToLayer("Background");
}
private void Start()
{
var bounds = GetComponentInChildren<Collider>().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() private void OnEnable()
@ -90,11 +119,42 @@ public class TargetFlow : MonoBehaviour
// 新增UI阻塞处理 // 新增UI阻塞处理
private bool HandleUIBlocking() private bool HandleUIBlocking()
{ {
// 新输入系统读取鼠标位置返回Vector2与Input.mousePosition的x、y值一致
//Vector2 mousePos = moveAction.action.ReadValue<Vector2>();
// 创建射线ScreenPointToRay支持Vector2内部会自动处理z轴为0 // 创建射线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<RaycastResult> uiResults = new List<RaycastResult>();
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; 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)); _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; _controlPosition = _position;
_targetWorldPos.z = transform.position.z; _targetWorldPos.y = transform.position.y;
//_targetWorldPos.z = transform.position.z;
if (_beforeTargetWorldPos == null) if (_beforeTargetWorldPos == null)
{ {
_beforeTargetWorldPos = _targetWorldPos; _beforeTargetWorldPos = _targetWorldPos;
} }
var pos = _currPosition + (_targetWorldPos - _beforeTargetWorldPos.Value); ResetLimitPos();
var pos = _currPosition + (_targetWorldPos - _beforeTargetWorldPos.Value) * moveMul;
var screenPos = _cam.WorldToScreenPoint(pos); 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; _currPosition = pos;
} }