private void Bird_DisableDisplayOnStateChange(Bird sender, BirdAIState currentState, BirdAIState oldState) { sender.StateChanged -= Bird_DisableDisplayOnStateChange; birdUI.CloseAll(); if (currentState == BirdAIState.Sitting) { StartCoroutine(BirdRotationFix()); } }
/// <summary> /// The bird will try to land at the specified location. /// </summary> /// <param name="location"></param> public void LandAtLocation(Vector3 location) { BirdAIState oldState = currentState; flightGoal = location; currentState = BirdAIState.Landing; characterController.Soar(); wingsSource.volume = 1; characterController.ForwardSpeedSet(forwardSpeed); if (StateChanged != null) { StateChanged(this, currentState, oldState); } }
/// <summary> /// The bird will fly to a specified location and circle around it once it /// arrives. /// </summary> /// <param name="direction"></param> public void FlyToLocation(Vector3 location, bool stopWhenDone) { stopAtDestination = stopWhenDone; BirdAIState oldState = currentState; flightGoal = location; currentState = BirdAIState.FlyingToGoal; characterController.Soar(); wingsSource.volume = 1; characterController.ForwardSpeedSet(forwardSpeed); if (StateChanged != null) { StateChanged(this, currentState, oldState); } }
// Update is called once per frame void Update() { if (chirpTimer > 0) { chirpTimer -= Time.deltaTime; if (chirpTimer < 0) { Chirp(); GetNewChirpTime(); } } Vector3 directionToGoal; switch (currentState) { case BirdAIState.Sitting: break; case BirdAIState.Floating: break; case BirdAIState.Landing: if (Vector3.Distance(transform.position, flightGoal) < 0.8) { forwardSpeed = Mathf.Lerp(0.3f, 0.1f, Mathf.InverseLerp(0, Vector3.Distance(transform.position, flightGoal), 0.8f)); characterController.forwardSpeed = forwardSpeed; } if (Vector3.Distance(transform.position, flightGoal) < 0.1f) { characterController.Landing(); currentState = BirdAIState.Sitting; wingsSource.volume = 0f; if (StateChanged != null) { StateChanged(this, BirdAIState.Sitting, BirdAIState.FlyingToGoal); } Invoke("DebugLanding", Time.deltaTime); break; } directionToGoal = (flightGoal - transform.position).normalized; transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(directionToGoal, Vector3.up), turnSpeed * Time.deltaTime); break; case BirdAIState.FlyingToGoal: if (stopAtDestination) { if (Vector3.Distance(transform.position, flightGoal) < 0.8) { forwardSpeed = Mathf.Lerp(0.3f, 0.1f, Mathf.InverseLerp(0, Vector3.Distance(transform.position, flightGoal), 0.8f)); characterController.forwardSpeed = forwardSpeed; } if (Vector3.Distance(transform.position, flightGoal) < 0.1f) { currentState = BirdAIState.Floating; wingsSource.volume = 1f; characterController.forwardSpeed = 0f; if (StateChanged != null) { StateChanged(this, BirdAIState.Floating, BirdAIState.FlyingToGoal); } break; } } directionToGoal = (flightGoal - transform.position).normalized; transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(directionToGoal, Vector3.up), turnSpeed * Time.deltaTime); break; default: break; } ProcessDebugCommands(); }