我可以帮助你们实现这一功能。但首先,我需要您提供更多信息和代码实现细节,如:
- 你想要实现的 Dodge/ Roll/ Dash 只有在玩家在地面上时生效。
- 我们如何确定玩家是否在地面上。
- 我们如何检测玩家是否处于 i-frames 状态,并且可以跳过对手,尤其是你的 EnemyHealth 组件有关于 i-frames 么。
- 你可以给出 Enemy 的 LayerMask,供我参考。
- 如果你需要更多建议和帮助,请提供更多关于你的项目和需求的细节。
基于你的要求,我已做了补充后的C#代码 :
public class PlayerMovement : MonoBehaviour
{
// ...
// 屏障检测
private void CollisionCheck()
{
Vector2 direction = new Vector2(Mathf.Sign(transform.localScale.x), 0f);
RaycastHit2D hit = Physics2D.BoxCast(
capsuleCollider.bounds.center,
capsuleCollider.bounds.size,
0f,
direction,
wallCheckDistance,
groundLayer
);
// 检测玩家是否在地面上
isGrounded = hit.collider != null;
// 检测玩家是否处于屏障上
isWallDetected = hit.collider != null;
// 检测玩家是否处于 i-frames 状态
isIframe = hit.collider != null && Time.time - lastHitTime < iframeDuration;
}
// dodge_roll_dash
public float dodgeSpeed = 5f;
public float iframeDuration = 0.5f;
private float lastHitTime = 0f;
private bool isIframe = false;
// Update()方法中添加
if (Input.GetKeyDown(KeyCodespace) && isGrounded && !isIframe)
{
DodgeRollDash();
}
// DodgeRollDash方法
private void DodgeRollDash()
{
// 检测玩家是否处于 i-frames 状态
isIframe = true;
lastHitTime = Time.time;
// 做一些动作,例如跳跃或滚动
if (Input.GetKeyDown(KeyCode(space)) && isGrounded)
{
body.velocity = new Vector2(dodgeSpeed, 0f);
}
}
// 改进 CollisionCheck方法判断玩家是否在地面上
private bool isGrounded()
{
// 检测玩家是否在地面上
RaycastHit2D raycastHit = Physics2D.BoxCast(
capsuleCollider.bounds.center,
capsuleCollider.bounds.size,
0f,
Vector2.down,
0.1f,
groundLayer
);
return raycastHit.collider != null;
}
// 增加 IFrame 实现
private void Attack()
{
// ...
// 检测玩家是否处于 i-frames 状态
if (isIframe)
{
// 做一些不攻击的操作,在 i-frames 状态下
Debug.Log("可以跳过对手了!");
}
else
{
// 做一些攻击操作,不处于 i-frames 状态下
// ...
}
}
// 增加 EnemyLayerMask
[SerializeField]
private LayerMask enemyLayerMask;
// ...
// 增加 EnemyHealth组件
public class EnemyHealth : MonoBehaviour
{
public float health = 10f;
public float iframeDuration = 0.5f;
private float lastHitTime = 0f;
private bool isIframe = false;
// 检测玩家是否处于 i-frames 状态
public bool isIframe()
{
return isIframe == true;
}
// 检测玩家是否处于 i-frames 状态,且在 iframeDuration 时间内
public bool isIframe(float dt)
{
return isIframe && Time.time - lastHitTime < iframeDuration;
}
// 做一些攻击操作,不处于 i-frames 状态下
public void TakeDamage(float damage, PlayerMovement playerMovement)
{
if (!isIframe(playerMovement))
{
health -= damage;
Debug.Log("被攻击!");
}
}
}
}
评论 (0)