feat():为压力板、按钮、拉杆添加音效,为玩家移动添加音效

This commit is contained in:
2025-10-29 17:05:10 +08:00
parent 97a3a351d7
commit 3c438b3871
11 changed files with 512 additions and 4 deletions

View File

@@ -19,6 +19,10 @@ namespace Script.Gameplay.Player
[Tooltip("地面检测半径")] public float groundDistance = 0.4f;
[Tooltip("地面层LayerMask")] public LayerMask groundMask;
[Header("Sound Effects")]
[SerializeField] private AudioSource playerSound;
[SerializeField] private AudioClip jumpSound;
[SerializeField] private AudioClip walkSound;
private float initSpeed;
private CharacterController characterController;
@@ -35,6 +39,10 @@ namespace Script.Gameplay.Player
[Tooltip("推动物体的力量")] [SerializeField]
private float pushPower = 2f;
[Header("Footstep Settings")]
[Tooltip("判定为走路的最小输入强度")] [SerializeField]
private float walkThreshold = 0.1f;
private void Awake()
{
characterController = GetComponent<CharacterController>();
@@ -42,6 +50,9 @@ namespace Script.Gameplay.Player
Debug.LogError("PlayerMoveController 需要 CharacterController 组件!");
initSpeed = speed;
playerSound = playerSound != null ? playerSound : GetComponent<AudioSource>();
if (playerSound == null)
Debug.LogWarning("PlayerMoveController: AudioSource 未设置,音效将不会播放。");
}
private void Start()
@@ -95,9 +106,17 @@ namespace Script.Gameplay.Player
Vector3 move = transform.right * x + transform.forward * z;
characterController.Move(move * speed * Time.deltaTime);
// 走路音效逻辑:在地面且移动输入较大时播放循环走路音效
HandleFootstepAudio(x, z);
// 跳跃
if (isGrounded && inputManager.JumpPressed)
{
// 跳跃时停止走路循环声并播放跳跃音效(一次性)
StopWalkAudioIfPlaying();
if (playerSound != null && jumpSound != null)
playerSound.PlayOneShot(jumpSound);
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
@@ -106,6 +125,45 @@ namespace Script.Gameplay.Player
characterController.Move(velocity * Time.deltaTime);
}
private void HandleFootstepAudio(float x, float z)
{
if (playerSound == null || walkSound == null) return;
float inputMag = new Vector2(x, z).magnitude;
bool shouldPlayWalk = isGrounded && inputMag > walkThreshold;
if (shouldPlayWalk)
{
if (playerSound.clip != walkSound || !playerSound.isPlaying)
{
playerSound.clip = walkSound;
playerSound.loop = true;
playerSound.Play();
}
}
else
{
// 如果当前正在播放走路音效则停止
if (playerSound.clip == walkSound && playerSound.isPlaying)
{
playerSound.Stop();
playerSound.loop = false;
playerSound.clip = null;
}
}
}
private void StopWalkAudioIfPlaying()
{
if (playerSound == null) return;
if (playerSound.clip == walkSound && playerSound.isPlaying)
{
playerSound.Stop();
playerSound.loop = false;
playerSound.clip = null;
}
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
@@ -114,7 +172,7 @@ namespace Script.Gameplay.Player
{
// 忽略从下方碰撞
if (hit.moveDirection.y < -0.3f) return;
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
body.AddForce(pushDir * pushPower, ForceMode.Impulse);
}
@@ -130,4 +188,4 @@ namespace Script.Gameplay.Player
speed = initSpeed;
}
}
}
}