// Start is called before the first frame update void Start() { currentKeyNode = KeyNodes[Random.Range(0, GameManager.instance.KeysRequired)]; path = currentKeyNode; transform.position = path.transform.position; rigidbody = GetComponent <Rigidbody>(); meshAgent = GetComponent <NavMeshAgent>(); moveSpeed = 5; currentState = enemyAiState.PATROL; FindAudioManager(); meshAgent.enabled = true; meshAgent.SetDestination(path.transform.position); //StartCoroutine("Step"); }
// Update is called once per frame protected virtual void Update() { if (GameManager.instance.CurrentState == gameState.PAUSE) { meshAgent.enabled = false; return; } if (meshAgent.enabled == false) { meshAgent.enabled = true; meshAgent.SetDestination(currentKeyNode.position); } if (capturedEnemy) { return; } //Chasing an invisible player takes priority over everything else so this line gets to be outside the state machine //but there probably shouldn't be too much else out here detectPlayerResults = DetectPlayer(); if (detectPlayerResults) { // If the enemy has found the player before entering chase state, start playing the chase music if (!foundPlayer) { audioManager.PlayChase(); } // enemy has found player foundPlayer = true; currentState = enemyAiState.CHASE; } switch (currentState) { case enemyAiState.PATROL: //The enemy normally walks around from node to node all over the map, looking for evidence //The enemy starts in this state and comes back to it after giving up after a certain amount of time spent in INVESTIGATE Patrol(); break; case enemyAiState.CHASE: //The enemy enters this state if they actively see the player during any of the other states //They pursue the player //If I lose sight of the player then this happens if (!detectPlayerResults && Vector3.Distance(transform.position, meshAgent.destination) <= 1) { // If the enemy had found the player before going into investigate, stop playing hte chase music if (foundPlayer) { audioManager.StopChase(); } // the enemy has not found the player foundPlayer = false; currentState = enemyAiState.INVESTIGATE; currentKeyNode = path; path = FindPath(); //Find the closest Node to where the player was and look around there meshAgent.SetDestination(path.transform.position); //probably need some way to mark a node as "evidence" should be whatever node the player was closest too when they were last seen } break; case enemyAiState.INVESTIGATE: //The enemy enters this state when they finish ALERTED //They will basically do the same thing they do in patrol, but about a small collection of nodes (which the one ALERTED took them too belongs too) //instead of all nodes on the map Patrol(); break; } }
protected void EndInvestigation() { currentState = enemyAiState.PATROL; path = currentKeyNode; timer = 0; }
protected bool DetectPlayer() { //Calculating the player mask for our player int layerMask = 1 << 8; layerMask = layerMask | 1 << 9; //Projecting our rays for (int i = 0; i < NUM_OF_RAYS; i++) { //Calculating direction of the ray Vector3 rayDir = new Vector3(Mathf.Sin((transform.rotation.eulerAngles.y + (((float)i / NUM_OF_RAYS) * CONE_DEGREES) - CONE_DEGREES / 2) * Mathf.Deg2Rad), .05f , Mathf.Cos((transform.rotation.eulerAngles.y + (((float)i / NUM_OF_RAYS) * CONE_DEGREES) - CONE_DEGREES / 2) * Mathf.Deg2Rad)); RaycastHit hit; Player potentialPlayer; //Shooting out a raycast in that direction bool detected = Physics.Raycast(new Vector3(transform.position.x, transform.position.y + 0.2f, transform.position.z), rayDir, out hit, VISION_DISTANCE, layerMask); if (detected) { if (hit.collider.gameObject.TryGetComponent <Player>(out potentialPlayer)) { //SEEK PLAYER HIT //If we find the player we go into chase mode and adjust our speed meshAgent.SetDestination(potentialPlayer.transform.position); meshAgent.speed = Mathf.Lerp(meshAgent.speed, MAX_SPEED, .005f); currentState = enemyAiState.CHASE; return(true); } } } for (int i = 0; i < NUM_OF_RAYS; i++) { //Calculating direction of the ray Vector3 rayDir = new Vector3(Mathf.Sin((-transform.rotation.eulerAngles.y + (((float)i / NUM_OF_RAYS) * CONE_DEGREES) - CONE_DEGREES / 2) * Mathf.Deg2Rad), .05f , Mathf.Cos((-transform.rotation.eulerAngles.y + (((float)i / NUM_OF_RAYS) * CONE_DEGREES) - CONE_DEGREES / 2) * Mathf.Deg2Rad)); RaycastHit hit; Player potentialPlayer; //Shooting out a raycast in that direction bool detected = Physics.Raycast(new Vector3(transform.position.x, transform.position.y + 0.2f, transform.position.z), rayDir, out hit, VISION_DISTANCE / 2, layerMask); if (detected) { if (hit.collider.gameObject.TryGetComponent <Player>(out potentialPlayer)) { //SEEK PLAYER HIT //If we find the player we go into chase mode and adjust our speed meshAgent.SetDestination(potentialPlayer.transform.position); meshAgent.speed = Mathf.Lerp(meshAgent.speed, MAX_SPEED, .005f); currentState = enemyAiState.CHASE; return(true); } } } return(false); }