feat():为压力板、按钮、拉杆添加音效,为玩家移动添加音效
This commit is contained in:
@@ -14,6 +14,10 @@ namespace Script.Gameplay.Facility
|
||||
private bool isPressing = false;
|
||||
private Vector3 buttonInitialPosition;
|
||||
|
||||
[SerializeField] private AudioClip turnSound;
|
||||
[SerializeField] private AudioClip returnSound;
|
||||
[SerializeField] private AudioSource buttonSound;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
buttonInitialPosition = buttonPressPoint.transform.position;
|
||||
@@ -37,12 +41,16 @@ namespace Script.Gameplay.Facility
|
||||
isPressing = true;
|
||||
// 按钮按下的动画或效果可以在这里添加
|
||||
buttonPressPoint.transform.position = new Vector3(0, 0, 0);
|
||||
buttonSound.clip = turnSound;
|
||||
buttonSound.Play();
|
||||
|
||||
yield return new WaitForSeconds(signalDuration);
|
||||
SendSignal(false, this.gameObject);
|
||||
isPressing = false;
|
||||
// 按钮弹起的动画或效果可以在这里添加
|
||||
buttonPressPoint.transform.position = buttonInitialPosition;
|
||||
buttonSound.clip = returnSound;
|
||||
buttonSound.Play();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ namespace Script.Gameplay.Facility
|
||||
{
|
||||
private bool isPulled = false;
|
||||
[SerializeField] private GameObject rotationPrefab;
|
||||
[SerializeField] private AudioClip turnSound;
|
||||
[SerializeField] private AudioClip returnSound;
|
||||
[SerializeField] private AudioSource LeverSound;
|
||||
|
||||
public override string GetInteractPrompt()
|
||||
{
|
||||
@@ -30,11 +33,15 @@ namespace Script.Gameplay.Facility
|
||||
{
|
||||
// 旋转拉杆到下拉位置
|
||||
rotationPrefab.transform.rotation = Quaternion.Euler(0f, 0f, 45f);
|
||||
LeverSound.clip = turnSound;
|
||||
LeverSound.Play();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 旋转拉杆回到初始位置
|
||||
rotationPrefab.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
|
||||
LeverSound.clip = returnSound;
|
||||
LeverSound.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,10 @@ namespace Script.Gameplay.Facility
|
||||
[SerializeField] private Vector3 plateOffset = Vector3.up * 0.1f;
|
||||
|
||||
[SerializeField] private GameObject pressTopPrefab;
|
||||
|
||||
[SerializeField] private AudioClip pressedSound;
|
||||
[SerializeField] private AudioClip returnSound;
|
||||
[SerializeField] private AudioSource plateSound;
|
||||
private bool lastState = false;
|
||||
private bool hasObject = false;
|
||||
|
||||
@@ -27,13 +31,21 @@ namespace Script.Gameplay.Facility
|
||||
{
|
||||
// 被压下动画
|
||||
if (pressTopPrefab != null)
|
||||
{
|
||||
pressTopPrefab.transform.localPosition = new Vector3(0, 0, 0.1f);
|
||||
plateSound.clip = pressedSound;
|
||||
plateSound.Play();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 弹起动画
|
||||
if (pressTopPrefab != null)
|
||||
{
|
||||
pressTopPrefab.transform.localPosition = new Vector3(0, 0, 1);
|
||||
plateSound.clip = returnSound;
|
||||
plateSound.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user