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; // } //} } }
public void STATE_Update(AgentController agent, StateMachine_GPATROL stateMachine, float deltaTime) { //Reset the path and velocity agent.navAgent.ResetPath(); agent.navAgent.velocity = Vector3.zero; //Store stateMachine so i can access it in a method(could probably just make the method accept a statemachine pointer) currentStateMachine = stateMachine; //Rotating Agent to face Will - LookAt does snap towards Will maybe lerp instead Vector3 v = agent.target.transform.position - agent.transform.position; //Remove the x and z components from the vector v.x = 0.0f; v.z = 0.0f; agent.transform.LookAt(agent.target.transform.position - v); //Rotate around the Y axis agent.transform.Rotate(0, agent.target.transform.rotation.y, 0); //End Rotation //SHOOT MECHANIC timer += Time.deltaTime; //minus the time //When player first enters the light, fire straight away before starting the charge timer if (initialEnter == true) { if (internalTimer < agent.bulletShootTime) { Vector3 target = agent.target.transform.position;// + new Vector3(0, 0.4f, 0); //target.x += UnityEngine.Random.Range(-agent.gunAccuracy, agent.gunAccuracy); //target.y += UnityEngine.Random.Range(-agent.gunAccuracy, agent.gunAccuracy); //target.z += UnityEngine.Random.Range(-agent.gunAccuracy, agent.gunAccuracy); protonBeam.target = target; protonBeam.fire = true; pBeam.SetActive(true); agent.anim.SetBool("stream", true); //Play stream animation PlayShootSound(true); internalTimer += Time.deltaTime; CheckTorch(); } else //done shooting for x amount of time so begin the recharge timer { timer = 0; internalTimer = 0; protonBeam.fire = false; pBeam.SetActive(false); agent.anim.SetBool("stream", false); //Stop stream animation PlayShootSound(false); initialEnter = false; } } else { if (timer > agent.bulletRecharge) //Once the timer is greater than the recharge wait time, fire for set amount of time { if (internalTimer < agent.bulletShootTime) { Vector3 target = agent.target.transform.position;// + new Vector3(0, 0.4f, 0); //target.x += UnityEngine.Random.Range(-agent.gunAccuracy, agent.gunAccuracy); //target.y += UnityEngine.Random.Range(-agent.gunAccuracy, agent.gunAccuracy); //target.z += UnityEngine.Random.Range(-agent.gunAccuracy, agent.gunAccuracy); protonBeam.target = target; protonBeam.fire = true; pBeam.SetActive(true); agent.anim.SetBool("stream", true); //Play stream animation PlayShootSound(true); internalTimer += Time.deltaTime; CheckTorch(); } else //done shooting for x amount of time so begin the recharge timer { timer = 0; internalTimer = 0; protonBeam.fire = false; pBeam.SetActive(false); agent.anim.SetBool("stream", false); //Stop stream animation } } } //End Else CheckTorch(); }