Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to use navmeshagent in unity

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<NavMeshAgent>(); //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
        }
    }


}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# int to string 
Csharp :: remove all non alphabetic characters from string c# 
Csharp :: C# function return datareader 
Csharp :: check if string variable contains only letters c# 
Csharp :: regex c# 
Csharp :: how to stop animation unity 
Csharp :: unity instantiate prefab 
Csharp :: item switch unity 
Csharp :: unity audio manager 
Csharp :: C# add two numbers using a method 
Csharp :: unity print vs debug log 
Csharp :: null coalescing operator c# 
Csharp :: c# convert datetime to unix timestamp 
Csharp :: c# datagridview rows clear not working 
Csharp :: unity create empty gameobject in code 
Csharp :: parent unity 
Csharp :: unity find gameobject with layer 
Csharp :: c# load form 
Csharp :: integer required asp.net core 
Csharp :: set rotation unity 
Csharp :: transform.position.x unity 
Csharp :: how to print statement in c# 
Csharp :: c# public static string 
Csharp :: join array in c# 
Csharp :: asp.net response.redirect new tab 
Csharp :: from string 
Csharp :: maclaurin series 
Csharp :: What is the yield keyword used for in C#? 
Csharp :: c# next level script 
Csharp :: c# example code 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =