Issues with pause menu, even when I pause the game enemy AIs still flow to the player (no animation) if it's pursing the player.
I'm working on a game using the FPS controller and enemy objects. Enemy AI and the FPS controller both have a capsule collider, and if two collide, it will cause damage to the player's HP. But when I run it, if the enemy is pursuing the player, even if I press ESC to pause the scene, the enemy objects still flow toward the player (no animations or attack move executed), it will stop next to the player. When I unpause the scene, then it immediately starts attacking (since it flow to the player's position) and causing damage to the player. I don't know how every object in the scene is not paused using Time.timeScale = 0f; Please help!
This is my PauseMenu script, attached to the Canvas of each scene.
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
public FirstPersonAIO myFPSAIO;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if(GameIsPaused)
{
Resume();
}
else
{
Pause();
}
}
}
public void Resume()
{
print("Inside resume");
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
myFPSAIO.ControllerSetPause(false);
}
void Pause()
{
pauseMenuUI.SetActive(true);
GameIsPaused = true;
myFPSAIO.ControllerSetPause(true);
Time.timeScale = 0f;
}
public void LoadMenu()
{
Time.timeScale = 1f;//Return to beta menu
//print("Loading Menu");
SceneManager.LoadScene("BetaMenu");
}
public void QuitGame()
{
//print("Quit Game");
Application.Quit();
}
}
And this is my enemy script. It will start following the player after it enters a certain distance and starts attacking once it's close to the player (both FPS controller and enemy objects has a capsule collider)
public float speed;
public Transform player;
Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
//If want the enemy to have limited angle of vision, use this code, and remove it from if statement
//Vector3 direction = player.position - this.transform.position;
//float angle = Vector3.Angle(direction, this.transform.forward);
if (Vector3.Distance(player.position, this.transform.position) < 10 /*&& angle < 30*/)//If player get closer than 10 then follow
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle", false);
if (direction.magnitude > 5)//If player are further than 5 then stop follow
{
this.transform.Translate(0, 0, speed);
anim.SetBool("isWalking", true);
anim.SetBool("isAttacking", false);
}
else
{
anim.SetBool("isAttacking", true);
anim.SetBool("isWalking", false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
}
}
}