faet(fallDamage): 实现玩家的摔落伤害

This commit is contained in:
2025-10-24 16:54:15 +08:00
parent 22a2ab724a
commit 78b8e5f154
2 changed files with 111 additions and 1 deletions

View File

@@ -402,6 +402,84 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: ddd89e93db279184daa80d86bd4e770f, type: 3}
--- !u!1001 &627238400
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalPosition.x
value: 25.97
objectReference: {fileID: 0}
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalPosition.y
value: 0.5
objectReference: {fileID: 0}
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalPosition.z
value: -5
objectReference: {fileID: 0}
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3535959014221630543, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: sendSignalMode
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3535959014221630543, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: controlTarget.Array.data[0]
value:
objectReference: {fileID: 279470472}
- target: {fileID: 6268190613584218029, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_Name
value: Console (1)
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 85652dd0e36597644aa5b3bc9603797b, type: 3}
--- !u!1001 &865274353
PrefabInstance:
m_ObjectHideFlags: 0
@@ -1369,7 +1447,7 @@ PrefabInstance:
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
propertyPath: m_LocalPosition.x
value: 25
value: 23.94
objectReference: {fileID: 0}
- target: {fileID: 40184683368772887, guid: 85652dd0e36597644aa5b3bc9603797b,
type: 3}
@@ -2450,4 +2528,5 @@ SceneRoots:
- {fileID: 1549323716}
- {fileID: 2110019781}
- {fileID: 730184548946979397}
- {fileID: 627238400}
- {fileID: 3372472533242412945}

View File

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