鬼的走路逻辑,状态机切换未写

This commit is contained in:
xhxy 2025-03-19 22:03:28 +08:00
parent ef10324f3c
commit d543254d82
2 changed files with 72 additions and 11 deletions

View File

@ -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

View File

@ -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;//逃跑速度
@ -58,6 +67,7 @@ public class RedGhost : MonoBehaviour
}
private void Idle()
{
curstate = GhostState.Chase;
@ -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);
}
}