update: pause code added
As per title, if the game is paused, everything stops, even the NPC pauses when patrolling. However, when the NPC goes from idle and patrol to chase mode, it goes after the player. When paused though during chase mode, the animations pause but the NPC still moves slowly, which I noticed the X and Z position increases. I’m assuming it has to do with the NPC’s transform but after adding and removing bits, still no idea why.
Heres the coding:
using UnityEngine;
using System.Collections;
public class CHASE : MonoBehaviour
{
public Transform player;
public Transform myTransform;
static Animator anim;
public float moveSpeed = 3.0f;
public Transform[] points;
private int destPoint = 0;
private NavMeshAgent agent;
void Start ()
{
anim = GetComponent<Animator> ();
agent = GetComponent<NavMeshAgent>();
agent = GetComponent<NavMeshAgent>();
GotoNextPoint();
}
void Update ()
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
float angle = Vector3.Angle (direction, this.transform.forward);
if (Vector3.Distance (player.position, this.transform.position) < 30 && angle < 180)
{
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
this.transform.rotation = Quaternion.Slerp
(this.transform.rotation, Quaternion.LookRotation (direction) , 0.1f * Time.deltaTime * 50.0f) ;
anim.SetBool ("isIdle", false);
if (direction.magnitude > 5)
{
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
this.transform.rotation = Quaternion.Slerp
(this.transform.rotation, Quaternion.LookRotation (direction) , 0.1f * Time.deltaTime * 50.0f) ;
this.transform.Translate (0f * Time.deltaTime, 0f * Time.deltaTime, 0.05f);
anim.SetBool ("isChasing", true);
anim.SetBool ("isDeath", false);
} else {
anim.SetBool ("isChasing", false);
anim.SetBool ("isDeath", false);
}
}else if(agent.remainingDistance < 0.5f)
{
anim.SetBool("isIdle", true);
anim.SetBool("isChasing", false);
anim.SetBool("isDeath", false);
GotoNextPoint();
}
}
void GotoNextPoint()
{
// Returns if no points have been set up
if (points.Length == 0)
return;
Debug.Log ("Not chasing", gameObject);
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
}
Heres also the pause script
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Pause_menu : MonoBehaviour
{
GameObject[] pauseObjects;
// Use this for initialization
void Start ()
{
Time.timeScale = 1;
pauseObjects = GameObject.FindGameObjectsWithTag("ShowOnPause");
hidePaused();
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
if(Time.timeScale == 1)
{
Time.timeScale = 0;
showPaused();
} else if (Time.timeScale == 0){
Debug.Log ("high");
Time.timeScale = 1;
hidePaused();
}
}
}
//Reloads the Level
public void Reload()
{
SceneManager.LoadScene(3);
}
public void Reload_MM()
{
SceneManager.LoadScene(("Main menu"));
}
//controls the pausing of the scene
public void pauseControl()
{
if(Time.timeScale == 1)
{
Time.timeScale = 0;
showPaused();
} else if (Time.timeScale == 0)
{
Time.timeScale = 1;
hidePaused();
}
}
//shows objects with ShowOnPause tag
public void showPaused()
{
foreach(GameObject g in pauseObjects)
{
g.SetActive(true);
}
}
//hides objects with ShowOnPause tag
public void hidePaused()
{
foreach(GameObject g in pauseObjects)
{
g.SetActive(false);
}
}
//loads inputted level
public void ExitGame()
{
Application.Quit();
}
}