unity3d - 如何在 Unity3d 中创建一个“击退”(逃离)玩家的 NPC
问题描述
我已经研究了大约一个小时来研究如何让 NPC “击退”,意思是逃离 Unity3d 中的玩家。无论我在哪里搜索,我都能找到很多关于如何让 NPC 跑向玩家但从不击退的东西。我是游戏开发的新手,所以我不知道如何做这些事情。
解决方案
取玩家和 NPC 的 Vector3 位置并执行以下计算:
Vector3 direction = NPCPosition - playerPosition;
direction.y = 0;
direction = Vector3.Normalize(direction);
NPCTransform.rotation = Quaternion.Euler(direction);
//MOVE NPC forward (the calculations above calculate the direction away from the player)
NPCTransform.Translate(NPCTransform.position + (transform.forward * NPCSpeed));
我希望这会有所帮助。:)
如果有什么不清楚的,尽管问我。:D
编辑:我添加了方向 = Vector3.Normalize(direction); 线。
推荐阅读
- java - 包裹 Java EE bean 的拦截器
- linux - 挂载的linux文件系统访问
- spring - 如何将下拉列表中的信息提交到 Spring Controller?
- c++ - 隐式调用是什么意思?
- list - 从列表中创建一个虚线对列表,将其对数划分为另一个数
- c++ - 从私有成员变量的成员方法返回 unique_ptr
- spring - 导入 org.springframework.boot.actuate.metrics.CounterService 无法解析
- java - 低功耗蓝牙正在下降特性
- database - 是否可以在 SSIS 中加入合并相同的数据库列?
- android - 相机分辨率