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

Unity 进阶 之 判断基于物体所处位置(前后左右)/ 方法原理 /注意事项的简单整理 Unity 进阶 之 判断基于物体所处位置(前后左右)/ 方法原理 /注意事项的简单整理 目录 Unity 进阶 之 判断基于物体所处位置(前后左右)/ 方法原理 /注意事项的简单整理 一、简单介绍 二、实现原理 三、注意事项 四、效果预览 五、实现步骤 六、关键代码 七、附录 点积 叉积 一、简单介绍 Unity中的一些知识点整理。 本节简单介绍在Unity开发中的,判断某个物体在某个物体的位置(相对的前后左右判断),便于根据位置进行一些其他的操作,如果你有新的方式.. 阅读详情

在做rpg类游戏的过程中,经常遇到要判断周围怪物相对自身的方位


1.判断目标在自己的前后方位可以使用下面的方法:

   Vector3.Dot(transform.forward, target.position)

       返回值为正时,目标在自己的前方,反之在自己的后方

2.判断目标在机子的左右方位可以使用下面的方法:

   Vector3.Cross(transform.forward, target.position).y

      返回值为正时,目标在自己的右方,反之在自己的左方


3.在这里顺便解说下关于空间向量的点积和叉积:

A.点积 
  点积的计算方式为:  a·b=|a|·|b|cos<a,b>  其中|a|和|b|表示向量的模,<a,b>表示两个向量的夹角。另外在 点积 中,<a,b>和<b,a> 夹角是不分顺序的。 
  所以通过点积,我们其实是可以计算两个向量的夹角的。 
  另外通过点积的计算我们可以简单粗略的判断当前物体是否朝向另外一个物体: 只需要计算当前物体的transform.forward向量与 otherObj.transform.position 的点积即可, 大于0则在前方,否则在后方。

B.叉积 
  叉积的定义: c =a x b  其中a,b,c均为向量。即两个向量的叉积得到的还是向量! 
  性质1: c⊥a,c⊥b,即向量c垂直与向量a,b所在的平面 。 
  性质2: 模长|c|=|a||b|sin<a,b> 
  性质3: 满足右手法则 。从这点我们有axb ≠ bxa,而axb = – bxa。所以我们可以使用叉积的正负值来判断向量a,b的相对位置,即向量b是处于向量a的顺时针方向还是逆时针方向


3D游戏引擎系列三 笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,优快云社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:《手把手教你架构3D游戏引擎》电子工业出版社和《Unity3D实战核心技术详解》电子工业出版社等。优快云课程视频网址:http://edu.youkuaiyun.com/lecturer/144 笔者以前在游戏论坛担任过技术版主,以及在51CTO教育网,优快云教育网,泰课在线等教育网站担 阅读详情

相关推荐

3D游戏开发所需的数学基础——笛卡尔坐标系

很多人会觉得图形学中的数学复杂难懂。的确,一些数学模型在初学者看来晦涩难懂。但很多情况下,我们需要打交道的只是一些基础的数学运算。 在游戏制作中,我们使用数学绝大多数是用于计算位置,距离和角度等信息。而这些计算都是在笛卡尔坐标系下进行的。

路漫漫其修远兮,吾将上下而求索。 3934

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

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

3万+

Unity - 判断当前所属平台

目标:实现判断当前运行环境是哪个平台,并且可以在脚本中区分开。 背景:我是在做适配的时候遇到这个问题,在iOS和安卓上有不一样的配置,所以需要实现上述目标。 首先我先上了unityManual上找到,如下。 // JS function Awake() { #if UNITY_EDITOR Debug.Log("Unity Editor"); #endif

心之所至的博客 3067

Unity 角色朝向目标 / 动态转向动画

Unity 角色朝向目标 / 动态转向动画

weixin_45891168的博客 2067

Unity中Shader编译目标级别

针对不同平台的特性,所做的一些功能。

楠溪泽岸的博客 2105

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

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

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

unity中 判断目标在自身的方位

1.在自身的前后:   Vector3.Dot (transform.forward, target.position-transform.position)      返回值为正时,目标在自己的前方,反之在后方2.在自身的左右:   Vector3.Dot(transform.right, target.position-transform.position)      返回值为正时,目标在自己...

z_mg_的博客 3750

RT-Thread Studio使用笔记(四):带片外Flash(W25Q128)的OTA(YModem)

前言 在学习OTA之前,请先参考上两篇博客写的关于SPI设备及W25Q128的使用方法,传送门: https://blog.youkuaiyun.com/tigerots/article/details/107423633 https://blog.youkuaiyun.com/tigerots/article/details/107933991 如何使用OTA 一. 制作BootLoader 打开Bootloader 在线获取地址,注册,并根据RT-thread官方文档生成BootLoader,官方文档已经写的很清楚,再此

tigerots的专栏 4008

unity判断其它物体相对于自身方位以及角度

[csharp] view plain copy //求角度 及前后左右方位 public void checkTargetDirForMe(Transform target) { //xuqiTest: target.position = new Vector3(3, 0, 5); Vector3 dir = target.position - transfo...

习惯冬天的静谧 3637

远程桌面(RDP)共享USB设备全攻略:不止Hyper-V,你的加密狗和CH340串口也能远程用

本文详细介绍了远程桌面(RDP)共享USB设备的完整解决方案,包括加密狗和CH340串口设备的远程使用技巧。通过RemoteFX USB重定向技术,用户可以在远程会话中无缝使用本地USB设备,提升工作效率。文章涵盖企业级配置、典型设备实战案例及高级故障排除方法,是远程工作者的实用指南。

weixin_42522964的博客 333

BBR算法原理与实现深度解析

BBR算法通过重新定义拥塞控制的基本范式,为现代网络应用提供了突破性的解决方案。随着QUIC协议的普及和智能网卡技术的发展,BBR类算法将在边缘计算、云游戏、工业物联网等领域发挥更重要的作用。

程序猿老樊的博客 2418

Vector3.Dot 方位判断

如图:我们看蓝色的第一行输出:Dot参数1是胶囊的forward,参数2是红色球的方向。当目标在胶囊正前方,Dot得到正数(0~1),正对着球时候最大为1。这里我还做了一个right和dir的Dot的数值。我们可以看到两个向量方向相同就是1,夹角越大就越趋近-1。Dot函数经常被用在方位判断,放上代码和结果。//当目标在胶囊正后方,Dot得到-1。//当目标在胶囊正前方,Dot得到1。//当目标在胶囊正右方,Dot得到0。//当目标在胶囊正左方,Dot得到0。在胶囊的右侧就是正数,左侧就是负数。

thinbug的专栏 6043

保姆级教程:用GPU-Burn给你的NVIDIA Tesla服务器做个“压力体检”(附温度功耗监控脚本)

本文提供了一份详细的GPU服务器压力测试指南,重点介绍如何使用GPU-Burn工具对NVIDIA Tesla服务器进行全面的性能测试和温度功耗监控。从环境部署、多GPU测试策略到专业级监控指标解析,帮助运维人员构建标准化的GPU健康评估体系,确保AI训练和科学计算等关键业务的稳定性。

weixin_30619101的博客 395

Unity判断其它物体相对于自身方位以及角度

1.判断目标在自己的前后方位可以使用下面的方法: Vector3.Dot(transform.forward, target.position) 返回值为正时,目标在自己的前方,反之在自己的后方 2.判断目标在机子的左右方位可以使用下面的方法: Vector3.Cross(transform.forward, target.position).y 返回值为正时,目标在自己的右方,反之在自己的左方 3.在这里顺便解说下关于空间向量的点积和叉积: A.点积 点积的计算方式为: a·b=|a|·|

weixin_45777589的博客 1605

数模笔记_多变量最优化计算之牛顿法

Date: 2_22 Name: Guo Yehao Theme: Calculation in Optimality with multiple variables Reference: 数学建模方法与分析(华章) 在上一节中,我们讨论多变量最优化计算中的随机搜索算法,它的精度并不是众多算法中最高的。在精度问题上,有的时候问题本身就带有明显的误差性,以至于我们不能将其忽略,比如上一节消防站部署的问题,我们在区域划分时就有了误差,因此我们并不需要对结果要求很高的精确度,所以用一些比较粗略的算法就可以.

weixin_45358177的博客 2415

Rockchip 自定义vendorstorages数据再u-boot通过cmdline给kernel传递数据

本文介绍通过vendorstorages,允许保存和读取自定义数据。通过在U-Boot中读取这些数据并将其添加到cmdline中,将这些数据传递给Linux内核,实现了一个灵活的方式来配置和定制系统的需求。

我其实什么都不会 1819

unity3d:向量计算:获得两点连线的垂直向量,判断目标方位(前后左右)

获得两点连线垂直向量 /// <summary> /// 获取某向量的垂直向量 /// </summary> public static Vector3 GetVerticalDir(Vector3 _dir) { //(_dir.x,_dir.z)与(?,1)垂直,则_dir.x * ? + _dir.z * 1 = 0...

9786

Android 14 (AOSP) 源码架构深度解析与开发者指南

Android14源码的仓库指南信息

保持专注,保持热爱 1186

Unity判断目标在哪个方向

Unity获取获目标所在的方向

g73269565的博客 2960

数字 IC 笔试面试必考点(2)FPGA 开发流程

FPGA 的详细开发流程就是利用 EDA 开发工具对 FPGA 芯片进行开发的过程,所以 FPGA 芯片开发流程讲的并不是芯片的制造流程,区分于 IC 设计制造流程哟(芯片制造流程多麻烦,要好几十亿的费用呢,而且国内也貌似没有哪家像样的 Foundry 厂,除了台湾的 TSMC 在世界有立足之地,Fabless 的 IC 设计公司倒是挺强的如 Hisilicon,这也是它为什么会被 USA 针对的原因)。

新芯设计的博客 2012

深度学习工具包 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'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('DeepLearnToolbox'));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('state',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'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',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 = 'sigm';%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, 'Too big error');```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('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';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)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';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, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% 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'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 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, 'Too big error');```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('state',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, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',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, 'Too big error');%% ex3 neural net with dropoutrand('state',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, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % 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, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % 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, 'Too big error');%% 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('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % 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, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: unity中的灯光烘焙问题lightMap
下一篇: unity3d优化总结篇
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值