From d543254d8228cef2cbaa966f2a13aec46c009105 Mon Sep 17 00:00:00 2001 From: xhxy <2290327506@qq.com> Date: Wed, 19 Mar 2025 22:03:28 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AC=BC=E7=9A=84=E8=B5=B0=E8=B7=AF=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E7=8A=B6=E6=80=81=E6=9C=BA=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E6=9C=AA=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scenes/SampleScene.unity | 12 ++++-- Assets/Scripts/RedGhost.cs | 71 +++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index d151647..c0f9c5d 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1593,11 +1593,15 @@ MonoBehaviour: curstate: 0 rb: {fileID: 0} anim: {fileID: 0} - PacMan: {fileID: 0} - ObstadeLayer: + pacMan: {fileID: 0} + obstadeLayer: serializedVersion: 2 m_Bits: 512 - speed: 240 + castDistance: 1 + boxWidth: 1.8 + validColor: {r: 0, g: 1, b: 0, a: 1} + blockedColor: {r: 1, g: 0, b: 0, a: 1} + speed: 1000 frightenedSpeed: 200 --- !u!58 &319435255 CircleCollider2D: @@ -1633,7 +1637,7 @@ CircleCollider2D: m_UsedByComposite: 0 m_Offset: {x: 0, y: 0} serializedVersion: 2 - m_Radius: 0.77 + m_Radius: 0.8 --- !u!50 &319435256 Rigidbody2D: serializedVersion: 4 diff --git a/Assets/Scripts/RedGhost.cs b/Assets/Scripts/RedGhost.cs index 84a2005..627a6d5 100644 --- a/Assets/Scripts/RedGhost.cs +++ b/Assets/Scripts/RedGhost.cs @@ -1,7 +1,9 @@ using System; using System.Collections; using System.Collections.Generic; +using Unity.VisualScripting; using UnityEngine; +using UnityEngine.UIElements; public class RedGhost : MonoBehaviour { @@ -23,7 +25,14 @@ public class RedGhost : MonoBehaviour public LayerMask obstadeLayer;//墙的层级 [Header("追击逃跑逻辑相关")] - private Vector2 currentDirection; + private Vector2 currentDirection; //当前方向 + + // 新增射线参数 + [Header("射线设置")] + public float castDistance = 1f; // 射线检测长度 + public float boxWidth = 0.3f; // 射线宽度 + public Color validColor = Color.green; // 有效射线颜色 + public Color blockedColor = Color.red; // 受阻射线颜色 public float speed;//基础速度 public float frightenedSpeed;//逃跑速度 @@ -57,6 +66,7 @@ public class RedGhost : MonoBehaviour } } + private void Idle() { @@ -72,7 +82,6 @@ public class RedGhost : MonoBehaviour private void Chase() { UpdateMovement(pacMan.transform.position, true); - //anim.SetBool("Frightened", false); } private void Frightened() @@ -87,6 +96,7 @@ public class RedGhost : MonoBehaviour + // 核心移动逻辑(追击/逃跑共用) private void UpdateMovement(Vector2 targetPos, bool isChasing) { @@ -98,8 +108,10 @@ public class RedGhost : MonoBehaviour { if (dir == -currentDirection) continue; // 循环到的方向等于当前的负方向,继续执行,禁止直接回头 - RaycastHit2D hit = Physics2D.Raycast(transform.position, dir, 1f, obstadeLayer);//起始点,方向,长度,过滤器 - if (hit.collider != null) continue; + // 使用BoxCast进行区域检测 + bool isBlocked = Physics2D.BoxCast(transform.position,GetBoxSize(dir),0f,dir,castDistance,obstadeLayer); + + if (isBlocked) continue; // 计算方向价值 float distance = Vector2.Distance(rb.position + dir, targetPos);//当前坐标加方向向量和目标主角坐标的差值 @@ -115,9 +127,54 @@ public class RedGhost : MonoBehaviour if (bestDir != Vector2.zero) { currentDirection = bestDir; - rb.velocity = currentDirection * (curstate == GhostState.Frightened ? frightenedSpeed : speed); - //anim.SetFloat("DirX", currentDirection.x); - //anim.SetFloat("DirY", currentDirection.y); + rb.velocity = currentDirection.normalized * (curstate == GhostState.Frightened ? frightenedSpeed : speed) * Time.deltaTime; } } + + // 根据方向获取不同的射线盒子尺寸 + private Vector2 GetBoxSize(Vector2 direction) + { + return new Vector2( + direction.x = boxWidth, + direction.y = boxWidth + ); + } + + // 可视化射线 + private void OnDrawGizmos() + { + if (!Application.isPlaying) return; + + foreach (Vector2 dir in new[] { Vector2.up, Vector2.down, Vector2.left, Vector2.right }) + { + Gizmos.color = Physics2D.BoxCast(transform.position, GetBoxSize(dir), 0f, dir, castDistance, obstadeLayer) + ? blockedColor + : validColor; + + DrawBoxCastGizmo( + transform.position, + dir, + GetBoxSize(dir), + castDistance + ); + } + } + + // 绘制盒子投射区域 + private void DrawBoxCastGizmo(Vector2 origin, Vector2 direction, Vector2 size, float distance) + { + Vector2 endPos = origin + direction * distance; + + // 绘制起点和终点的盒子 + Gizmos.DrawWireCube(origin, size); + Gizmos.DrawWireCube(endPos, size); + + // 绘制连接线 + Vector2 perpendicular = new Vector2(-direction.y, direction.x) * size.x / 2; + Vector2 edge1 = origin + perpendicular; + Vector2 edge2 = origin - perpendicular; + Gizmos.DrawLine(edge1, edge1 + direction * distance); + Gizmos.DrawLine(edge2, edge2 + direction * distance); + } } +