I am trying to create an AI for the enemies in my game. I have existing code that the player uses to select what enemy is being attacked and where to attack. It works perfectly for the player but I am having trouble translating it to an AI. I want the AI to be able to do the same as the player and be able to pick the type of attack that would do the most damage.
This is the code I want to translate:
if (DetectCharacterSelect.SelectedPlayer.GetComponent<CharacterSheet> ().characterActive == true) {
if (Input.GetKey ("escape") && attackButtonPanel.activeInHierarchy == true) {
selectedEnemy = null;
attackButtonPanel.SetActive (false);
}
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(DetectCharacterSelect.SelectedPlayer != null){
if (Physics.Raycast (ray, out hit,DetectCharacterSelect.SelectedPlayer.GetComponent<CharacterSheet>().CurrentClass.Range)) {
if (hit.transform.tag == "Enemy") {
selectedEnemy = hit.collider.transform.gameObject;
BoxCollider box = hit.collider as BoxCollider;
if (box == null) Debug.LogWarning("Collider is not a BoxCollider!");
Vector3 Normal = hit.normal;
Normal = hit.transform.TransformDirection( Normal );
float dotUp = Vector3.Dot(hit.normal, hit.transform.up);
float dotForward = Vector3.Dot(hit.normal, hit.transform.forward);
float dotRight = Vector3.Dot(hit.normal, hit.transform.right);
if(dotUp < -0.99){
//Below
attackButtonText.text = "Attack" + " (Below)";
Debug.Log ("Sneak Attck Below Enemy");
attackAboveOrBelow = true;
attackFront = false;
attackBehind = false;
attackFlank = false;
attackButtonPanel.SetActive (true);
}else if(dotUp > 0.99){
//Above
attackButtonText.text = "Attack" + " (Arial)";
Debug.Log("Arial Attack Above Enemy");
attackAboveOrBelow = true;
attackFront = false;
attackBehind = false;
attackFlank = false;
attackButtonPanel.SetActive (true);
}else if (dotForward < -0.99){
//behind
attackButtonText.text = "Attack" + " (Behind)";
Debug.Log("Unaware Attack Behind Enemy");
attackAboveOrBelow = false;
attackFront = false;
attackBehind = true;
attackFlank = false;
attackButtonPanel.SetActive (true);
}else if(dotForward > 0.99){
//front
attackButtonText.text = "Attack" + " (Infront)";
Debug.Log("Direct Attack Infront Of Enemy");
attackAboveOrBelow = false;
attackFront = true;
attackBehind = false;
attackFlank = false;
attackButtonPanel.SetActive (true);
}else if (dotRight < -0.99){
//Left
attackButtonText.text = "Attack" + " (Flank Left)";
Debug.Log("Flank Attack To The Left");
attackAboveOrBelow = false;
attackFront = false;
attackBehind = false;
attackFlank = true;
attackButtonPanel.SetActive (true);
}else if (dotRight > 0.99){
//Right
attackButtonText.text = "Attack" + " (Flank Right)";
Debug.Log("Flank Attack To The Right");
attackAboveOrBelow = false;
attackFront = false;
attackBehind = false;
attackFlank = true;
attackButtonPanel.SetActive (true);
}
Statistics.EnemySelected();
}
}
}
}
}
if (DetectCharacterSelect.SelectedPlayer.GetComponent<CharacterSheet> ().characterActive == false) {
attackButtonPanel.SetActive(false);
}