首页 > 解决方案 > 如何在 Unity3d 中创建一个“击退”(逃离)玩家的 NPC

问题描述

我已经研究了大约一个小时来研究如何让 NPC “击退”,意思是逃离 Unity3d 中的玩家。无论我在哪里搜索,我都能找到很多关于如何让 NPC 跑向玩家但从不击退的东西。我是游戏开发的新手,所以我不知道如何做这些事情。

标签: unity3d

解决方案


取玩家和 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); 线。


推荐阅读