I want the NavMesh agent in unity to stop walking once my Attack() method is called. And then after a couple of seconds, i want the AI to start walking again, at its normal walking speed. I have tried writing something but it dosent work, and i dont know why??
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AiLoco3 : MonoBehaviour
{
private Transform player;
private NavMeshAgent agent;
Animator animator;
public float agentSpeed = 2f;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
agent.speed = agentSpeed;
}
void Update()
{
agent.destination = player.position;
animator.SetFloat("Speed", agent.velocity.magnitude);
//Debug.Log(agent.velocity.magnitude);
}
public void Attack()
{
Debug.Log("ATTACK");
agent.speed = 0f;
StartCoroutine(ResumeWalking());
}
IEnumerator ResumeWalking()
{
yield return new WaitForSeconds(2);
agent.speed = 2f;
}
public void Charge()
{
Debug.Log("Run");
agent.speed = 8f;
Invoke("ChargeTime", 2.0f);
}
public void ChargeTime()
{
agent.speed = agentSpeed;
}
}
The Attack Method is called from a other script, that works perfectly. Because when the Attack Method is called it does debug "ATTACK" however it does not stop walking at all, it just keeps walking, like nothing ever happend.
And i dont get any error messages.