public IEnumerator GuardAttack() { while (CStates == Guard_State.Attack) { //whilst close enough attack the player, if still has lineofsight but too far to attack back to chase, If lineofsight lost go to suspicious. detectionLight.color = Color.black; GuardNav.SetDestination(LastPos); while (GuardNav.pathPending) { yield return(null); } if (GuardNav.remainingDistance < 10f) { GuardNav.isStopped = true; canAttack = true; anim.SetBool("closeAttack", true); } if (GuardNav.remainingDistance > 10f) { CurrentState = Guard_State.Chase; } else if (!ClearLineOfSight) { CurrentState = Guard_State.Suspicious; yield break; } yield return(null); } }
void PatrolRoute() { if (canSee || canChase) { SetDestination(); } if (isTravelling && guardAgent.remainingDistance <= 0.5f) { isTravelling = false; waiting = true; waitTimer = 0; totalWaitTime = Random.Range(minWait, maxWait); } if (waiting) { baseStates = Guard_State.Idle; waitTimer += Time.deltaTime; if (waitTimer >= totalWaitTime) { baseStates = Guard_State.Patrol; waiting = false; currentPatrolPoint = (currentPatrolPoint + 1) % patrol.Count; SetDestination(); } if (canSee && canChase) { waiting = false; } } }
public IEnumerator GuardPatrol() { while (CStates == Guard_State.Patrol) { detectionLight.color = Color.yellow; GuardNav.speed = patrolSpeed; isTravelling = true; waiting = false; isSuspicious = false; GuardNav.isStopped = false; anim.SetBool("isWaiting", false); anim.SetBool("isChasing", false); anim.SetBool("isPatrolling", true); //Sets target to the next waypoint in the list Vector3 WaypointTarget = patrol[currentPatrolPoint].transform.position; GuardNav.SetDestination(WaypointTarget); //path is null is it wasn't completed yet, fixes the waypoint skip problem while (GuardNav.pathPending) { yield return(null); } //is close enought to the target waypoint start waiting if (isTravelling && GuardNav.remainingDistance <= 0.5f) { isTravelling = false; waiting = true; } //waits at waypoint for random ammount of seconds if (waiting) { anim.SetBool("isPatrolling", false); anim.SetBool("isWaiting", true); waitTimer += Time.deltaTime; GuardNav.isStopped = true; if (waitTimer >= totalWaitTime) { //when the timer reaches the max timer take current target then add 1, sets next waypoint as destination GuardNav.isStopped = false; waiting = false; currentPatrolPoint = (currentPatrolPoint + 1) % patrol.Count; GuardNav.SetDestination(WaypointTarget); } } else if (!waiting) { waitTimer = 0; totalWaitTime = Random.Range(minWait, maxWait); } if (ClearLineOfSight) { //if guard see's player chnage to chase state CurrentState = Guard_State.Chase; yield break; } yield return(null); } }
public void Awake() { anim = GetComponent <Animator>(); currentPatrolPoint = 0; guardAgent = this.GetComponent <NavMeshAgent>(); baseStates = Guard_AI.Guard_State.Patrol; anim.SetBool("isPatrolling", true); }
//takes the players position and the npc position, check is the player is wintin 30 of the enemy or 60 angle void DetectPlayer() { Vector3 direction = playerTarget.position - this.transform.position; direction.y = 0; if (Vector3.Distance(playerTarget.position, this.transform.position) < FOVDist && FOVAngle < 75) { baseStates = Guard_State.Chase; detectCounter += Time.deltaTime; if (detectCounter >= maxDetectCount) { canChase = true; isTravelling = false; baseStates = Guard_State.Chase; detectCounter = 0; guardAgent.destination = playerTarget.position; } else if (Vector3.Distance(playerTarget.position, this.transform.position) < 20 && FOVAngle < 20) { detectCounter = 0; canChase = true; isTravelling = false; baseStates = Guard_State.Chase; } if (!canSee) { detectCounter = 0; } } if (Vector3.Distance(playerTarget.position, this.transform.position) > MaxViewRange && !isTravelling && !waiting) { canSee = false; canChase = false; isTravelling = true; baseStates = Guard_State.Patrol; PatrolRoute(); } if (Vector3.Distance(playerTarget.position, this.transform.position) <= AttackDist) { canAttack = true; canChase = false; baseStates = Guard_State.Attack; anim.SetBool("closeAttack", true); } else { anim.SetBool("closeAttack", false); canAttack = false; } }
// setting the next wayppoint destination private void SetDestination() { if (canSee || canChase) { guardAgent.destination = playerTarget.position; baseStates = Guard_State.Chase; } if (!canSee) { Vector3 targetPoint = patrol[currentPatrolPoint].transform.position; guardAgent.SetDestination(targetPoint); isTravelling = true; } }
public IEnumerator GuardChase() { while (CStates == Guard_State.Chase) { detectionLight.color = Color.red; GuardNav.speed = chaseSpeed; GuardNav.isStopped = false; beingDetected = false; waiting = false; canAttack = false; isTravelling = false; isSuspicious = false; anim.SetBool("isChasing", true); anim.SetBool("isWaiting", false); anim.SetBool("isPatrolling", false); anim.SetBool("closeAttack", false); //sets to dest to lastknownpos GuardNav.SetDestination(LastPos); while (GuardNav.pathPending) { yield return(null); } if (ClearLineOfSight) { detectionTimer = 0; } if (!ClearLineOfSight) { CurrentState = Guard_State.Suspicious; } //if the guard is close enought to attack stop the nav agent moving then switch to attack, if lost sight go to suspicious if (GuardNav.remainingDistance <= 10f) { GuardNav.isStopped = true; { CurrentState = Guard_State.Attack; yield break; } } yield return(null); } }
public IEnumerator GuardSus() { while (CStates == Guard_State.Suspicious) { //walks to lastkknown pos, then stops and waits for 5 seconds, if player detected during the 5 seconds back to chase else return to patrol. detectionLight.color = Color.blue; canAttack = false; isTravelling = true; isSuspicious = true; anim.SetBool("closeAttack", false); GuardNav.SetDestination(LastPos); while (GuardNav.pathPending) { yield return(null); } if (isTravelling && GuardNav.remainingDistance <= 0.5f) { waiting = true; isTravelling = false; } if (waiting) { anim.SetBool("isWaiting", true); anim.SetBool("isChasing", false); waitTimer += Time.deltaTime; GuardNav.isStopped = true; if (waitTimer >= totalWaitTime) { CurrentState = Guard_State.Patrol; } } else if (!waiting) { waitTimer = 0; totalWaitTime = 5; } if (ClearLineOfSight) { CurrentState = Guard_State.Chase; yield break; } yield return(null); } }
public void OnTriggerEnter(Collider other) { canHear = true; baseStates = Guard_State.Chase; guardAgent.destination = playerTarget.position; }
private void Start() { CurrentState = Guard_State.Patrol; currentPatrolPoint = 0; }
//simply ontrigger if the player is making too much noise the collider radius will increase causing ontrigger enter. public void OnTriggerEnter(Collider Sound) { canHear = true; CurrentState = Guard_State.Chase; }