Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2150.00元
Stealth秘密行动-人物移动声音问题
SleepingD发起了问答2018-10-01
4
回复
761
浏览

老师好,想问下为什么我自己写的代码会一直调用footStep.Play(),导致声音重复播放,难道不应该是第一帧打开了AudioSource,第二帧 !footStep.isPlaying就为false了吗

我的代码:

    private AudioSource footStep;

	void Awake ()
	{
	    anim = GetComponent<Animator>();
	    footStep = GetComponent<AudioSource>();
	}

	void Update ()
	{
        if (anim.GetCurrentAnimatorStateInfo(0).IsName("Locomotion") && !footStep.isPlaying)
        {
            footStep.Play();
            print("play");
        }

AudioSource中的Loop是勾选了的

附上siki老师写的代码,是正确的:

	void Update ()
	{

        if (anim.GetCurrentAnimatorStateInfo(0).IsName("Locomotion"))
        {
            PlayFootStep();
        }

    private void PlayFootStep()
    {
        if (!footStep.isPlaying)
        {
            footStep.Play();
            print("play");
        }
    }

 

所有回复
  • siki 2018-10-05

    重复播放具体是什么呢,是footStep.Play被调用了吗

    可以看下play是输出了几次,以及输出的时候检查下footStep.isPlaying的值

    • SleepingD 2018-10-07

      是的老师,是footStep.Play一直被调用。运动以后play会一直被调用。footStep.isPlaying的值很奇怪,没有运动时一直输出为false,是正常的;运动的时候即输出false又输出ture,false输出的频率比以前低,true的输出数目和play相等,所以可以推断运动时footStep.isPlaying的true和false输出的数目相等。。。但是用老师的代码就是正常的,我不太清楚区别在哪儿。。输出结果和代码见楼下

      (0) 回复
    还有-4条回复,点击查看
    你还没有登录,请先登录注册
  • SleepingD 2018-10-07

    在Update方法下输出的footStep.isPlaying():

    	void Update ()
    	{
            print(footStep.isPlaying);
            if (anim.GetCurrentAnimatorStateInfo(0).IsName("Locomotion") && !footStep.isPlaying)
            {
                footStep.Play();
                print("play");
    
    
            }

     

    某一次的输出结果如下

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • siki 2018-10-11

    看下调用footStep.Play方法之前的时候,看下footStep.isPlaying的值是多少,然后看下调用完之后footStep.isPlaying是多少

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!