void Start() { attackScript = GetComponent<InterfaceZombieAttack>(); spitZombieState = stateSZ.sensedPlayerNearby; newRoamingPosition = Vector3.zero; }
// Start is called before the first frame update void Start() { attackScript = GetComponent <InterfaceZombieAttack>(); spitZombieState = stateSZ.sensedPlayerNearby; hasReachedPatrolPoint = false; if (nextPatrolPoint == null) { nextPatrolPoint = patrolPoints[0]; } }
// Update is called once per frame void Update() { switch (spitZombieState) { case stateSZ.sensedPlayerNearby: spitZombieState = stateSZ.chasePlayerUntilInLos; break; case stateSZ.attacking: if (isPlayerInsideAttackRange == true) { attackScript.IzombieAttack(); zombieNavmeshAgent.isStopped = true; } else { spitZombieState = stateSZ.chasePlayerUntilInLos; zombieNavmeshAgent.isStopped = false; } break; case stateSZ.chasePlayerUntilInLos: if (isPlayerWithinLos() == true || isPlayerSensedNearby == true) // los should work only if the angle is in the front of zommbie or If very close { zombieNavmeshAgent.SetDestination(playerTransform.position); } else { // go to Roaming player state, after going to the last spotted position if (zombieNavmeshAgent.remainingDistance <= zombieNavmeshAgent.stoppingDistance) { if (!zombieNavmeshAgent.hasPath || zombieNavmeshAgent.velocity.sqrMagnitude == 0f) { spitZombieState = stateSZ.roaming; } } } break; case stateSZ.huntingPlayer: // this state is kinda a bit whacky : 1. Go to last spotted player Position 2. Patrol in 4 directions for sometime 3. Back to roaming state break; case stateSZ.roaming: if (hasReachedPatrolPoint == true) { int rand = Random.Range(0, patrolPoints.Length - 1); nextPatrolPoint = patrolPoints[rand]; NavMeshHit hit; NavMesh.SamplePosition(nextPatrolPoint.position, out hit, targetSampleNavmeshRadiusAllowed, 1); nextPatrolPoint.position = hit.position; hasReachedPatrolPoint = false; } else { zombieNavmeshAgent.SetDestination(nextPatrolPoint.position); if (!zombieNavmeshAgent.pathPending) { if (zombieNavmeshAgent.remainingDistance <= zombieNavmeshAgent.stoppingDistance) { if (!zombieNavmeshAgent.hasPath || zombieNavmeshAgent.velocity.sqrMagnitude == 0f) { hasReachedPatrolPoint = true; } } } } if (isPlayerWithinLos() == true) { spitZombieState = stateSZ.chasePlayerUntilInLos; } break; } }
void Update() { switch(spitZombieState) { case stateSZ.sensedPlayerNearby: enemyZombAgent.SetDestination(playerTransform.position); if(fovScript.playerInFovSpotted==true) { spitZombieState = stateSZ.attacking; enemyZombAgent.isStopped = true; } break; case stateSZ.attacking: if (fovScript.playerInFovSpotted == true) { attackScript.IzombieAttack(); } else { enemyZombAgent.isStopped = false; // this is dangerous as we don't know where the NavMeshAgent destination is set to if (checkPlayerInLineOfSight()==true){ spitZombieState = stateSZ.chasePlayerUntilInLos; }else { spitZombieState = stateSZ.huntingPlayer; } } break; case stateSZ.chasePlayerUntilInLos: if (checkPlayerInLineOfSight() == true) { enemyZombAgent.SetDestination(playerTransform.position); if (fovScript.playerInFovSpotted == true) { spitZombieState = stateSZ.attacking; enemyZombAgent.isStopped = true; } } else { lastSpottedPlayerPosition = playerTransform.position; spitZombieState = stateSZ.huntingPlayer; enemyZombAgent.SetDestination(lastSpottedPlayerPosition); } break; case stateSZ.huntingPlayer: // this state is kinda a bit whacky : 1. Go to last spotted player Position 2. Patrol in 4 directions for sometime 3. Back to roaming state if (enemyTransform.position == lastSpottedPlayerPosition) { if (huntingTimer >= 0) { huntingTimer -= Time.deltaTime; Vector3 randomDirection = Random.insideUnitSphere * huntRadius; randomDirection = new Vector3(randomDirection.x, 0f, randomDirection.z); randomDirection += transform.position; NavMeshHit hit; NavMesh.SamplePosition(randomDirection, out hit, huntRadius, 1); lastSpottedPlayerPosition = hit.position; } else { huntingTimer = 10.0f; spitZombieState = stateSZ.roaming; } } else { enemyZombAgent.SetDestination(lastSpottedPlayerPosition); } if (checkPlayerInLineOfSight() == true) { huntingTimer = 10.0f; spitZombieState = stateSZ.chasePlayerUntilInLos; } break; case stateSZ.roaming: if(waitTime>=3) { if(newRoamingPosition== Vector3.zero) { newRoamingPosition = getRandomRoamingPosition(); } else if (enemyTransform.position!=newRoamingPosition) { enemyZombAgent.velocity = newRoamingPosition - enemyTransform.position; enemyTransform.forward = enemyZombAgent.velocity.normalized; } else if(enemyTransform.position == newRoamingPosition) { newRoamingPosition = Vector3.zero; waitTime = 0; } }else { waitTime += Time.deltaTime; } break; } }