// We have returned to where we were previously patrolling public void OnArriveAtPatrolPoint() { // Continue patrolling State_Patrol state = new State_Patrol(this); state.SetAnimator(anim); state.SetPatrolPoints(checkpoints, ticksToReach); state.SetNavAgent(navAgent); state.currentCheckpoint = lastCheckpoint; stateMachine.SetState(state); }
public float attackRange = 1.2f; // How far ahead we can attack // Start is called before the first frame update new void Start() { base.Start(); // Get references anim = GetComponent <Animator>(); if (anim == null) { Debug.Log("Animator could not be found"); } visionRange = GetComponent <ConeCollider>(); if (visionRange == null) { Debug.Log("Could not find cone collider"); } attackCollider = GetComponent <CapsuleCollider>(); if (attackCollider == null) { Debug.Log("Could not find capsule collider"); } navAgent = GetComponent <NavMeshAgent>(); if (navAgent == null) { Debug.Log("Could not find nav mesh agent"); } // Set the attack capsule's range attackCollider.radius = attackRange; attackCollider.isTrigger = true; // Currently not attacking attackCollider.enabled = false; visionRange.enabled = true; // Init list of checkpoints checkpoints = new List <Vector3>(); ticksToReach = new List <int>(); // Get the list of checkpoints if (transform.Find("Movepoints") != null) { Transform movingPoints = transform.Find("Movepoints").gameObject.transform; int numChildren = movingPoints.childCount; for (int i = 0; i < numChildren; ++i) { // Get each checkpoint GameObject goalPoint = movingPoints.GetChild(i).gameObject; Checkpoint cp = goalPoint.GetComponent(typeof(Checkpoint)) as Checkpoint; if (cp != null) { checkpoints.Add(goalPoint.transform.position); ticksToReach.Add(cp.GetMoveTime()); } } } // Init state machine stateMachine = new FSM(); State_Patrol state = new State_Patrol(this); state.SetAnimator(anim); state.SetNavAgent(navAgent); state.SetPatrolPoints(checkpoints, ticksToReach); state.currentCheckpoint = -1; stateMachine.SetState(state); }
void Start() { hpDesire = 0; hpStatus = 0; weaponDesire = 0; powerful = 0; agent = GetComponentInParent <Agent>(); vehicle = GetComponentInParent <Vehicle>(); GameObject[] tempallies = GameObject.FindGameObjectsWithTag("Enemy_AI"); for (int i = 0; i < tempallies.Length; i++) { allies.Add(tempallies[i]); } for (int i = 0; i < allies.Count; i++) { if (allies[i] == agent.gameObject) { allies.Remove(allies[i]); } } if (allies.Count > 0) { hasBeenFoundDead.Capacity = allies.Count; Debug.Log(allies.Count); for (int i = 0; i < hasBeenFoundDead.Capacity; i++) { hasBeenFoundDead.Add(false); } } if (isRoaming) { wanderDesire = 0.1f; postDesire = 0f; State_Patrol patrolState = new State_Patrol(); agent.ChangeState(patrolState); } else { wanderDesire = 0f; postDesire = 0.1f; Debug.Log("is it here?"); State_Idle idleState = new State_Idle(); agent.ChangeState(idleState); } switch (Personality) { case 0: //the cowardly unit fightModifier = 0.7f; runModifier = 1.3f; break; case 1: //the regular unit that does not have edited values fightModifier = 1f; runModifier = 1f; break; case 2: fightModifier = 1.5f; runModifier = 0.7f; break; } }
void Update() { if (agent.gameObject.GetComponent <Perception>().MemoryMap.ContainsKey(player)) { timeSinceSeen = DateTime.Now - agent.gameObject.GetComponent <Perception>().MemoryMap[player].TimeLastSensed; } else { powerful = 0; //if the player has never been seen then the power is set to 0 by default to stop it from wanting to fight something it doesn't know exists } for (int i = 0; i < allies.Count; i++) { if (hasBeenFoundDead[i] == false) { if (agent.gameObject.GetComponent <Perception>().MemoryMap.ContainsKey(allies[i])) { if (agent.gameObject.GetComponent <Perception>().MemoryMap[allies[i]].targetDead == true) { deadAllyCounter += 0.1f; hasBeenFoundDead[i] = true; } } } } List <float> desireList = new List <float>(); desireList.Add(weaponDesire); desireList.Add(hpDesire); desireList.Add(powerful); desireList.Add(wanderDesire); desireList.Add(postDesire); if (timeSinceSeen.Seconds > timeToForget) { powerful = 0; //if the unit has not seen the player in 50 seconds the power will be set to 0 } if (desireList.Max() == powerful) { if (agent.gameObject.GetComponent <Perception>().MemoryMap != null && agent.gameObject.GetComponent <Perception>().MemoryMap.ContainsKey(player) && agent.gameObject.GetComponent <Perception>().MemoryMap[player].WithinFoV == false) { //if the powerful state has the highest desirability and the AI cannot see the player, the AI will search for the player if (Search_State_On == false) { State_Search_Player pSearchState = new State_Search_Player(); ResetBools(); Search_State_On = true; agent.ChangeState(pSearchState); } } else if (agent.gameObject.GetComponent <Perception>().MemoryMap != null && agent.gameObject.GetComponent <Perception>().MemoryMap.ContainsKey(player) && agent.gameObject.GetComponent <Perception>().MemoryMap[player].WithinFoV == true) { if (player.GetComponent <Player_Gun>().playerPower <= powerful) //checks to see how strong the player is compared to the unit - will make them flee if they feel weaker in comparison { if (Vector3.Distance(agent.transform.position, player.transform.position) > agent.fireRange) { if (Search_State_On == false) { State_Search_Player pSearchState = new State_Search_Player(); ResetBools(); Search_State_On = true; agent.ChangeState(pSearchState); } } else { if (Fight_State_On == false) { State_Fight_Player fightState = new State_Fight_Player(); ResetBools(); Fight_State_On = true; agent.ChangeState(fightState); } } } else if (player.GetComponent <Player_Gun>().playerPower > powerful) { Debug.Log("Epic"); if (Flee_State_On == false) { State_Flee fleeState = new State_Flee(); ResetBools(); Flee_State_On = true; agent.ChangeState(fleeState); } } } } if (desireList.Max() == weaponDesire) { //if the desire to find ammo is the highest it will try to find ammo if (Ammo_State_On == false) { State_Search_Ammo ammoState = new State_Search_Ammo(); ammoState.hasCreatedPath = false; ResetBools(); Ammo_State_On = true; agent.ChangeState(ammoState); } } if (desireList.Max() == hpDesire) { //if the desire to find health is the highest it will try to find health if (HP_State_On == false) { State_Search_Health healthState = new State_Search_Health(); healthState.hasCreatedHealthPath = false; ResetBools(); HP_State_On = true; agent.ChangeState(healthState); } } if (desireList.Max() == wanderDesire) { State_Patrol patrolState = new State_Patrol(); agent.ChangeState(patrolState); } if (desireList.Max() == postDesire) { if (Vector3.Distance(transform.position, postLocation) < 5f) { agent.SB.IsSeekOn = false; State_Idle idleState = new State_Idle(); agent.ChangeState(idleState); } else { if (Post_State_On == false) { State_Return_To_Post postState = new State_Return_To_Post(); ResetBools(); Post_State_On = true; agent.ChangeState(postState); } } } }