【问题标题】:Detect and follow the player检测并跟随玩家
【发布时间】:2020-01-19 23:30:04
【问题描述】:

我已经开始了一个新游戏。我有一个敌人,当玩家与敌人保持一定距离时,他会攻击。我的脚本有效并且敌人跟随玩家,但是尽管我设置了数字,但它仍然跟随玩家。 我需要敌人在离玩家足够近后才跟随。 我有一个空物体附在敌人身上,脚本就在上面。

我在统一社区答案中寻找答案,并在此链接中找到我使用的脚本https://answers.unity.com/questions/274809/how-to-make-enemy-chase-player-basic-ai.html 我也用谷歌搜索了它,但找不到任何正确的解决方案。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AIController : MonoBehaviour
{
    public int AttackTrigger2;
    public Transform Player;
    public int MoveSpeed = 4;
    public int MaxDist = 10;
    public int MinDist = 5;

    void Update()
    {
        transform.LookAt(Player);

        if (Vector3.Distance(transform.position, Player.position) >= MinDist)
        {               
            transform.position += transform.forward * MoveSpeed * Time.deltaTime;       
            if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
            {
                //Here Call any function U want Like Shoot at here or something
            }   
        }
    }
}

我的代码没有错误,他做了我需要的,但我需要在我的玩家离开一定距离后敌人停止跟随玩家。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我猜您链接的论坛中的下一个答案实际上可以解决您的问题。问题只是复制和粘贴,而不了解为什么某些行为应该如此。在这种情况下:

    >= MinDist 
    

    表示只要大于等于MinDist,敌人就会跟随玩家,在本例中为5。我猜你想要的是:

    <= MaxDist
    

    这样敌人只有在距离小于 10 时才会跟随。如果距离超过 10 个,请停止关注。

    【讨论】:

      【解决方案2】:

      您应该在第一个 if 案例中更改条件。 根据您的代码,如果他们之间的距离大于 MinDist,敌人将跟随玩家。 将&gt;= 替换为&lt;=。 我想你可能想要这样的东西。

      if (Vector3.Distance(transform.position, Player.position) <= MaxDist)//not MinDist
              {               
                  transform.position += transform.forward * MoveSpeed * Time.deltaTime;       
                  if (Vector3.Distance(transform.position, Player.position) <= MinDist)//not MaxDist
                  {
                      //Here Call any function you want, like Shoot or something
                  }   
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多