unity3d - 防止对角线运动
问题描述
我正在开发一款 3D 战术 RPG 游戏,我想知道如何在 NPC 移动时防止对角线移动?我希望他能检测到人类玩家在网格中的位置,然后去追他。
我可以采取他们的网格位置,但它没有用。NPC 进行对角线运动并开始在网格中“下沉”。我尝试过这样的事情,目标位置是向量 3,我在 Update() 中调用函数,这个脚本附加到 NPC:
public void MoveNPC()
{
HumanPlayer = GameObject.FindGameObjectWithTag("Player");
ScriptPlayer PlayerPosition = HumanPlayer.GetComponent<ScriptPlayer>();
NPC NPCPosition = this.GetComponent<NPC>();
if ((PlayerPosition.GridPosition.x - NPCPosition.GridPositonNPC.x) + PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) >= movMax &&
(PlayerPosition.GridPosition.x -NPCPosition.GridPositonNPC.x) + (PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) <= movMax)
{
if((PlayerPosition.GridPosition.x - NPCPosition.GridPositonNPC.x) - (PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) >= movMax &&
(PlayerPosition.GridPosition.x - NPCPosition.GridPositonNPC.x) - (PlayerPosition.GridPosition.y - NPCPosition.GridPositonNPC.y) <= movMax){
targetPosition = HumanPlayer.transform.position;
targetPosition.y = 1.4f;
}
}
if(this.transform.position != targetPosition)
{
this.transform.position = Vector3.MoveTowards(transform.position, targetPosition, velocity * Time.deltaTime);
}
}
我该如何解决?
解决方案
推荐阅读
- javascript - 比较 3 个数组并在 javascript 中找到组合
- html - 在eclipse中创建新的html文件时如何更改默认文本?
- ubuntu-20.04 - BladeRF-cli --interactive 错误文件:未配置
- java - 在“org.eclipse.pde.build”包中添加“pdebuild.jar”到编译类路径?
- python - 如何检测线条 - OpenCV
- angular - Angular material 12自定义排版不起作用
- java - 是否可以同时从实体和列表对象查询
- angular - 在 Angular 共享模块中提供类型的推荐方法是什么?
- java - 确保首先调用 FirebaseApp.initializeApp(Context)。默认 FirebaseApp 在此过程中未初始化
- python - 将商品添加到购物篮时出现 AttributeError