Help working existing code into AI.

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);
        }

You need to ask yourself how you expect the enemy to behave, and then think of the logic behind what your ai should do things.

For example, in a game where you are racing an ai car, if the car is heading for a wall, then the car should turn and avoid that wall. when the car is in front of another car, then the car should release oil onto the road(to make it slipery for the car behind it) if it has any in storage.

You can get started with panda BT (free) or a similar asset from the store.
The asset allows you to easily peice together funtions to create anai.
Look at how in the example scene the enemies work

1 Like

I dislike using pre-made assets. It kind of ruins the point of learning how to do something if it’s already done for you.

At least look at it, you might very well learn something new.

Take a piece of paper and make a giant flow chart that represents the way you want your ai to behave kinda like this one:

write your main behavior logic (all your ifs else statements using variables that you can change in the editor) using logs or something similar to indicate your ai’s state.
Once you are happy with how the logic works, you can write the individual functions like bool isWaiting() or void attack().
And Tada! after a few broken keyboards and sleepless nights, you have an AI!

This is helpful but I think you are missing my original intent. I have all of this worked out. I wanted help getting the raycasting to work on its own in the AI. I already have a couple FSM’s (Finite State Machine) in place controlling everything.

Panda BT is not some sort of a collection of pre-made and ready to use AIs. It is a framework to make your coding easier. Exactly how your AI behaves is still up to you. If your like learning, Behaviour Tree really is worth it. It’s another tool to add to your tool set, next to FSM.