问题分享:https://www.bilibili.com/video/BV1zLetz1Ew8

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endifpublic class SectorCheck : MonoBehaviour
{[Tooltip("扇形圆心")] public Transform center;[Tooltip("目标点")] public Transform point;[Tooltip("扇形朝向")] public Vector2 direction = Vector2.right;[Tooltip("扇形半径")] public float radius = 5f;[Tooltip("扇形角度")] [Range(0f,360f)] public float angle = 60f;[Header("Gizmo")][Tooltip("扇形段数")] [Range(4,256)] public int segments = 40;public Color outlineColor = new Color(1f, 0f, 0f, 1f);public Color insidePointColor = Color.green;public Color outsidePointColor = Color.red;void FixedUpdate(){if (center == null || point == null) return;bool inside = PointInSector_Dot(point.position, center.position, direction, angle, radius);Debug.Log(inside ? "点在扇形内" : "点不在扇形内");}/// <summary>/// 点乘方法/// </summary>/// <param name="targetPoint">目标点</param>/// <param name="sectorCenter">扇形中心</param>/// <param name="sectorDir">扇形朝向</param>/// <param name="angleDeg">扇形弧度</param>/// <param name="radius">扇形半径</param>/// <returns>是否在指定扇形里</returns>public static bool PointInSector_Dot(Vector2 targetPoint, Vector2 sectorCenter, Vector2 sectorDir, float angleDeg, float radius){Vector2 dir = targetPoint - sectorCenter;float dist = dir.magnitude;if (dist > radius) return false;if (dist == 0f) return true;Vector2 dirNorm = dir.normalized;Vector2 sectorNorm = sectorDir.normalized;float dot = Vector2.Dot(sectorNorm, dirNorm);float cosHalf = Mathf.Cos((angleDeg * 0.5f) * Mathf.Deg2Rad);// 若 dot >= cosHalf,则夹角 <= halfAngle,即在扇形内return dot >= cosHalf;}/// <summary>/// 将一个向量旋转指定度数/// </summary>/// <param name="v">指定向量</param>/// <param name="degrees">旋转度数</param>/// <returns>旋转后的新向量</returns>private static Vector2 Rotate(Vector2 v, float degrees) {// 将角度转换为弧度,因为 Mathf.Sin 和 Mathf.Cos 函数使用弧度制float rad = degrees * Mathf.Deg2Rad;float c = Mathf.Cos(rad);float s = Mathf.Sin(rad);return new Vector2(v.x * c - v.y * s, v.x * s + v.y * c);}// 2D 叉乘(返回 z 分量)private static float Cross(Vector2 a, Vector2 b){return a.x * b.y - a.y * b.x;}/// <summary>/// 叉乘方法/// </summary>/// <param name="targetPoint">目标点</param>/// <param name="sectorCenter">扇形中心</param>/// <param name="sectorDir">扇形朝向</param>/// <param name="angleDeg">扇形弧度</param>/// <param name="radius">扇形半径</param>/// <returns>是否在指定扇形里</returns>public static bool PointInSector_Cross(Vector2 targetPoint, Vector2 sectorCenter, Vector2 sectorDir, float angleDeg, float radius) {Vector2 dir = targetPoint - sectorCenter;float dist = dir.magnitude;if (dist > radius) return false;if (dist == 0f) return true;float half = angleDeg * 0.5f;Vector2 fwd = sectorDir.normalized;Vector2 left = Rotate(fwd, half);//左边界向量Vector2 right = Rotate(fwd, -half);//右边界向量float crossLeft = Cross(left, dir);float crossRight = Cross(right, dir);// crossLeft <= 0 && crossRight >= 0 表示 dir 在 right 与 left 之间return crossLeft <= 0f && crossRight >= 0f;}// 在 Scene 视图绘制扇形private void OnDrawGizmos(){if (center == null) return;// 计算基础数据Vector3 centerPos = center.position;Vector3 fromDir3 = new Vector3(direction.x, direction.y, 0f);if (fromDir3.sqrMagnitude <= 0.0001f) fromDir3 = Vector3.up;fromDir3 = fromDir3.normalized;// 画扇形边框与辐条Gizmos.color = outlineColor;DrawWireSector(centerPos, fromDir3, angle, radius, segments);// 若有指定点,画出点并根据是否在扇形内标色if (point != null){bool inside = PointInSector_Dot(point.position, centerPos, direction, angle, radius);Gizmos.color = inside ? insidePointColor : outsidePointColor;Gizmos.DrawSphere(point.position, Mathf.Max(0.05f, radius * 0.02f));}}//用线段绘制扇形边界并画从中心到弧上点的辐条private void DrawWireSector(Vector3 centerPos, Vector3 fromDir3, float angleDeg, float radius, int segs){if (segs < 3) segs = 3;float half = angleDeg * 0.5f;Vector3 prev = centerPos + Quaternion.Euler(0f, 0f, -half) * fromDir3 * radius;// 绘制弧for (int i = 1; i <= segs; i++){float t = (float)i / segs;float ang = -half + t * angleDeg;Vector3 cur = centerPos + Quaternion.Euler(0f, 0f, ang) * fromDir3 * radius;Gizmos.DrawLine(prev, cur);prev = cur;}// 两条边到中心Vector3 left = centerPos + Quaternion.Euler(0f, 0f, -half) * fromDir3 * radius;Vector3 right = centerPos + Quaternion.Euler(0f, 0f, half) * fromDir3 * radius;Gizmos.DrawLine(centerPos, left);Gizmos.DrawLine(centerPos, right);//绘制若干辐条int spokes = Mathf.Clamp(segs / 6, 0, segs);if (spokes > 0){for (int i = 0; i <= spokes; i++){float t = (float)i / spokes;float ang = -half + t * angleDeg;Vector3 p = centerPos + Quaternion.Euler(0f, 0f, ang) * fromDir3 * radius;Gizmos.DrawLine(centerPos, p);}}}
}

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

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

相关文章

基于Python sdk发布自己的第一个mcp-client

说在前面 上一篇文章发布了一个mcp-server&#xff0c;具体的server是否能被正确的访问到&#xff1f;是否能够得到正常的返回&#xff1f; 在github上找到一个客户端的代码实现&#xff0c;我把里面的大模型调用换成了支持国内大模型的方式&#xff0c;一起来验证一下吧~ 主…

C# 浮点数与定点数详细解析

C# 浮点数与定点数详细解析 在 C# 中&#xff0c;数值类型主要分为&#xff1a; 整数型&#xff08;int, long 等&#xff09;浮点型&#xff08;float, double&#xff09;定点型&#xff08;decimal&#xff09; 浮点数和定点数在内部的表示方式不同&#xff0c;导致它们的 精…

【小宁学习日记5 stm32】LED闪烁 LED流水灯 蜂鸣器

目录 01.LED闪烁 1、搭建电路板 2、新建工程 &#xff08;1&#xff09;前期准备 &#xff08;2&#xff09;创建工程文件夹结构 &#xff08;3&#xff09;复制固件库文件到对应文件夹 &#xff08;4&#xff09;在 Keil 中创建工程 &#xff08;5&#xff09;配置工程…

openstack的novnc兼容问题

1.今天在部署O版过程中发现了novnc组件不兼容openstack2.novnc一直报错&#xff0c;令牌过期&#xff0c;原本以为是python代码配置的问题&#xff0c;最后经过排查很久发现竟然是novnc的版本和openstack的O版不兼容novncyum remove -y novnc*安装支持版本yum install -y novnc…

Day25 栈 队列 二叉树

day25 栈 队列 二叉树使用栈计算表达式的值 概述 通过两个栈&#xff08;数值栈和符号栈&#xff09;实现中缀表达式求值。算法核心是&#xff1a; 遇到数字时&#xff0c;累加并入数值栈&#xff1b;遇到运算符时&#xff0c;比较其与符号栈顶运算符的优先级&#xff1a; 若当…

阿里云RDS MySQL数据归档全攻略:方案选择指南

引言在日常数据库管理中&#xff0c;数据归档是必不可少的重要环节。随着业务数据的不断增长&#xff0c;将历史数据从生产数据库迁移到更经济的存储方案中&#xff0c;不仅可以降低存储成本&#xff0c;还能提升数据库性能。阿里云提供了丰富的数据归档解决方案&#xff0c;本…

线性回归学习

一、线性回归简介核心思想&#xff1a;线性回归是一种通过属性的线性组合来做预测的模型。它的目标很明确&#xff0c;就是找到一条合适的直线、平面或者更高维度的超平面&#xff0c;让预测出来的值和实际真实值之间的差距尽可能小。比如在预测房屋价格时&#xff0c;就可以根…

如何使用 DeepSeek 助力工作:全面指南​

一、引言​1.1 DeepSeek 简介​DeepSeek 的定位与目标概述​核心技术亮点&#xff08;大语言模型、多模态能力、AI Agent 框架&#xff09;​1.2 工作场景中应用 AI 的趋势​AI 对职场效率提升的重要性​DeepSeek 在众多 AI 工具中的独特地位​二、DeepSeek 基础功能介绍​2.1 …

车载诊断架构 --- EOL引起关于DTC检测开始条件的思考

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 做到欲望极简,了解自己的真实欲望,不受外在潮流的影响,不盲从,不跟风。把自己的精力全部用在自己。一是去掉多余,凡事找规律,基础是诚信;二是…

JCTools Spmc 单生产者-多消费者的无锁并发有界队列

SpmcArrayQueue 是 JCTools 中为 单生产者-多消费者&#xff08;Single-Producer-Multi-Consumer&#xff09; 场景设计的有界队列。与 SPSC 模型相比&#xff0c;SPMC 的复杂性主要体现在消费者侧&#xff0c;因为多个消费者线程需要以线程安全的方式竞争消费同一个队列中的元…

SpringAI1.0.1实战教程:避坑指南25年8月最新版

Spring AI 1.0.1 使用教程 项目简介 作为一个Java的开发者 听到Java也有ai框架了 很高兴~~~ 本来想学一下SpringAI但是网上卖课的一大堆&#xff0c;并且大部分课程都是五月的&#xff0c;到2025年的8月份&#xff0c;SpringAI的版本做了很多更新&#xff0c;所以我本人参考…

Maven架构的依赖管理和项目构建

​​​​​​什么是依赖管理对第三方依赖包的管理&#xff0c;可以连接互联网下载项目所需第三方jar包。对自己开发的模块的管理&#xff0c;可以像引用第三方依赖包一样引用自己项目的依赖包。Maven的依赖管理方式和传统方式有什么区别传统方式&#xff1a;从官网手动下载jar包…

微信小程序开发(一):使用开发者工具创建天气预报项目

Hi&#xff0c;我是前端人类学&#xff08;之前叫布兰妮甜&#xff09;&#xff01; 从今天开始&#xff0c;我将开启一个全新的微信小程序开发系列教程&#xff0c;通过实际项目带大家系统学习小程序开发。作为系列的第一篇文章&#xff0c;我们将从最基础的环境搭建开始&…

【链表 - LeetCode】24. 两两交换链表中的节点

24. 两两交换链表中的节点 - 力扣&#xff08;LeetCode&#xff09; 题解&#xff1a; - 迭代 首先是直接遍历的做法&#xff0c;这里注意调整指针指向的顺序。 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* List…

爬虫基础学习-链接协议分析,熟悉相关函数

1、urlparse&#xff1a;&#xff08;python标准库中的一个模块&#xff0c;解析和操作url&#xff09;标准的url链接格式&#xff1a;scheme://netloc/path;params?query#fragmentscheme&#xff08;协议&#xff09; http or https netloc&#xff08;网络位置&#xff09; …

kkfileview预览Excel文件去掉左上角的跳转HTML预览、打印按钮

上篇说了使用nginx代理kkfile预览文件&#xff0c;但是又发现个新问题&#xff0c;预览其他文件时都正常&#xff0c;但是预览.xlsx格式的时候&#xff0c;在左上角会有【跳转HTML预览】【打印】两个按钮&#xff0c;如下所示&#xff1a;这篇就来说一下如何去掉。首先这个跟kk…

阿里开源新AI code工具:qoder功能介绍

下载地址&#xff1a; https://qoder.com/ 文档地址&#xff1a; https://docs.qoder.com/ 文章目录1. AI 编码发展趋势2. 真实世界软件开发的挑战3. 我们的方法3.1. 透明度3.1.1. 知识可见性3.1.2. 执行透明度3.2. 增强上下文工程3.3. 规范驱动与任务委托3.3.1. 聊天模式&…

什么是短视频矩阵系统企业立项功能源码开发,支持OEM

短视频矩阵系统企业立项功能源码开发解析在短视频行业蓬勃发展的当下&#xff0c;企业纷纷布局短视频矩阵&#xff0c;以实现多平台、多账号的协同运营。而企业立项作为短视频矩阵项目启动的关键环节&#xff0c;其高效、规范的管理直接影响项目的推进效率与成果。为此&#xf…

当GitHub宕机时,我们如何协作?

问题背景与影响 GitHub作为主流代码托管平台的依赖现状宕机对分布式团队、CI/CD流水线、紧急修复的影响案例其他类似平台&#xff08;GitLab、Bitbucket&#xff09;的潜在连带风险 本地与离线协作方案 利用Git分布式特性&#xff1a;本地仓库继续提交&#xff0c;恢复后同步搭…

【会议跟踪】Model-Based Systems Engineering (MBSE) in Practice 2025

会议主旨与议题 会议宣传链接:https://www.sei.cmu.edu/events/mbse-in-practice/ 本次会议将于2025年8月21日位美国弗吉尼亚州阿灵顿(五角大楼所在地)举行。本次会议主旨为 MBSE in Practice: Bridging the Gap Between Theory and Success(2025)。随着软件定义系统日趋…