如何在Unity中控制我的步行声音的节奏?

huangapple go评论75阅读模式
英文:

How can I pace my walking sound in Unity?

问题

我尝试为我的Unity角色添加行走声音,当检测到移动输入时,它起作用,但声音触发得太快,我想要间隔一下。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WalkingSound : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField] private AudioSource audioSource;

    private void Update() {

        if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) ) {
            
            audioSource.enabled = true;
        } else {
            audioSource.enabled = false;
        }
    }

}

当我尝试使用带有时间延迟的枚举器时,音频根本不播放,但使用上面的基本脚本时却可以播放。有任何想法吗?

英文:

I tried to add a walking sound to my unity character when the movement input is detected, it works, however, the sound is triggering too rapidly and I'd like to pace it out.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WalkingSound : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField] private AudioSource audioSource;

    private void Update() {

        if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) ) {
            
            audioSource.enabled = true;
        } else {
            audioSource.enabled = false;
        }
    }

}

When I try to use an enumerator with a time delay the audio doesn't play at all, but it does with the basic script above. Any ideas?

答案1

得分: 1

Using animation event is a good solution to your problem but if you are not using any sort of animation, u can just use coroutine and play the sound with some delay.
example:

private playSoundCoroutine;

private void Update ()
{

  if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.D)){

      if (playSoundCoroutine == null)
         playSoundCoroutine = StartCoroutine (PlayWalkSound (delayDuration));

  }
  else{

      playSoundCoroutine == null;
      audioSource.enabled = false;
    }
}



private IEnumerator PlayWalkSound (float delay)
{
  //or use WaitUntil 
  yield return new WaitForSeconds (delay);
  PlaySound ();
  playSoundCoroutine = null;
}

above example code worked for me, i called this whenever player made
some movement and remember to set delay as per your preference and it
might need to be changed for also inAirMovement or sprint

英文:

Using animation event is a good solution to your problem but if you are not using any sort of animation ,u can just use coroutine and play the sound with some delay.
example:

private  playSoundCoroutine;

private void Update ()
{

  if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.D)){

      if (playSoundCoroutine == null)
	     playSoundCoroutine = StartCoroutine (PlayWalkSound (delayDuration));
	
  }
  else{

      playSoundCoroutine == null;
      audioSource.enabled = false;
    }
}



private IEnumerator PlayWalkSound (float delay)
{
  //or use WaitUntil 
  yield return new WaitForSeconds (delay);
  PlaySound ();
  playSoundCoroutine = null;
}

> above example code worked for me,i called this whenever player made
> some movement and remember to set delay as per your preference and it
> might need to be changed for also inAirMovement or sprint

答案2

得分: 0

只需取消音频源上的“循环复选框”并为您的行走动画添加一个“动画事件”以播放音频源。

英文:

Just uncheck Loop Checkbox on AudioSource and add a Animation Event to your Walking animation to play AudioSource

huangapple
  • 本文由 发表于 2023年5月28日 03:01:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定