I’ve looked for an answer on this all over none cove using the two together very well. Im trying to use NavMesh to have the enemy run to the player then attack if the player is in range i finally got it animation but its not running to the player its just running to nowhere here is my code so far any help would be appreciated i have created the navMesh Layer already and the enemy object has a navMeshAgent on it the Target is set to the Player GameObject
public class Agent : MonoBehaviour
{
public Transform target;
public Transform enemy;
NavMeshAgent agent;
Animator anim;
int attackHash = Animator.StringToHash(“Attack”);
int speedHash = Animator.StringToHash(“Speed”);
public float speed;
protected Locomotion locomotion;
void Start()
{
agent = GetComponent();
enemy = GetComponent ();
anim = GetComponent();
SetupAgentLocomotion();
locomotion = new Locomotion(anim);
}
void Update()
{
CurSpeed = agent.speed;
agent.SetDestination(target.position);
SetSpeed ();
SetupAgentLocomotion();
}
public void SetSpeed ()
{
anim.SetFloat (“Speed”, speed);
}
protected void SetupAgentLocomotion()
{
if (AgentDone())
{
locomotion.Do(0, 0);
}
else
{
speed = agent.desiredVelocity.magnitude;
Vector3 velocity = Quaternion.Inverse(transform.rotation) * agent.desiredVelocity;
float angle = Mathf.Atan2(velocity.x, velocity.z) * 180.0f / 3.14159f;
locomotion.Do(speed, angle);
}
}
void OnAnimatorMove()
{
agent.velocity = anim.deltaPosition / Time.deltaTime;
transform.rotation = anim.rootRotation;
}
protected bool AgentDone()
{
return !agent.pathPending && AgentStopping();
}
protected bool AgentStopping()
{
return agent.remainingDistance <= agent.stoppingDistance;
}
}