//Return true if he can see an apple in his front public bool Sight(Vector3 inputPoint, GameObject apple) { //Debug.Log(appleList.Count); //Get the cosene of the angle between the foward vector from player and the vector from the apple float cosAngle = Vector3.Dot((inputPoint - this.transform.position).normalized, this.transform.forward); //transform the angle from radians to degrees float angle = Mathf.Acos(cosAngle) * Mathf.Rad2Deg; //Distance between player and apple. float distance = Vector3.Distance(this.transform.position, apple.transform.position); //Get Handler to AppleCycle. AppleCycle appleCycle = apple.GetComponent <AppleCycle>(); //If the angle is minor than the cutoff (45 degres) and the distance is minor than 15.0f then the "Player" is seeing the apple. if (angle < cutoff && distance < 15.0f) { //Change variable to stop GameObject to continue going on list. if (appleCycle.inList == false) { appleCycle.inList = true; //Insert apple on the list. InsertAppleInList(apple); } //Draw a line from the player to the apple. Debug.DrawLine(transform.position, inputPoint, Color.red); //return true. return(true); } //return false. return(false); }
private void NearestApple() { //Nearest Apple distance variable. float nearest = 1000000000f; AppleCycle appleCycleScript = null; //Give weight for the decision of what apple to choose. When 1 == rotting apple, 2 == green apple and 3 == red apple. int chooseWeight = 0; //Check in list the nearest apple foreach (GameObject apple in appleList) { //Check if the object in list is not null if (apple != null) { searchingApple = true; //Distance variable between player and apple. float distance = Vector3.Distance(apple.transform.position, this.transform.position); int appleCycleIndex = ChooseBetterAppleByStatus(apple); //Check if Distance from the apple is lesser than the previous apple if (distance < nearest && appleCycleIndex > chooseWeight) { chooseWeight = appleCycleIndex; nearest = distance; appleGameObj = apple; appleCycleScript = apple.GetComponent <AppleCycle>(); } movingToApple = true; } } //if the script is not null the variable chosen apple became true, this will control the next behavior of the AI. if (appleCycleScript != null) { appleCycleScript.chosenApple = true; } }
//This method will reset the variable of all the apples GameObject in the list appleList. private void ResetInList() { Debug.Log("ResetInList"); foreach (GameObject apple in appleList) { //Check if the variable isn't null if (apple != null) { //Handler to AppleCycle script AppleCycle appleCycle = apple.GetComponent <AppleCycle>(); appleCycle.inList = false; //Test if the apple needed to be destroyed or no. appleCycle.appleChange(); } } CleanList(); }
//This function will give a weight to the choose of he apple private int ChooseBetterAppleByStatus(GameObject apple) { AppleCycle appleCycleScript = apple.GetComponent <AppleCycle>(); if (appleCycleScript.appleCount == 0) { return(2); } else if (appleCycleScript.appleCount == 1) { return(3); } else { return(1); } }
private void OnTriggerEnter(Collider other) { //If it is the choosen apple then when Ai collides this changes will occur. if (other.tag == "Food") { AppleCycle foodScript = other.GetComponent <AppleCycle>(); if (foodScript.chosenApple == true) { int appleGeneratedHealth = 1; //Green Apple. if (foodScript.appleCount == 0) { appleGeneratedHealth = 15; //Red apple } else if (foodScript.appleCount == 1) { appleGeneratedHealth = 30; } //Rotting apple. else { appleGeneratedHealth = 5; } //Buff Bar EatApple(appleGeneratedHealth); //AI is no longer choosing apple. searchingApple = false; //AI is no longer doing an action. doingAction = false; //Desalocate all list. ResetInList(); //Player no longer moves to apple. movingToApple = false; //Rotation is needed to be done again. madeRotation = false; //Rotation is reseted. rotationLeft = 360f; Destroy(other.gameObject); } else { return; } } }