Vector3.Dot 方位判断

Unity之 Vector3 的详细介绍以及方法的介绍 你真的懂得unity 中的Vector3 的方法吗?Please fellow me 阅读详情

Dot函数经常被用在方位判断,放上代码和结果。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

//UTF8 说明
public class dottest : MonoBehaviour
{
    [SerializeField] Text txtShow;
    [SerializeField] Transform me;  //胶囊
    [SerializeField] Transform point; //目标点
    

    // Update is called once per frame
    void Update()
    {
        Vector3 target_dir = (point.position - me.position).normalized;
        //Debug.DrawLine(me.position, target_dir * 10f, Color.blue,0.1f);
        float dot = Vector3.Dot(me.forward, target_dir);
        float dot2 = Vector3.Dot(point.right, target_dir);
        txtShow.text = "Dot(me.forward, target_dir) = " + dot.ToString("F1")+"\n"+
            "Dot(me.right, target_dir) = " + dot2.ToString("F1")
            ;
    }
}

如图:我们看蓝色的第一行输出:Dot参数1是胶囊的forward,参数2是红色球的方向。
当目标在胶囊正前方,Dot得到正数(0~1),正对着球时候最大为1。反之就是负数。

//当目标在胶囊正前方,Dot得到1。
在这里插入图片描述

//当目标在胶囊正右方,Dot得到0。
在这里插入图片描述
//当目标在胶囊正后方,Dot得到-1。
在这里插入图片描述

//当目标在胶囊正左方,Dot得到0。
在这里插入图片描述
下面是GIF图,来观测下数值变化。
请添加图片描述
我们可以看到两个向量方向相同就是1,夹角越大就越趋近-1。

这样我们就可以知道目标是在后面还是前面。
但是左边和右边如何区分呢?
这里我还做了一个right和dir的Dot的数值。就是第二行蓝色输出。
在胶囊的右侧就是正数,左侧就是负数。

参数1和参数2的向量位置可以互换,不用考虑先后问题。
这个Dot函数被应用在方位指向等。

点乘与叉乘知识点 public Transform A; void Update () { float d= Vector3.Dot(transform.forward,A.position-transform.position); Vector3 a = Vector3.Cross(transform.forward,A.position-transform.position); if (d>=0&&a.y>0) {... 阅读详情

相关推荐

【unity进阶篇】向量的点乘Vector3.Dot与叉乘Vector3.Cross、判断物体方位和范围

【unity进阶篇】向量的点乘Vector3.Dot与叉乘Vector3.Cross

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 1037

Unity Vector3.Dot(VectorA, VectorB)

Unity Vector3.Dot(VectorA, VectorB) Vector3.Dot(VectorA, VectorB) 等于 VectorA * VectorB。 而对于两个向量的乘积计算如下:

Wilhelm的博客 6270

【远控使用】Cobalt Strike横向渗透域控

categories: 知识积累 top: FALSE copyright: true abbrlink: 1643719980 #date: '2021-8-7 12:00:00' #description:1400x780 tags: 内网渗透 提权 渗透测试 黑客工具photos: https://gitee.com/gylq/cloudimages/raw/master/img/image-20210826182104275.png 前言 Cobalt Strike.

孤桜懶契 2704

Vector3.Dot用法

Vector3.Dot也叫点积,它返回1个-1.0~1.0之间的一个值。网上确实也这么说。但是这个值表示什么呢?恩,表示返回进行Dot计算的两个向量之间的夹角的余弦值(Cos弧度角).要注意的是能进行Dot计算的前提是两个向量首先要变成单位向量! public class DotTest : MonoBehaviour {   public Vector3 v1; public V

Simon 1万+

Super4PCS文章阅读笔记

Super4PCS阅读笔记 Super4PCS阅读笔记 前言 前言 今天给大家分享一下点云配准四点法4PCS0的变种——Super4PCS。此算法的原文在:Super4PCS: Fast Global Pointcloud Registration via Smart Indexing。稍微我也会写一篇关于如何使用这篇文章的里所提供的代码进行点云粗配准。关于四点法...

Vodake的博客 4539

采用离散模型搭建永磁同步电机控制系统

永磁同步电机发电系统分为控制器和发电机,通过simulink仿真软件进行发电系统的离散系统仿真。离散搭建仿真,可以更方便的自动代码生成。通过实践经验离散模块搭建,更能加深对整个控制系统的理解。离散系统在矢量控制中经常涉及到上个开关周期的角度,电压,或者预测下个周期的角度,电流,一般都是通过延时模块实现。PWM比较值寄存器被改写后都是使能了预装载,采样后触发计算到比较值生效可以延时0.5个开关周期Ts,也可以延时1个开关周期Ts,可以通过离散系统进行仿真,大大缩短开发周期。

f1072239934的博客 2863

threejs向量Vector3点乘.dot()

dot()

u014291990的博客 1434

liquibase介绍

1、Liquibase还是Flyway Flyway和Liquibase都支持专业数据库重构和版本控制所需的所有功能,因此您将始终知道要处理的数据库模式的版本以及它是否与软件版本匹配。两种工具都集成在Maven或Gradle构建脚本中以及Spring Boot生态系统中,因此您可以完全自动化数据库重构。 Flyway使用SQL定义数据库更改,因此您可以定制SQL脚本,使其与基础数据库技术(例如Oracle或PostgreSQL)良好地配合使用。另一方面,使用Liquibase,您可以通过使用XML,YA

赶路人儿 7534

Vector3.Dot

a.transform.position这两个向量,当cube(2)在自身(cube)的前方时,其夹角永远小于90°,在后方时,永远大于90°,如此可依据点乘来判断“敌人”相对于自身的位置(前方or后方)这里我将两个向量传入,第一个为cube本身的正方向,第二个为cube指向cube(2)的方向,由于向量点乘等于其各自模长相乘再乘上二者之间的余弦值,所以Vector3.Dot返回值的正负取决于其夹角。如图:cube在cube(2)的前方、

qq_29172391的博客 771

Verilog简易电梯控制系统(2层)

电梯处于1楼,按KEY3,LED3亮,电梯处于上行状态时,立刻按 KEY2,LED2亮,电梯继续运行至2楼,LED3灭;按KEY3,LED3亮,电梯处于上行状态时,立刻按 KEY0,LED0亮,电梯继续运行至2楼,LED3灭;电梯处于2楼,按KEY2,LED2亮,电梯处于下行状态时,立刻按 KEY3,LED3亮,电梯继续运行至1楼,LED2灭;电梯处于2楼,按KEY2,LED2亮,电梯处于下行状态时,立刻按 KEY1,LED1亮,电梯继续运行至1楼,LED2灭;电梯运行至2楼后LED1灭,2楼待机。

m0_54240221的博客 6952

巨正则蒙特卡洛—甲烷和二氧化碳的竞争吸附GCMC

巨正则蒙特卡洛—甲烷和二氧化碳的竞争吸附GCMC

Rockyoung 8018

Vector3.Dot 判断方位

判断方位 假设空间中有这几个坐标,判断一个物体在另一个物体的左边还是右边,前后还是后面 物体空间图 假如以C为中心,判断L是在它的左边还是右边 判断方法 using UnityEngine; using System.Collections; public class GetDirection : MonoBehaviour { public Transf...

weixin_33966095的博客 198

YOLO11 改进 - C2PSA | C2PSA融合HEWL(高频增强小波层):小波逆变换+双通道注意力增强+高频显式增强 | TGRS 2026

本文介绍了高频增强小波层(HEWL),一种专为解决红外小目标检测中边缘退化与语义模糊问题设计的上采样模块。该方法结合小波逆变换与交替通道注意力(ALCA),通过显式增强高频细节并优化特征融合,实现精准的边缘重建与语义对齐。我们将 HEWL 融入 PSABlock 结构构建了 C2PSA_HEWL 模块,并成功集成进 YOLO11 的骨干网络,替代原有的 C2PSA 模块。实验证明,结合 HEWL 的 YOLO11 在检测任务中表现优异,有效提升了模型对细节特征的捕捉能力。

魔改工程师的博客 102

Vector3.Dot(点积)与Vector3.Cross(叉积)判断敌人相对于角色的方位——Unity的API学习篇

概念理解:          向量的点积:               向量的点积又称为数量积或内积,计算方式:两向量的模与其夹角的余弦值相乘,例如a和b两个向量,a·b...

weixin_45373419的博客 3714

在群辉(NAS)中安装虚拟机套件(Virtual Machine Manager)

一、在群辉中下载虚拟机套件Virtual Machine Manager ①启用Open vSwitch(网络-->管理--启用Open vSwitch) ②打开“套件中心” ③查找到Virtual Machine Manager套件点击“安装套件” 二、安装虚拟机套件(Virtual Machine Manager) ①打开虚拟机套件运行设置向导 ②选择存储空间...

提供C#相关开发 、云计算、运维测试、网络安全相关技术分享与服务,有意向可联系。 2万+

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Unity3D中目标相对自身的前后左右方位判断

在做rpg类游戏的过程中,经常遇到要判断周围怪物相对自身的方位 1.判断目标在自己的前后方位可以使用下面的方法: Vector3.Dot(transform.forward,target.position) 返回值为正时,目标在自己的前方,反之在自己的后方 2.判断目标在机子的左右方位可以使用下面的方法: Vector3.Cross(transform.forward,ta...

modabobo 157

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Vector3.Dot 与Vector3.Cross

转载:http://blog.csdn.net/cxihu/article/details/46833443 Vector3.Dot(点积) : 点积的计算方式为:  a·b=|a|·|b|cos; 其中和 夹角不分顺序; 物理学中点积用来计算力沿物体运动的方向所做的功:W=F*S=|F|*|S|*cosθ;功是数量,故点积又称数量积,无向积等; 通过点积的

klwuxie的专栏 1371

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

物体方位判断:Vector3计算物体角度与方位 u3d学习总结笔记本

1.角度计算 2.方位判断  3.坐标系转换transform.InverseTransformPoint(); //============================================== 1.角度计算 Vector3 PlayerToPoint = Point.position - transform.position; //主角指向点的向量 float te...

闪电黑客的博客 3058

unity中向量点乘

Vector3 提供了计算点乘的方法----Dot。向量a 点乘 向量b 的结果。通过点乘推导公式算出夹角。得到两个向量的点乘结果。

m0_73113333的博客 365

Unity点乘和叉乘的基本使用

[csharp] view plain copy //求角度 及前后左右方位 public void checkTargetDirForMe(Transform target) { //xuqiTest: target.position = new Vector3(3, 0, 5); Vector3 dir = target.position - transform.position; //位置差,方向 //方式1 点乘 //点积的计算方式为:.

LearnToStick的博客 1909

Unity游戏AI开发:用点积与叉积实现敌人感知与攻击范围判定

在游戏开发中,空间关系判定是构建智能AI与交互系统的核心技术基础。其核心原理依赖于向量数学,特别是点积与叉积运算。点积通过计算两个向量的余弦值,高效判断目标是否位于前方扇形区域或测量方向一致性,而叉积则能生成垂直于两向量的新向量,用于判断目标的左右方位。这些数学工具为游戏对象间的相对位置、朝向和角度关系提供了高效的计算方法,其技术价值在于避免了复杂的三角函数运算和冗余的条件判断,显著提升了运行性能。在工程实践中,它们广泛应用于敌人AI的视野检测、扇形攻击范围判定、智能转身逻辑以及摄像机相对关系计算等场景。本

weixin_30420305的博客 336

Unity中判断目标在自己的哪个方位

在做rpg类游戏的过程中,经常遇到要判断周围怪物相对自身的方位 1.判断目标在自己的前后方位可以使用下面的方法:    Vector3.Dot(transform.forward, target.position)        返回值为正时,目标在自己的前方,反之在自己的后方 2.判断目标在机子的左右方位可以使用下面的方法:    Vecto

Root915的博客 3588
上一篇: Unity根据变量来添加脚本
下一篇: HLSL内置函数
Thinbug
博客等级 码龄22年 2万+粉丝 187原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thinbug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值