public bool UpdateMovement() { // Search for a new path if path is not found if (m_searchNewTarget || m_newTargetPosition == null) { //Debug.Log("Have no path or target, finds new target and path"); m_newTargetPosition = GetTargetPosition(); m_searchNewTarget = false; } Debug.DrawRay(transform.position, transform.forward * 5.0f, Color.green); // Check if seeker has reached target position if (m_movement.MoveToPos(m_newTargetPosition)) { //Debug.Log("Searching for a new position since target is reached"); m_searchNewTarget = true; return(true); } return(false); }
// Can have a day and night behaviour and state void Update() { if (m_discovered || m_disabled) { return; } DisplayCurrentState(); m_fieldOfView.SetLookDir(transform.forward); bool hasVisibleTarget = m_fieldOfView.FindVisibleTargets(); // Move according to state // If target is found, hunt it if (hasVisibleTarget) { //Debug.Log("Found target!"); // Abort current pathfinding and change target m_currentState = (int)State.Hunting; m_noise = 0.0f; m_targetPos = m_fieldOfView.m_visibleTargets[0].position; m_targetVisibleTimer += Time.deltaTime; } //else if(currentState == (int)State.Hunt){ // // Look at position where target was last seen // currentState = (int)State.LocalPatrol; //} else { m_targetVisibleTimer -= Time.deltaTime; } m_targetVisibleTimer = Mathf.Clamp(m_targetVisibleTimer, 0, m_timeToSpotTarget); m_spotlight.color = Color.Lerp(m_originalSpotlightColor, Color.red, m_targetVisibleTimer / m_timeToSpotTarget); m_movementDir = m_movement.GetMoveDir(); switch (m_currentState) { case (int)State.Idling: // Can the seeker se the player whilst idle? break; case (int)State.Patrolling: // Move and search the area bool isPatrolling = GetComponent <Patrolling>().UpdateMovement(); // Update movement if (isPatrolling) { m_currentState = (int)State.Searching; m_isSearchingNearby = true; // TODO: Fix so that they search again if a path is not found at all } // Set viewing direction if (m_movementDir != Vector3.zero) { transform.rotation = Quaternion.LookRotation(m_movementDir); } break; case (int)State.Searching: // Look around where the seeker is standing // Check if you have finished looking around in the nearby are m_isSearchingNearby = GetComponent <Searching>().Run(); // Look Around if (m_isSearchingNearby) { // Start searching for the next place to look at m_currentState = (int)State.Patrolling; } break; case (int)State.Hunting: // TODO: If player found start searching in the area around it with distortion over time // TODO: After a certain amount of time it goes back to normal and searches everywhere // TODO: If the player is caught ==> GAME OVER (Restart) // TODO: If the player is seen but not caught ==> Search the last seen position // If target is found, move towards it using pathfinding if (hasVisibleTarget) { m_lastSeenTarget = m_fieldOfView.m_visibleTargets[0].position; m_movement.MoveToPos(m_lastSeenTarget); // Set viewing direction transform.rotation = Quaternion.LookRotation(m_movement.GetMoveDir()); Debug.Log("ENEMY: Distance seeker to target = " + Vector3.Distance(transform.position, m_lastSeenTarget)); // Check if target is within reach and if the seeker has grasped the target if (Vector3.Distance(transform.position, m_lastSeenTarget) < 2.0f) { Debug.Log("Caught intruder!"); m_discovered = true; m_player.SetDiscovered(m_discovered); GameManager.manager.Discovered(); } // Check if target has been visible for a long while (= Game Over) } else if (!hasVisibleTarget) // has not visible target { m_currentState = (int)State.LocalPatrolling; } break; case (int)State.LocalPatrolling: if (!hasVisibleTarget) { if (m_noise < m_EPSILON) { m_lastSeenTarget = m_targetPos; } // Noise that is larger over time m_noise += Time.deltaTime; // If too much time has passed, the target is lost if (m_noise > 0.8f) { // Start searching the area m_currentState = (int)State.Searching; m_noise = 0.0f; } // Move towards target position with noise if (m_movement.MoveToPos(m_lastSeenTarget)) { float newX = Random.Range(m_targetPos.x - m_noise, m_targetPos.x + m_noise); float newZ = Random.Range(m_targetPos.z - m_noise, m_targetPos.z + m_noise); // Set new target with larger noise m_lastSeenTarget = new Vector3(newX, m_targetPos.y, newZ); } // Set viewing direction transform.rotation = Quaternion.LookRotation(m_movement.GetMoveDir()); } else { m_currentState = (int)State.Hunting; m_noise = 0.0f; } break; } }