- Home /
Navmesh agent problem
İ have agent idle...i made trigger to when i pass as FPS controller agent follow me and game over..But agent start follow but it stops place where i pass trigger not following me...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class hedefbul : MonoBehaviour
{
public GameObject target;
public NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void OnTriggerEnter()
{
agent.SetDestination(target.transform.position);
}
}
Answer by unity_ek98vnTRplGj8Q · Jan 27, 2020 at 08:03 PM
Right, you only set the target position once. If you want it to follow an object, you need to update your path repeatedly. There may be a cleaner way to do this, but here is an easy way -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class hedefbul : MonoBehaviour
{
public GameObject target;
public NavMeshAgent agent;
public float pathUpdateInterval = 1f;
private GameObject activeTarget = null;
private float timer = 0f;
// Start is called before the first frame update
void Start()
{
}
void Update(){
if(activeTarget == null) return;
timer += Time.deltaTime;
if(timer >= pathUpdateInterval){
timer = 0;
agent.SetDestination(activeTarget.transform.position);
}
}
// Update is called once per frame
void OnTriggerEnter()
{
activeTarget = target;
timer = pathUpdateInterval;
}
}
but my enemy will be idle first..so when idle even if i collide with enemy..FPS will not die..but after trigger which above activated when FPS collide with enemy FPS will die..how can i do that ? it is so confusing..i am beginner at unity and my english not perfect sorry about that..
I'm sorry, can you rephrase your question? I'm not quite sure what you are asking