• 检测目标是否在视线范围内(附视线范围扇形画法)
using UnityEngine;

public class Test_CanSeeTarget : MonoBehaviour
{
public GameObject target; /// <summary>
/// 视线范围-半径
/// </summary>
public float sightViewRadius = 5; /// <summary>
/// 视线范围-欧拉角
/// </summary>
public float sightViewEuler = 120; /// <summary>
/// 画扇形
/// </summary>
/// <param name="radius"></param>
/// <param name="euler"></param>
/// <param name="color"></param>
private void DrawSemicircle(float radius, float euler, Color color)
{
int segments = 10;
float deltaAngle = euler / segments;
Vector3 forward = transform.forward; Vector3[] vertices = new Vector3[segments + 2];
vertices[0] = transform.position;
for (int i = 1; i < vertices.Length; i++)
{
Vector3 pos = Quaternion.Euler(0f, -euler / 2 + deltaAngle * (i - 1), 0f) * forward * radius + transform.position;
vertices[i] = pos;
} // 画圆弧
for (int i = 1; i < vertices.Length - 1; i++)
{
UnityEngine.Debug.DrawLine(vertices[i], vertices[i + 1], color);
} // 画两条边
UnityEngine.Debug.DrawLine(vertices[0], vertices[vertices.Length - 1], color);
UnityEngine.Debug.DrawLine(vertices[0], vertices[1], color); } private bool CanSeeTarget()
{
float distance = Vector3.Distance(transform.position, target.transform.position); Vector3 forward = transform.rotation * Vector3.forward;
Vector3 dir = target.transform.position - transform.position; // 计算两个向量间的夹角,必须归一化normalized
float angle = Mathf.Acos(Vector3.Dot(forward.normalized, dir.normalized)) * Mathf.Rad2Deg; if (distance < sightViewRadius)
{
if (angle <= sightViewEuler / 2)
{
UnityEngine.Debug.Log("CanSeeTarget: {true}");
return true;
}
} UnityEngine.Debug.Log("CanSeeTarget: {false}"); return false; } void Update()
{
DrawSemicircle(sightViewRadius, sightViewEuler, Color.yellow);
CanSeeTarget(); } }

【Unity】检测目标是否在视线范围内(附视线范围扇形画法)的更多相关文章

  1. Unity 检测物体是否在相机视野范围内

    需求: 类似NPC血条,当NPC处于摄像机视野内,血条绘制,且一直保持在NPC头顶. 开始: 网上查找资料,然后编写代码: public RectTransform rectBloodPos; voi ...

  2. [DeeplearningAI笔记]卷积神经网络3.1-3.5目标定位/特征点检测/目标检测/滑动窗口的卷积神经网络实现/YOLO算法

    4.3目标检测 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.1目标定位 对象定位localization和目标检测detection 判断图像中的对象是不是汽车--Image clas ...

  3. MIT挑战(如何在12个月内自学完成MIT计算机科学的33门课程|内附MIT公开课程资源和学习顺序

    译者注:本文译自Scott H. Young的博客,Scott拥有超强的学习能力,曾在12个月内自学完成麻省理工学院计算机科学的33门课程.本文就是他个人对于这次MIT挑战的介绍和总结. 版权声明:本 ...

  4. 电机噪声之谐波分析(内附simulink中FFT分析的相关参数配置与解析)

    电机噪声之谐波分析(内附simulink中FFT分析的相关参数配置与解析) 目录 电机噪声之谐波分析(内附simulink中FFT分析的相关参数配置与解析) 写在前面 正文 电机噪声 谐波的产生 什么 ...

  5. 从零开始学数据分析,什么程度可以找到工作?( 内附20G、5000分钟数据分析工具教程大合集 )

    从零开始学数据分析,什么程度可以找到工作?( 内附20G.5000分钟数据分析工具教程大合集 )   我现在在Coursera上面学data science 中的R programming,过去很少接 ...

  6. C# Windows Phone 8 WP8 高级开发,制作不循环 Pivot ,图片(Gallery)导览不求人! 内附图文教学!!

    原文:C# Windows Phone 8 WP8 高级开发,制作不循环 Pivot ,图片(Gallery)导览不求人! 内附图文教学!! 一般我们在开发Winodws Phone APP 的时候往 ...

  7. jQuery Cookie (内附 上百行的中文使用手册,与 所有的注释中文翻译)

    jQuery Cookie (内附 上百行的中文使用手册,与 所有的注释中文翻译) 博主亲自翻译. 大家多多捧场. 更多资源请点击"查看TA 的资源" .全场通通 2积分. htt ...

  8. UE4如何检测目标在锥形视野内

    转自:http://blog.csdn.net/l346242498/article/details/70237083 做UE4游戏AI方面经常会遇到一个问题,就是何如判定目标在AI单位的视野范围内, ...

  9. 第十九节、基于传统图像处理的目标检测与识别(词袋模型BOW+SVM附代码)

    在上一节.我们已经介绍了使用HOG和SVM实现目标检测和识别,这一节我们将介绍使用词袋模型BOW和SVM实现目标检测和识别. 一 词袋介绍 词袋模型(Bag-Of-Word)的概念最初不是针对计算机视 ...

  10. 如何用OpenCV自带的adaboost程序训练并检测目标

    参考博文: 1.http://blog.csdn.net/wuxiaoyao12/article/details/39227189 2.http://www.cnblogs.com/easymind2 ...

随机推荐

  1. C CODE STYLE 每天瞅一两眼

    阅读进度:只读到了swich 转载于:https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html C Coding Standard C Co ...

  2. Vulnhub 靶场 LOOZ: 1

    Vulnhub 靶场 LOOZ: 1 前期准备: 靶机地址:https://www.vulnhub.com/entry/looz-1,732/ kali攻击机ip:192.168.147.190 靶机 ...

  3. sprinboot多个子模块下 依赖包没有找到 解决方案

    最近因为在使用springboot开发项目,在开发过程中,发现自己的子模块导入通用的模块 在启动中 说找不到这个类 百度下 说我要在pom文件下 pulus 插件 那里 加上这段代码 <conf ...

  4. SSM项目

    1.环境搭建 1.1 结构目录 1.2 配置逆向工程 1.2.1 pom.xml <?xml version="1.0" encoding="UTF-8" ...

  5. react backend uploadfile

    public List<string> WriteFile(List<UploadDTO> uploads) { List<string> fileNames = ...

  6. this.$refs 获取的值是undefined

    以下是父组件内的代码截图 如果想取子组件内的方法,参数,等可以试以下两种方法 1.在mounted内使用this.$nextTick(()=>{   }) 2.直接再undated() {} 内 ...

  7. FIFO 串口接收处理机制

    与安富莱电子的串口处理机制做对比交互 参考链接: https://www.eet-china.com/mp/a161019.html

  8. Python_DL_July_深度学习_10_深度学习与各种迁移学习应用

    深度学习10(上)深度学习与各种迁移学习应用

  9. vector 用法2

    vector 是向量类型,它可以容纳许多类型的数据,如若干个整数,所以称其为容器.vector 是C++ STL的一个重要成员,使用它时需要包含头文件: 复制代码 代码如下: #include< ...

  10. OpenEuler 中C与汇编的混合编程

    2.5.1用汇编代码编程 将C代码编译成汇编代码 C代码: /**********a.c file********/ #include <stdio.h> extern int B(); ...