/// <summary> /// Kills the rat /// </summary> public void Killed() { // Change state currentState = ratStates.dead; }
// Update is called once per frame (Should be fixed) void Update() { bool playerInSight = CanSeePlayer(); if (playerInSight) { Debug.DrawRay(transform.position, GameObject.Find("Player").transform.position - transform.position, Color.red); } // state machine switch (currentState) { case ratStates.patrol: if (pathContainer != null) { // if the agent isn't moving, get them moving towards the target if (thisAgent.isStopped) { thisAgent.isStopped = false; } Vector3 distance = transform.position - agentTarget; Vector2 XZdistance = new Vector2(distance.x, distance.z); // if we're already at that point if (XZdistance.magnitude <= 0.1f) { // target the next node targetNode++; // if the next node does not exist if (targetNode >= path.Count) { // target the first node targetNode = 0; } } // set the target to the current node agentTarget = GetTargetNodePosition(); } else { currentState = ratStates.idle; } // if we spot the player while patrolling, start searching for the player if (playerInSight) { currentState = ratStates.curious; timeSpottingPlayer = 0.0f; timeSinceSawPlayer = 0.0f; } break; case ratStates.curious: //stop the agent if it's not stopped already if (!thisAgent.isStopped) { thisAgent.isStopped = true; } // turn towards the player RotateTowardsPosXZ(GameObject.Find("Player").transform.position); // increment how long we've been spotting the player for if (playerInSight) { timeSpottingPlayer += Time.deltaTime; } else { // decrement timeSpottingPlayer -= Time.deltaTime; // if the time has reached 0 if (timeSpottingPlayer <= 0.0f) { timeSpottingPlayer = 0.0f; currentState = ratStates.patrol; } } if (timeSpottingPlayer >= spotChaseTime) { currentState = ratStates.chase; agentTarget = GameObject.Find("Player").transform.position; } //if () break; case ratStates.chase: if (thisAgent.isStopped) { thisAgent.isStopped = false; timeSinceSawPlayer = 0.0f; } agentTarget = GameObject.Find("Player").transform.position; //RotateTowardsPosXZ(GameObject.Find("Player").transform.position); if (playerInSight) { timeSinceSawPlayer = 0.0f; } else { timeSinceSawPlayer += Time.deltaTime; } if (timeSinceSawPlayer >= giveUpTime) { currentState = ratStates.patrol; timeSpottingPlayer = 0.0f; } break; case ratStates.dead: thisAgent.isStopped = true; GetComponentInChildren <Animator>().enabled = false; break; default: break; } if (thisAgent) { thisAgent.SetDestination(agentTarget); } }
// Update is called once per frame (Should be fixed) void Update() { // state machine switch (currentState) { case ratStates.patrol: if (pathContainer != null) { if (!SmoothMovementPosXZ(GetTargetNodePosition())) // if we're already at that point { // target the next node targetNode++; if (targetNode >= path.Count) // if the next node does not exist { // target the first node targetNode = 0; } } } else { currentState = ratStates.idle; } if (CanSeePlayer()) { currentState = ratStates.curious; } else { timeSpottingPlayer -= Time.deltaTime; if (timeSpottingPlayer < 0.0f) { timeSpottingPlayer = 0.0f; } } if (timeSpottingPlayer >= spotChaseTime) { currentState = ratStates.chase; } break; case ratStates.curious: RotateTowardsPosXZ(GameObject.Find("Player").transform.position); if (CanSeePlayer()) { timeSpottingPlayer += Time.deltaTime; } else { timeSpottingPlayer -= Time.deltaTime; if (timeSpottingPlayer < 0.0f) { timeSpottingPlayer = 0.0f; currentState = ratStates.patrol; } } if (timeSpottingPlayer >= spotChaseTime) { currentState = ratStates.chase; } //if () break; case ratStates.chase: if (CanSeePlayer()) { timeSinceSawPlayer = 0.0f; } else { timeSinceSawPlayer += Time.deltaTime; } if (timeSinceSawPlayer >= giveUpTime) { currentState = ratStates.patrol; } SmoothMovementPosXZ(GameObject.Find("Player").transform.position); Vector3 distance = GameObject.Find("Player").transform.position - transform.position; if (distance.magnitude > 5.0F) { currentState = ratStates.idle; } break; case ratStates.idle: if (CanSeePlayer()) { currentState = ratStates.chase; } break; default: break; } }