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

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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等