在Unity中创建敌人AI脚本的最简单方法是什么?

问题描述 投票:0回答:1

我是意大利人,这是我编程的第一周团结,我尝试为跟随并攻击玩家的僵尸创建脚本。

我的债务是:

  • 为什么僵尸可以通过播放器?
  • 这是创建此脚本的最佳简单方法吗?这是navmesh的正确用法吗?
  • 如何在navmesh中使用物理学?一些代码行是我在网上找到的示例(随机移动功能),我需要一些帮助和技巧。

我上传了脚本,并添加了一些注释以澄清代码。

public class simpleNav : MonoBehaviour
{
    [SerializeField] public Animator myAnimationController;
    public GameObject target;
    NavMeshAgent agent;

    public int vita_z;
    public bool morto;

    private float timer;
    public float wanderRadius;
    public float wanderTimer;


    // Start is called before the first frame update
    void Start()
    {

        agent = GetComponent<NavMeshAgent>();

        myAnimationController.SetBool("condizione", false);   //this is the start animation, the zombie is in idle
        morto = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (morto != true) //if zombie isn't die
        {
            agent.isStopped = false;
            float dist = Vector3.Distance(transform.position, target.transform.position);
            myAnimationController.SetBool("condizione", true); //start walk animation


            if (dist <= 20)
            {  
                agent.SetDestination(target.transform.position);
                agent.speed = 5;
                myAnimationController.SetBool("corre", true); //start run animation
            }



            else
            {
                myAnimationController.SetBool("corre", false); //stop run animation, walk animation starts
                agent.speed = 1.3F;
                timer += Time.deltaTime;

                if (timer >= wanderTimer)
                {
                    Vector3 newPos = RandomNavSphere(transform.position, wanderRadius, -1); //function from internet to generate random movement
                    agent.SetDestination(newPos);

                    timer = 0;
                }
            }
        }

       else agent.isStopped = true;
    }


    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "bullet")
        {
            vita_z -= 10; //if bullet enter, the life of the zombie will be decremented

            if (vita_z <= 0 && morto != true) //when life <= zero, play the die animation
            {
                punt_e_vita.currentScore += 10; //player's score increment
                morto = true; //die = true
                myAnimationController.SetBool("sparato", true); //die animation
            }
        }

        else if (other.gameObject.tag == "player")
        {

            if (morto != true)
            {
                punt_e_vita.vita -= 5; //decrement the life of the player
            }
        }
    }

    private void OnTriggerStay(Collider other)
    {
        if(other.gameObject.tag == "player")
        {

            myAnimationController.SetBool("attacca", true); //this is attack animation if the object in collider is the palyer

        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "player")
        {
            myAnimationController.SetBool("attacca", false);//stop attack
        }

        myAnimationController.SetBool("sparato", false);

    }


    public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask) //this is the random movement function
    {
        Vector3 randDirection = Random.insideUnitSphere * dist;

        randDirection += origin;

        NavMeshHit navHit;

        NavMesh.SamplePosition(randDirection, out navHit, dist, layermask);

        return navHit.position;
    }
}
c# unity3d
1个回答
0
投票

如果您想让敌人避免与玩家碰撞,或希望导航避免某些对象对其使用障碍组件并将其更改为动态,则您的导航网格将在运行时更新,并且代理将避免与这些对象发生碰撞。

至于脚本,这只是您可以通过事物的工作方式来实现或学习的基本功能,但是脚本大多基于您要实现的目标,如果您只想跟随并被玩家击落,那就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.