//State changing away from PURSUE void CheckTorch() { if (currentAgent.isWillInTorchLight() == false) { protonBeam.fire = false; pBeam.SetActive(false); currentAgent.anim.SetBool("shoot", false); timer = 0; //Change colour of torch cone to green Color myColor = new Color(0.03f, 0.16f, 0.06f); currentAgent.torchCone.GetComponent <Renderer>().material.SetColor("_TintColor", myColor); //Will is now out of sight - go to where you last saw Will currentAgent.AddPointOfInterest(currentAgent.target.transform.position); //Check for points of interest if (currentAgent.CheckForPOI() == true) { currentStateMachine.ChangeState(currentAgent, new GPATROL_Alerted()); } else { //Default to wander if all else fails currentStateMachine.ChangeState(currentAgent, new GPATROL_Wander()); } //More state changing to come } }
public void STATE_Update(AgentController agent, StateMachine_GPATROL stateMachine, float deltaTime) { for (int i = 0; i < agent.GetPOICount(); i++) { //double distance = System.Math.Sqrt((agent.transform.position.x - agent.GetPositionFromPOI(i).x) * (agent.transform.position.x - agent.GetPositionFromPOI(i).x) // + (agent.transform.position.y - agent.GetPositionFromPOI(i).y) * (agent.transform.position.y - agent.GetPositionFromPOI(i).y) // + (agent.transform.position.z - agent.GetPositionFromPOI(i).z) * (agent.transform.position.z - agent.GetPositionFromPOI(i).z)); //some code to ignore broken paths (the agent cant reach the poi so dont send them there) NavMeshPath path = new NavMeshPath(); agent.navAgent.CalculatePath(agent.GetPositionFromPOI(i), path); if (path.status == NavMeshPathStatus.PathPartial) { //Debug.Log("DodgyPath"); agent.RemovePOI(i); break; } //path good set destination agent.navAgent.SetDestination(agent.GetPositionFromPOI(i)); if ((agent.transform.position - agent.navAgent.destination).magnitude <= agent.torch.range / 2)//this code gets them close enough to spot will if he is around { agent.RemovePOI(i); if (agent.isWillInTorchLight() == true) { stateMachine.ChangeState(agent, new GPATROL_Pursue()); break; } } else { if (agent.isWillInTorchLight() == true) { stateMachine.ChangeState(agent, new GPATROL_Pursue()); } break; } } //Check if Will is in torchlight if (agent.isWillInTorchLight() == true) { stateMachine.ChangeState(agent, new GPATROL_Pursue()); } else { //Check if the PointsOfInterest list is empty - if it is then default to Wander if (agent.CheckForPOI() == false) { stateMachine.ChangeState(agent, new GPATROL_Wander()); } } }
//Possibly use this later for now unused //Agent_Blackboard blackboard = Agent_Blackboard.Instance; // Use this for initialization void Start() { navAgent = GetComponent <NavMeshAgent>(); //Find the navMesh component navAgent.speed = movementSpeed; //Set the agent speed through the AgentController Script target = GameObject.FindGameObjectWithTag("Player"); anim = GetComponent <Animator>(); torch = transform.GetChild(1).GetComponent <Light>(); //Gets the lightsource the agent is using colliderSphere = GetComponent <SphereCollider>(); colliderSphere.radius = audioSearch; //Line Renderer for debugging the wander radius line = GetComponent <LineRenderer>(); line.enabled = false; if (ShowRadius) //Enables the component if the inspector flag is true { line.enabled = true; } //If the text canvas is not in this position - will throw an error txtState = transform.GetChild(0).GetChild(0).GetComponent <Text>(); //Accesses the txtState Text object in the heirachy attached to this Agent //Setup shoot sound shootSound = FMODUnity.RuntimeManager.CreateInstance(shootSoundRef); //StateMachine creation - Setting Default State - Will inherit this from inspector m_stateMachine = new StateMachine_GPATROL(); m_stateMachine.ChangeState(this, new GPATROL_Wander()); #if UNITY_EDITOR { ShowState = true; } #else { ShowState = false; } #endif if (ShowState == true) { txtState.enabled = true; } else { txtState.enabled = false; } pointsOfInterest = new List <Vector3>(); }
public void STATE_Update(AgentController agent, StateMachine_GPATROL stateMachine, float deltaTime) { scared = false; agent.anim.SetBool("scared", scared); //Set the animation controller //Get the direction dirAwayFromWill = currentAgent.transform.position - currentAgent.target.transform.position; //If will is not close to the agent then don't flee if (dirAwayFromWill.magnitude > scaredRadius) { currentAgent.navAgent.velocity = Vector3.zero; //Stop the agent from moving //Animation settings scared = true; agent.anim.SetBool("scared", scared); //Debug.Log(timer); timer -= Time.deltaTime; //minus the time if (timer <= 0f) { //Reset the animation settings for next state scared = false; agent.anim.SetBool("scared", scared); stateMachine.ChangeState(agent, new GPATROL_Wander()); //Change back to wander timer = 3.0f; //Reset timer } } else //The player has moved close to the agent so move the agent { timer = 3.0f; //Reset the timer if (agent.navAgent.hasPath == false) { Vector3 randDirection = UnityEngine.Random.onUnitSphere * currentAgent.wanderRadius; randDirection += currentAgent.transform.position; //Update direction based on Agent's current position NavMeshHit navHit; //Stores the result of a NavMesh query NavMesh.SamplePosition(randDirection, out navHit, currentAgent.wanderRadius, -1); agent.navAgent.SetDestination(navHit.position); } } //End else } //End update
public void STATE_Update(AgentController agent, StateMachine_GPATROL stateMachine, float deltaTime) { #region Visualize the WANDER radius if (agent.ShowRadius == true) //If the show radius flag is checked in inspector display the ring { //Set number of segments agent.line.positionCount = segments + 1; //Drawing the Circle around the agent to show its search radius for (int i = 0; i < segments + 1; i++) { int angle = (360 / segments) * i; agent.line.SetPosition(i, agent.transform.position + agent.wanderRadius * new Vector3(Mathf.Cos(Mathf.Deg2Rad * angle), 0, Mathf.Sin(Mathf.Deg2Rad * angle))); } } #endregion if (agent.enableWander == true && Time.timeScale != 0) { //If the agent doesnt have an INITIAL path then pick a new one //This also picks a new one once the agent reaches the end of a path if (agent.navAgent.hasPath == false) { PickNewWanderPoint(); } if (agent.navAgent.hasPath && Vector3.Distance(agent.transform.position, agent.navAgent.destination) <= 0.5) { agent.navAgent.ResetPath(); } //Check if Will is in torchlight if (agent.isWillInTorchLight() == true) { stateMachine.ChangeState(agent, new GPATROL_Pursue()); } else { //Check if any points of interest has been registered if (agent.CheckForPOI() == true) { stateMachine.ChangeState(agent, new GPATROL_Alerted()); } } //if (agent.transform.position == agent.navAgent.pathEndPosition) //{ // waiting = true; // timer += deltaTime; // if (timer >= 1) // { // waiting = false; // timer = 0; // } //} } }