【问题标题】:AI is trying to move to my position, it makes me stuckAI 试图移动到我的位置,这让我卡住了
【发布时间】:2018-03-13 18:06:18
【问题描述】:

player Cameracameraenemyenemy

我尝试从 Brackeys 教程中做简单的 AI,它可以工作,但是当我离立方体太近时,它会“消耗”我。我无法摆脱它。我没有任何玩家角色模型,我只有相机,我烤了地板(不管是什么意思,我在 cmets 上找到了这个技巧,它帮助 AI 移动)。 第二件事是立方体从地图下方的一半开始,如果我将其抬起,则它根本不会移动。我还有一个骨架资产模型,如何使用它成为敌人的模型?同样在 cmets 中我读到它不适用于第二个立方体,我应该改变什么来拥有多个敌人?这是我的代码:

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

public class PlayerManager : MonoBehaviour {

    #region Singleton
    public static PlayerManager instance;
     void Awake()
    {
        instance = this; 
    }
    #endregion

    public GameObject player;
}

第二个脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyControler : MonoBehaviour {

    public float LookRadius = 10f;
    Transform target;
    NavMeshAgent agent;

    void Start ()
    {
        agent = GetComponent<NavMeshAgent>();
        target = PlayerManager.instance.player.transform;
    }

    void Update () {
        float distance = Vector3.Distance(target.position, transform.position);

        if (distance <= LookRadius)
        {
            agent.SetDestination(target.position);

            if (distance < agent.stoppingDistance)
            {
                FaceTarget();
            }
        }
    }
    void FaceTarget ()
    {
        Vector3 direction = (target.position = transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, LookRadius);
    }
}

字符控制器:mysz=mouse 和 klawiatura=keybord,其余单词是高度、速度值等。

using UnityEngine;
using System.Collections;

public class PlayerControler : MonoBehaviour
{

    //Obiekt odpowiedzialny za ruch gracza.
    public CharacterController characterControler;

    public float predkoscPoruszania = 9.0f;
    public float wysokoscSkoku = 7.0f;
    public float aktualnaWysokoscSkoku = 0f;
    public float predkoscBiegania = 7.0f;

    public float czuloscMyszki = 3.0f;
    public float myszGoraDol = 0.0f;
    public float zakresMyszyGoraDol = 90.0f;

    void Start()
    {
     characterControler = GetComponent<CharacterController>();
    }
    void Update()
    {
        klawiatura();
        myszka();

    }

    private void klawiatura()
    {

        float rochPrzodTyl = Input.GetAxis("Vertical") * predkoscPoruszania;

        float rochLewoPrawo = Input.GetAxis("Horizontal") * predkoscPoruszania;

        if (characterControler.isGrounded && Input.GetButton("Jump"))
        {
            aktualnaWysokoscSkoku = wysokoscSkoku;
        }
        else if (!characterControler.isGrounded)
        {
            aktualnaWysokoscSkoku += Physics.gravity.y * Time.deltaTime;
        }

        Debug.Log(Physics.gravity.y);

        if (Input.GetKeyDown("left shift"))
        {
            predkoscPoruszania += predkoscBiegania;
        }
        else if (Input.GetKeyUp("left shift"))
        {
            predkoscPoruszania -= predkoscBiegania;
        }
        Vector3 ruch = new Vector3(rochLewoPrawo, aktualnaWysokoscSkoku, rochPrzodTyl);
        ruch = transform.rotation * ruch;
        characterControler.Move(ruch * Time.deltaTime);
    }

    private void myszka()
    {
         if (!PauseMenu.GameIsPaused)
            {
            float myszLewoPrawo = Input.GetAxis("Mouse X") * czuloscMyszki;
            transform.Rotate(0, myszLewoPrawo, 0);

            myszGoraDol -= Input.GetAxis("Mouse Y") * czuloscMyszki;
            myszGoraDol = Mathf.Clamp(myszGoraDol, -zakresMyszyGoraDol, zakresMyszyGoraDol);
            Camera.main.transform.localRotation = Quaternion.Euler(myszGoraDol, 0, 0);
            }
    }

}

【问题讨论】:

  • " 烤了地板(不管是什么意思,我在 cmets 上发现了这个技巧,它帮助 AI 移动)",如果你使用的是 NavMesh 系统,这意味着有一个平面 2d 地图AI可以导航。这比在 3d 空间中导航要快得多。因此,使用预烘焙的地图,它可以正常运行。
  • "第二件事是立方体从地图下方的一半开始,如果我把它抬起来" 立方体是 AI 吗?人工智能应该靠近你吗?
  • 艾应该会走向我并攻击我(但我目前没有任何战斗系统,我会添加它),我希望它像在天际一样工作,问题立方体是“进入”播放器的,我不知道它是因为我没有播放器模型还是只是一个糟糕的脚本。我还检查了烘焙区域,它不像 2d,它检测到我在地形上制作的丘陵和山脉,但立方体仍在地图下方的一半

标签: unity3d artificial-intelligence


【解决方案1】:

当我离立方体太近时,它会“消耗”我。我不能离开它。

您希望多维数据集具有什么行为?跟着你,但不是那么密切?走得比你慢?

如果您希望它停得更远,您可以在检查器中增加agent.stoppingDistance

如果您希望它移动得更慢,您可以调整它用来移动的 NavMeshAgent 组件的速度。这将位于立方体 GameObject 上。

我烤了地板(不管是什么意思,我在 cmets 上发现了这个技巧,它帮助 AI 移动)

烘焙 用于各种目的,但在大多数情况下(如果不是所有情况)都是为了将​​复杂的信息简化为更有用的东西,同时也对性能友好。在您的情况下,这听起来像是您烘焙了一个导航网格,它就像一个覆盖您的静态对象的平面 2d 地图。如果您有寻路系统(例如使用 NavMeshAgents),AI 可以使用它来导航。如果您转到 Window -> Navigation 并且场景视图上出现一个蓝色区域,您应该能够看到它,这将是您的烘焙导航地图。

我还有一个骨架资产模型,如何使用它成为敌人的模型?

将它拖到场景视图中,它就变成了一个游戏对象。您现在可以将组件和脚本附加到它上,就像您处理的立方体或其他对象一样。

另外在 cmets 中我读到它不适用于第二个立方体,我应该改变什么来拥有多个敌人

在代码中,您通过单例编程模式(“实例”部分)设置目标。这意味着永远只能有其中一个目标。

如果您的意图是拥有更多允许的目标,那么您不应该使用单例模式,而是使用不同的方法来查找目标,例如检查他们是否进入了设置为触发器的对撞机。

如果我误解了什么,请详细说明。

请详细说明您要达到的目标,这样我就不必推测了:-)。记住这一点以备将来的帖子。虽然一般来说是一篇不错的帖子,因为您确实提供了很多信息。

编辑:对撞机触发器示例

void OnTriggerEnter(Collider other) {
    Debug.Log("Trigger entered"); //Just to write in console when trigger is activated.
    if (other.gameObject.CompareTag("Player")) //Check the tag, this can be set at the top of the inspector when you select the player, look top left in the inspector window.
    {
    //Player entered, attack him!
    //other == player in this case.
    }
}

【讨论】:

    猜你喜欢
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多