Question by
brayden-lee · Aug 07, 2017 at 01:33 AM ·
c#unity 5prefabnavmeshnavigation
How Can I make a prefab using navmesh alternate destination targets?
Hi guys,
I have a "Golem" prefab that is currently using a navmesh to move to the "end" destination. This works fine however, what I would like to do is: when a "cannon" appears in the scene, have the Golem prefab change its target to the cannon, and then change back again to "End" when the cannon is no longer in the scene.
Thanks in advance!
public class GolemEnemy : MonoBehaviour {
public GameObject Cannon;
public GameObject End;
public float DistanceFromCannon = 5.0f;
public float Speed = 3f;
// Use this for initialization
void Start () {
Cannon = GameObject.FindGameObjectWithTag("Cannon");
if (!Cannon)
{
End = GameObject.FindGameObjectWithTag("End");
}
}
// Update is called once per frame
void Update () {
if (Cannon)
{
GetComponent<NavMeshAgent>().destination = Cannon.transform.position;
}
else
{
GetComponent<NavMeshAgent>().destination = End.transform.position;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How do I cancel baking?? 1 Answer
How do I Instantiate a prefab at the x axis position of where the UI button was clicked 1 Answer
Navmesh How to check if full path available? C# 5 Answers
Object reference not set to an instance of an Object? 0 Answers
Solved: Cant get onclick assignet to prefabs button 1 Answer