📜  如何统一使用 navmeshagent - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:13.291000             🧑  作者: Mango

代码示例1
using UnityEngine;
using UnityEngine.AI; //using the AI system for navmeshaganet

public class enemybehavior : MonoBehaviour
{
    NavMeshAgent enemy; //call an object to navigate using the AI system of navmesh

    public GameObject dieAfterImpact;

    // Start is called before the first frame update
    void Start()
    {
        enemy = GetComponent(); //assign the name to the NavMeshAgent itself (because there is no need to use a public one)
    
    }
    //until here is what you really need to assig a NavMeshAgent , the rest below is just to add
    
    
    
    
    

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) //when i press a key, in this example is SPACEBAR
        {
            enemy.SetDestination(tower.position); //make the enemy get to the target using the navigation system and NavMeshAgent
        }
    }

    private void OnCollisionEnter(Collision collision) // in my case i imported an object from blender and its 
                                                       //pivot point was not in the middle of the object, so i used
                                                       //an Empty GameObject to make it its center, so i have 2 types 
                                                       //of Destroy() here, one for the object and one for the Empty.
    {
        if (collision.gameObject.tag == "Tower") //using tags to know the target
        {
            Destroy(dieAfterImpact); //kills specified gameobject
            Destroy(gameObject); //kills itself
        }
    }


}