Unity人工智能-----感知系统-----听觉实现(2)
作者:互联网
听觉触发器:
using System.Collections;
using UnityEngine;
/// <summary>
/// 听觉触发器
/// </summary>
namespace AI.Perception
{
public class SoundTrigger:AbstractTrigger
{
//传播距离
public float caseDistance = 5;
//有效时间
public float lifeTime = 3;
//初始是否有效
public bool awakePlay = false;
public new void Start()
{
base.Start();
//设置类型
triggerType = TriggerType.Sound;
this.enabled = awakePlay;
}
public void OnEnable()
{
StartCoroutine(TimeDown());
}
private IEnumerator TimeDown()
{
//等待生命期之后
yield return new WaitForSeconds(lifeTime);
//禁用当前组件
this.enabled = false;
}
public override void Init(){}
}
}
听觉感应器:
using UnityEngine;
/// <summary>
/// 听觉感应器
/// </summary>
namespace AI.Perception
{
public class SoundSensor : AbstractSensor
{
//听力范围,听觉距离
public float hearDistance;
protected override bool TestTrigger(AbstractTrigger trigger)
{
//判断trigger是否是声音触发器
if(trigger is SoundTrigger)
{
//如果是,转类型
var tempTrigger = trigger as SoundTrigger;
//触发器与传感器的距离
var distance = Vector3.Distance(transform.position,
tempTrigger.transform.position);
//听力范围 + 传播范围 > 触发器与传感器的距离
return tempTrigger.caseDistance + hearDistance > distance;
}
return false;
}
public override void Init() { }
}
}
标签:触发器,false,人工智能,void,听觉,Unity,trigger,-----,public 来源: https://blog.csdn.net/weixin_49397205/article/details/113446712