allabout11 2023-01-12 15:31 采纳率: 73.9%
浏览 73
已结题

unity人物行走动画声音

人物行走的时候播放行走动画并有声音
走一下之后,停止,声音还是存在
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class plyer_move_next : MonoBehaviour
{
    private CharacterController Controller;
    public float speed = 3f;
    public float RotateSpeed = 0.3f;
    public Animator anim;
    public AudioSource Sounds;
    public bool soundsPlay = true;
    public bool soundsEnd;
    
    // Start is called before the first frame update
    void Start()
    {
        Controller = transform.GetComponent<CharacterController>();
        anim = GetComponentInChildren<Animator>();
        Sounds = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    private void Move()
    {
        float moveX = Input.GetAxis("Horizontal");
        float moveZ = Input.GetAxis("Vertical");

        var moveDirection = transform.forward * (speed * moveZ * Time.deltaTime);
        if (moveDirection != Vector3.zero)
        {
            anim.SetFloat("speed",1);
            PlayerSounds();
        }
        else if (moveDirection ==Vector3.zero)
        {
            anim.SetFloat("speed",0);
        }
        Controller.Move(moveDirection);
        
        transform.Rotate(Vector3.up,moveX * RotateSpeed);   //针对坐标轴旋转


    }

    private void PlayerSounds()
    {
        if (soundsPlay == true && soundsEnd == true)
        {
            Sounds.Play();
            soundsEnd = false;

        }

        if (soundsPlay == false && soundsEnd == true)
        {
            Sounds.Stop();
            soundsEnd = false;

        }

    }
}



报错:Assertion failed on expression: 'errors == MDB_SUCCESS || errors == MDB_NOTFOUND'
我尝试过很多类似于改变soundsEnd的值但是结果还是不正确的
我想要达到的结果是静止的时候没有声音,行走的时候有声音
  • 写回答

3条回答 默认 最新

  • 於黾 2023-01-12 15:55
    关注

    PlayerSounds函数只有走的时候执行,停的时候你也要调用啊,要不然什么时候执行stop呢
    再说soundsPlay 这个变量一直是true,你也没改过它的值呀

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月15日
  • 已采纳回答 3月7日
  • 创建了问题 1月12日

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器