叉乘实现角色和敌人的位置判断(左上,左下,右上,右下)
2015-08-10 00:32
591 查看
我们常常在游戏中遇到这种问题. 比如敌人遇到了主角就会朝他旋转过去. 或者判断主角在左边还是右边等等
效果图:
向量A,B的叉乘获得一个垂直于他们的C向量,我们可以通过这上面的值来判断敌人四个区域的某一区
代码的实现:
项目下载地址: http://yunpan.cn/cdYkG48mxIGGD 访问密码 54c3
效果图:
向量A,B的叉乘获得一个垂直于他们的C向量,我们可以通过这上面的值来判断敌人四个区域的某一区
代码的实现:
using UnityEngine; using System.Collections; public class Test : MonoBehaviour { public Transform a; public Transform b; public TextMesh text; public TextMesh oneResult; public TextMesh twoResult; public float distance; private float dot; public void Update() { //B在A的前方,A是主角,B是敌人 Vector3 toOther = a.transform.position - b.position; //获取的Y可以判断敌人 在人物的左边和右边 Vector3 chaCheng1 = Vector3.Cross(a.forward, toOther); Vector3 chaCheng2 = Vector3.Cross(a.right, toOther); oneResult.text = chaCheng1.ToString(); twoResult.text = chaCheng2.ToString(); Debug.Log("第一次值: " + chaCheng1); //获取的Y可以判断敌人 在人物的前边和后边 Debug.Log("第二次值: " + chaCheng2); if (chaCheng1.y > 0 && chaCheng2.y > 0) { text.text = "位置: 左上"; } if (chaCheng1.y < 0 && chaCheng2.y < 0) { text.text = "位置: 右下"; } if (chaCheng1.y > 0 && chaCheng2.y < 0) { text.text = "位置: 左下"; } if (chaCheng1.y < 0 && chaCheng2.y > 0) { text.text = "位置: 右上"; } } }
项目下载地址: http://yunpan.cn/cdYkG48mxIGGD 访问密码 54c3
相关文章推荐
- 弹出框
- Regionals 2009 Asia - Hsinchu UVALIVE, 4525 - Clues 搜索
- 2015-08-09
- 【LeetCode】38 - Count and Say
- Mysql导入导出
- linux系统忘root密码怎么办
- 剑指Offer——面试小提示(持续更新中)
- linux系统启动
- 编写Shader时的一些性能考虑
- 2015 Multi-University Training Contest-5 MZL's xor
- akka 之 Dispatcher学习笔记
- hdu1022(Train Problem I)----- 典型栈类题目
- Foundation框架基础 - 常用结构体
- Java生成字母和数字组成的随机字符串
- sed高级用法:模式空间(pattern space)和保持空间(hold space)
- 10进制数转16位2进制补码
- TCP三次握手及其背后的缺陷
- iOS应用内部切换语言,不跟随系统
- iOS 宏(define)与常量(const)的正确使用
- 《搜索引擎---信息检索实践》(读书笔记)