# Thread: What is the best way to have ai fire at a random target?

1. ## What is the best way to have ai fire at a random target?

I need my ai to launch a object at a random target only at targets that still exist in the frame in my battle system, for example I have 7 player objects that each have their own attacks and can attack the enemies but I also have 7 enemy objects that each have their own attacks and the battle is over when either the player or computer is defeated. Help.

Hmmm... I suppose if I use 7 variables and base the target on their random value.
It might just be possible

2. You can use ForEach loops to loop through each enemy ship, and then run a new ForEach loop that loops through all potential targets, checks if they have allready been targeted, or else mark them as targeted and add their id to the enemy ship. After the loops ends, all your enemy ships should know which playership to aim at.

3. The easiest way I can think of is by using qualifiers. If all the player objects get given the same qualifier you can test their X/Y positions to make sure they're within the camera's edges, and pick one based off of that

4. Thanks that should work well I will give that a try and let you know how it turns out.
I just need to pick one from group good, because I don’t use cameras, but can you give me a YouTube video of how you do this “pick one of group good” thing? Because I don’t understand it or how to do that.

5. @marbenx could you please send me a YouTube video of how to do that?

6. Ah what the hey I will do it using a random variable.

7. A common way to do it is to set a value in the AI to the fixed value of the target and make it shoot towards that by checking for match.

Something like this:

Every 1 seconds
Target object alt value HP greater than 0 (or whatever else trigger you want to use)
Pick object Target at random
->Set alt value A of AI to Fixed Value of Target

AI Alt value A is equal to Fixed Value of Target
->Fire bullet towards Target
->set Alt Value A of AI to 0

In theory that should every 1 second check for all Target object that has HP greater than 0, then pick one at random, set an value in your AI object to the fixed value id of picked object. Then it checks if the AI value is equal to a Target object and then fire a bullet towards the picked object.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•