void Start() { sounds = GetComponent <AudioSource> (); thwompCharacterController = this.GetComponent <CharacterController> (); //Records the Thwomp's start position startPosition = this.transform.position; currentState = ThwompState.waitingToFall; timer = 0f; }
IEnumerator IDLE() { Debug.Log("Enter Idle State"); while (CurrentState == ThwompState.IDLE) { if (Vector3.Distance(Dan.transform.position, transform.position) < ActivationDistance) { CurrentState = ThwompState.CHASING; } yield return(null); } Debug.Log("Exit Idle State"); }
//This coroutine helps determine if the Thwomp has run into something //(as an alternative to OnCollisionEnter(), which was giving me (Nick) some problems) IEnumerator CheckIfMoving() { //Saves the current position of the Thwomp Vector3 currentPos = this.transform.position; //Waits a frame yield return(0); //Checks to see if the Thwomp is in the same position as it was a frame ago if (currentPos == this.transform.position) { sounds.PlayOneShot(boom, 0.5f); sounds.PlayOneShot(grunt, 1f); //Tells the Thwomp to stop moving and to prepare to rise again currentState = ThwompState.waitingToRise; } }
IEnumerator ATTACKING() { Debug.Log("Enter Attacking State"); while (CurrentState == ThwompState.ATTACKING) { var oldY = transform.position.y; var newPosition = Vector3.MoveTowards(transform.position, Dan.transform.position, ThwompSpeed * Time.deltaTime); transform.position = new Vector3(transform.position.x, newPosition.y, transform.position.z); if (Math.Abs(transform.position.x - Dan.transform.position.x) > 0.5) { CurrentState = ThwompState.CHASING; transform.position = new Vector3(transform.position.x, oldY, transform.position.z); } yield return(null); } Debug.Log("Exit Attacking State"); }
IEnumerator CHASING() { Debug.Log("Enter Chasing State"); yield return(new WaitForSeconds(ActivationDelay)); while (CurrentState == ThwompState.CHASING) { var newPosition = Vector3.MoveTowards(transform.position, Dan.transform.position, MoveSpeed * Time.deltaTime); transform.position = new Vector3(newPosition.x, transform.position.y, transform.position.z); if (Math.Abs(transform.position.x - Dan.transform.position.x) < 0.5) { CurrentState = ThwompState.ATTACKING; } yield return(null); } Debug.Log("Exit Chasing State"); }
void Update() { //STEP 1: Define ray Ray thwompDetectionRay = new Ray(transform.position, new Vector3(0, -1, 0)); //STEP 2: Declare raycast distance float rayDistance = 2f; //STEP 3: Visualize the raycast Debug.DrawRay(thwompDetectionRay.origin, thwompDetectionRay.direction * rayDistance, Color.yellow); //If the Thwomp is wating to drop if (currentState == ThwompState.waitingToFall) { //Tells the script that it's okay for the Thwomp to do damage to the player again didDamage = false; //Resets the Thwomp to it's starting position this.GetComponent <Transform> ().position = startPosition; //Ticks up the timer timer += Time.deltaTime; //Checks if it's time to drop yet if (timer >= secondsToDrop) { //Resets the timer for the next drop timer = 0f; currentState = ThwompState.falling; } } if (currentState == ThwompState.falling) { //Moves the Thwomp downward thwompCharacterController.Move(Vector3.down * dropSpeed * Time.deltaTime); Vector3 currentPos = this.transform.position; //Checks if the Thwomp is still moving or if it has run into something StartCoroutine(CheckIfMoving()); } //If the Thwomp is wating to rise if (currentState == ThwompState.waitingToRise) { //Ticks up the timer timer += Time.deltaTime; //Checks if it's time to rise back up yet if (timer >= secondsToRise) { //Resets the timer for the next drop timer = 0f; currentState = ThwompState.rising; } } if (currentState == ThwompState.rising) { //Moves the Thwomp upward thwompCharacterController.Move(Vector3.up * risingSpeed * Time.deltaTime); //Returns the Thwomp to waiting mode if it returns to it's original position if (this.transform.position.y >= startPosition.y) { currentState = ThwompState.waitingToFall; } } }
// Start is called before the first frame update void Start() { CurrentState = ThwompState.IDLE; StartCoroutine(ThwompFSM()); }