faet(fallDamage): 实现玩家的摔落伤害
This commit is contained in:
@@ -29,6 +29,11 @@ namespace Script.Gameplay.Player
|
||||
private Vector3 velocity;
|
||||
private bool isGrounded;
|
||||
private IInputManager inputManager;
|
||||
|
||||
private float lastGroundY;
|
||||
private bool wasGroundedLastFrame;
|
||||
[Tooltip("从高处落下时的伤害阈值(米)")]
|
||||
[SerializeField] private float fallDamageThreshold = 3f; //伤害阈值
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -46,6 +51,32 @@ namespace Script.Gameplay.Player
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// 地面检测
|
||||
bool isCurrentlyGrounded = groundCheck != null
|
||||
? Physics.CheckSphere(groundCheck.position, groundDistance, groundMask)
|
||||
: characterController.isGrounded;
|
||||
|
||||
// 记录离开地面时的高度
|
||||
if (wasGroundedLastFrame && !isCurrentlyGrounded)
|
||||
{
|
||||
lastGroundY = transform.position.y;
|
||||
}
|
||||
|
||||
// 落地检测
|
||||
if (!wasGroundedLastFrame && isCurrentlyGrounded)
|
||||
{
|
||||
float fallHeight = lastGroundY - transform.position.y;
|
||||
if (fallHeight > fallDamageThreshold) // 3米为伤害阈值,可调整
|
||||
{
|
||||
int damage = Mathf.RoundToInt((fallHeight - 3f) * 10); // 每超出1米伤害10点
|
||||
var playerController = GetComponent<PlayerController>();
|
||||
if (playerController != null)
|
||||
playerController.TakeDamage(damage);
|
||||
}
|
||||
}
|
||||
|
||||
wasGroundedLastFrame = isCurrentlyGrounded;
|
||||
|
||||
// 地面检测
|
||||
if (groundCheck != null)
|
||||
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
|
||||
|
||||
Reference in New Issue
Block a user