【unity】麦克风声音驱动,控制身体做出不同动作

1.在角色对象上挂在animator组件,并将动作控制器与其关联
在这里插入图片描述
2.在角色对象上挂在audio source组件。
3.新建voice control脚本,编写代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Animations;
public class VoiceControl : MonoBehaviour
{public Animator animator;public float volumeThreshold = 0f; // 调整为一个更合理的阈值private string talkingParamName = "Talk";public AudioSource audioLocal;private string device;private IEnumerator coroWaitMic;private const float coroutineLoggingIfDebug = 5f; // debug log ERROR every 5 seconds while audiodevice errorconst int loopLength = 5;const int sampleRate = 16000;// Start is called before the first frame updatevoid Start(){// 设置初始动画状态为NotTalkingaudioLocal = GameObject.Find("xiaoyang").GetComponent<AudioSource>(); // 1 定位本地音频设备audioLocalif (Microphone.devices.Length <= 0){Debug.LogError("没有麦克风或麦克风权限未打开");throw new Exception("没有麦克风或麦克风权限未打开");}else{device = Microphone.devices[0];//获取麦克风名 }StartMicrophone(device);animator.SetBool(talkingParamName, false);}// Update is called once per framevoid Update(){float currentVolume = GetAverageAmplitude();Debug.Log($"Current Average Amplitude: {currentVolume}, Volume Threshold: {volumeThreshold}");// 根据音量大小切换Talk参数和动画状态// 根据当前音量大小决定是否切换动画状态if (currentVolume > volumeThreshold){animator.SetBool("Talk", true);Debug.Log("true执行");}else{animator.SetBool("Talk", false);Debug.Log("false执行");}}private void StartMicrophone(string mic){// Start and wait for the microphone to start recording dataif (coroWaitMic != null)StopCoroutine(coroWaitMic);coroWaitMic = WaitForMic(device);StartCoroutine(coroWaitMic);} // end StartMicrophone()IEnumerator WaitForMic(string mic){float timeCheck = Time.time;if (audioLocal == null){Debug.LogWarning("[WaitForMic()] - AudioSource is not present...bailing...");yield break;}audioLocal.clip = Microphone.Start(mic, true, loopLength, sampleRate);// Let the Microphone start filling the buffer prior to activating the AudioSource.while (!(Microphone.GetPosition(mic) > 0)){if (Time.time - timeCheck > coroutineLoggingIfDebug){Debug.Log("[WaitForMic()] - is waiting for the mic to record.");timeCheck = Time.time;}// Wait for Microphone to start gathering data.yield return null;}audioLocal.Play();audioLocal.loop = true;} // end WaitForMic() 录音设备开始录音private float GetAverageAmplitude(){float[] data = new float[sampleRate]; // 假设每次读取sampleRate个样本数据,实际项目中可以根据需要调整audioLocal.clip.GetData(data, 0); // 获取麦克风实时输入的数据float sumOfSquares = 0f;for (int i = 0; i < data.Length; i++){float sampleValue = Mathf.Abs(data[i]); // 取绝对值计算振幅sumOfSquares += Mathf.Pow(sampleValue, 2); // 将样本平方累加,用于计算均方根(RMS)}float averageAmplitude = Mathf.Sqrt(sumOfSquares / data.Length); // 计算均方根得到平均振幅大小Debug.Log($"Current Average Amplitude: {averageAmplitude}, Volume Threshold: {volumeThreshold}");return averageAmplitude;}
}

4.将动作控制器关联到脚本上
在这里插入图片描述
5.设置音量阈值,使得当达到某个音量值时切换动画
在这里插入图片描述
6.在动作控制器中设置不同的动作状态
(1)添加没说话时和说话时的状态并添加自循环,使动作能够重复播放
在这里插入图片描述
(2)新建布尔参数,用于控制状态
在这里插入图片描述
(3)设置Notalking→talking过渡动画的属性和出发布尔值的条件
在这里插入图片描述
(4)设置talking→Notalking过渡动画的属性和出发布尔值的条件
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/411398.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

鸿蒙开发(四)UIAbility和Page交互

通过上一篇的学习&#xff0c;相信大家对UIAbility已经有了初步的认知。在上篇中&#xff0c;我们最后实现了一个小demo&#xff0c;从一个UIAbility调起了另外一个UIAbility。当时我提到过&#xff0c;暂不实现比如点击EntryAbility中的控件去触发跳转&#xff0c;而是在Entry…

oracle篇—19c新特性自动索引介绍

☘️博主介绍☘️&#xff1a; ✨又是一天没白过&#xff0c;我是奈斯&#xff0c;DBA一名✨ ✌✌️擅长Oracle、MySQL、SQLserver、Linux&#xff0c;也在积极的扩展IT方向的其他知识面✌✌️ ❣️❣️❣️大佬们都喜欢静静的看文章&#xff0c;并且也会默默的点赞收藏加关注❣…

WIndows系统重装、备份与恢复实操问题笔记

一 windows重装 1.1 基本步骤 下载大白菜根据官网使用教程制作启动u盘从MSDN或者微软官网下载Windows镜像根据查询的快捷键进入BIOS系统&#xff0c;设置U盘为第一启动 重装 1.2 Windows 11 激活 微软其实在2023年9月20日的公告中宣布停掉免费升级&#xff0c;数字激活工具…

【MATLAB】 SSA奇异谱分析信号分解算法

有意向获取代码&#xff0c;请转文末观看代码获取方式~ 1 基本定义 SSA奇异谱分析&#xff08;Singular Spectrum Analysis&#xff09;是一种处理非线性时间序列数据的方法&#xff0c;可以对时间序列进行分析和预测。 它基于构造在时间序列上的特定矩阵的奇异值分解&#…

K8s(二)Pod资源——node调度策略、node亲和性、污点与容忍度

目录 node调度策略nodeName和nodeSelector 指定nodeName 指定nodeSelector node亲和性 node节点亲和性 硬亲和性 软亲和性 污点与容忍度 本文主要介绍了在pod中&#xff0c;与node相关的调度策略&#xff0c;亲和性&#xff0c;污点与容忍度等的内容 node调度策略node…

Android车载系统Car模块架构链路分析

一、模块主要成员 CarServiceHelperService SystemServer 中专门为 AAOS 设立的系统服务&#xff0c;用来管理车机的核心服务 CarService。该系统服务的具体实现在 CarServiceHelperServiceUpdatableImpl CarService Car模块核心服务APP&#xff0c;Android 13版本开始分为…

Kubernetes网络模型概述

Kubernetes网络模型设计的一个基础原则是&#xff1a;每个Pod都拥有一个独立的IP地址&#xff0c;并假定所有Pod都在一个可以直接连通的、扁平的网络空间中。所以不管这些Pod是否运行在同一个Node中&#xff0c;都要求它们可以直接通过对方的IP进行访问。由于Kubernetes的网络模…

【每周AI简讯】GPT-5将有指数级提升,GPT Store正式上线

AI7 - Chat中文版最强人工智能 OpenAI的CEO奥特曼表示GPT-5将有指数级提升 GPT奥特曼参加Y-Combinator W24启动会上表示&#xff0c;我们已经非常接近AGI。GPT-5将具有更好的推理能力、更高的准确性和视频支持。 GPT Store正式上线 OpenAI正式推出GPT store&#xff0c;目前…

HUAWEI华为MateStation S台式机电脑12代PUC-H7621N,H5621N原装出厂Windows11.22H2系统

链接&#xff1a;https://pan.baidu.com/s/1QtjLyGTwMZgYiBO5bUVPYg?pwd8mx0 提取码&#xff1a;8mx0 原厂WIN11系统自带所有驱动、出厂主题壁纸、系统属性专属联机支持标志、Office办公软件、华为电脑管家等预装程序 文件格式&#xff1a;esd/wim/swm 安装方式&#xf…

ES自动补全

安装IK分词器 要实现根据字母做补全&#xff0c;就必须对文档按照拼音分词。在GitHub上恰好有elasticsearch的拼音分词插件。地址&#xff1a;GitHub - medcl/elasticsearch-analysis-pinyin: This Pinyin Analysis plugin is used to do conversion between Chinese characte…

如何在CentOS 7 中基于OpenSSL 3.0 搭建Python 3.0 环境

1、OpenSSL 1.1 原因 [rootlocalhost ~]# openssl version OpenSSL 1.0.2k-fips 26 Jan 2017 [rootlocalhost ~]#通过执行openssl version可知Linux系统已经安装了OpenSSL&#xff0c;但该版本较低&#xff1b;Python 3 要求 OpenSSL版本不能低于1.1.1&#xff0c;否则安装P…

VMware虚拟机自定义网段及物理机ping不通虚拟机问题解决

Vmware网络介绍&#x1f6dc; VMware虚拟机提供了几种网络模式&#xff0c;其中包括桥接模式&#xff08;Bridged Mode&#xff09;、NAT模式&#xff08;Network Address Translation Mode&#xff09;和仅主机模式&#xff08;Host-Only Mode&#xff09;。这些模式允许虚拟…