Question by
el-mas-pro-470 · Jan 08, 2019 at 04:31 PM ·
ainavmeshnavmeshagentfollowbake
Navmesh dont work!
Hello! what is the error? Navmesh Agent do not follow the player! Thanks
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public Transform Target;
public Animator Animador;
public float Distancia = 30;
public float LastDistance = 50;
public bool Searching = false;
public float RotationDamping = 2;
public float moveSpeed = 5;
public Vector3 TargetPosition;
public NavMeshAgent EnemyNV;
void Start()
{
Animador.SetBool("Aiming", false);
TargetPosition = Target.transform.position;
EnemyNV = GetComponent<NavMeshAgent>();
}
void Update()
{
var distance = Vector3.Distance(Target.position, transform.position);
if(distance <= Distancia)
{
LookAtTarget();
Animador.SetBool("Aiming", true);
Animador.SetBool("WalkingSearch", false);
Animador.SetBool("Idle", false);
}
if(distance > Distancia)
{
SearchTarget();
Animador.SetBool("Aiming", false);
Animador.SetBool("WalkingSearch", true);
Animador.SetBool("Idle", false);
Searching = true;
}
if(distance > LastDistance)
{
LastTarget();
}
}
void LookAtTarget()
{
var dir = Target.position - transform.position;
dir.y = 0;
var rotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * RotationDamping);
}
void SearchTarget()
{
if(Searching == true)
{
Animador.SetBool("WalkingSearch", true);
EnemyNV.SetDestination(Target.position);
}
}
void LastTarget()
{
Searching = false;
Animador.SetBool("Aiming", false);
Animador.SetBool("WalkingSearch", false);
Animador.SetBool("Idle", true);
}
}
Comment