修复内容:
PlayerMovement:新增 _facingLocked 字段 + LockFacing(bool) 方法;UpdateFacing() 锁定时直接返回 WallSlideState:OnStateEnter 调用 LockFacing(true) + FlipFacing(_wallDir);OnStateExit 调用 LockFacing(false) 解锁 WallJumpState:OnStateEnter 保险性再调一次 LockFacing(false);WallJumpAway/Toward 同步写入 _inputVelocityX,确保解锁后 UpdateFacing 朝向正确(背墙跳 = 离墙方向,对墙跳 = 朝墙方向)
This commit is contained in:
@@ -4,36 +4,67 @@ using UnityEngine;
|
||||
namespace BaseGames.World
|
||||
{
|
||||
/// <summary>
|
||||
/// 危险区域。玩家进入时触发即死或持续伤害。
|
||||
/// 危险区域。玩家进入时触发即死(调用 Kill(),无视无敌帧)或持续伤害。
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class HazardZone : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private bool _isInstantKill = true;
|
||||
[SerializeField] private int _damage = 9999;
|
||||
[SerializeField] private bool _isInstantKill = true;
|
||||
[SerializeField] private int _damage = 1;
|
||||
[Tooltip("持续伤害模式下每次造成伤害的间隔(秒)。仅 _isInstantKill = false 时生效。")]
|
||||
[SerializeField] private float _damageInterval = 0.5f;
|
||||
|
||||
private PlayerStats _cachedStats;
|
||||
private float _damageTimer;
|
||||
private bool _playerInside;
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if (!other.CompareTag("Player")) return;
|
||||
_cachedStats = other.GetComponentInParent<PlayerStats>();
|
||||
if (_cachedStats == null) return;
|
||||
|
||||
var stats = other.GetComponentInParent<PlayerStats>();
|
||||
if (stats == null) return;
|
||||
_playerInside = true;
|
||||
ApplyDamage();
|
||||
_damageTimer = _damageInterval;
|
||||
}
|
||||
|
||||
private void OnTriggerExit2D(Collider2D other)
|
||||
{
|
||||
if (!other.CompareTag("Player")) return;
|
||||
_playerInside = false;
|
||||
_cachedStats = null;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_playerInside || _isInstantKill || _cachedStats == null) return;
|
||||
|
||||
_damageTimer -= Time.deltaTime;
|
||||
if (_damageTimer <= 0f)
|
||||
{
|
||||
ApplyDamage();
|
||||
_damageTimer = _damageInterval;
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyDamage()
|
||||
{
|
||||
if (_cachedStats == null) return;
|
||||
if (_isInstantKill)
|
||||
stats.TakeDamage(stats.MaxHP * 2); // 确保即死(超过最大血量)
|
||||
_cachedStats.Kill();
|
||||
else
|
||||
stats.TakeDamage(_damage);
|
||||
_cachedStats.TakeDamage(_damage);
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
Gizmos.color = new Color(1f, 0f, 0f, 0.3f);
|
||||
var col = GetComponent<Collider2D>();
|
||||
if (col != null)
|
||||
Gizmos.DrawCube(transform.position, col.bounds.size);
|
||||
if (col == null) return;
|
||||
Gizmos.color = new Color(1f, 0f, 0f, 0.3f);
|
||||
Gizmos.DrawCube(transform.position, col.bounds.size);
|
||||
Gizmos.color = new Color(1f, 0f, 0f, 0.8f);
|
||||
if (col != null)
|
||||
Gizmos.DrawWireCube(transform.position, col.bounds.size);
|
||||
Gizmos.DrawWireCube(transform.position, col.bounds.size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user