Discuss Scratch

blueybestie4789
Scratcher
4 posts

How do I make enemy AI

I'm decently new and don't know how to make enemy AI in my boxing game or make it more intelligent. I don't want it to mindlessly chase the player I want it to have a brain and be able to decide whether to back up from a situation block attack or push forward but I have no idea how to do this
N8_D_GR8_1
Scratcher
1000+ posts

How do I make enemy AI

blueybestie4789 wrote:

I'm decently new and don't know how to make enemy AI in my boxing game or make it more intelligent. I don't want it to mindlessly chase the player I want it to have a brain and be able to decide whether to back up from a situation block attack or push forward but I have no idea how to do this
If you are looking for a simple solution, you can use the random block to make the enemy AI decide what to do. Here's an example:

define enemy decide what to do
set [random v] to (pick random (1) to (3))
if <(random) = (1)> then
set [what to do v] to [punch]
stop [this script v]
end
if <(random) = (2)> then
set [what to do v] to [block]
stop [this script v]
end
if <(random) = (3)> then
set [what to do v] to [move back]
stop [this script v]
end
...//add more as needed

You can call this function every once in a while and then have your enemy move according to its “what to do” variable.

I hope that helps. Just let me know if you have any questions or are looking for something different.
wei04787
Scratcher
100+ posts

How do I make enemy AI

In addition to that you can say

define enemy blocks
forever
if <<<player kicks enemy> and <(pick random (1) to (3)) = [1]>>> then
set [what to do] to [block kick]
end
if <<<player punches enemy> and <(pick random (1) to (3)) = [1]>>> then
set [what to do] to [block punch]
end
end

and so on. Also, make sure whatever you do is in some sort of loop or it will only repeat on time
blueybestie4789
Scratcher
4 posts

How do I make enemy AI

Thanks i'll try to do this!
blueybestie4789
Scratcher
4 posts

How do I make enemy AI

N8_D_GR8_1 wrote:

blueybestie4789 wrote:

I'm decently new and don't know how to make enemy AI in my boxing game or make it more intelligent. I don't want it to mindlessly chase the player I want it to have a brain and be able to decide whether to back up from a situation block attack or push forward but I have no idea how to do this
If you are looking for a simple solution, you can use the random block to make the enemy AI decide what to do. Here's an example:

define enemy decide what to do
set [random v] to (pick random (1) to (3))
if <(random) = (1)> then
set [what to do v] to [punch]
stop [this script v]
end
if <(random) = (2)> then
set [what to do v] to [block]
stop [this script v]
end
if <(random) = (3)> then
set [what to do v] to [move back]
stop [this script v]
end
...//add more as needed

You can call this function every once in a while and then have your enemy move according to its “what to do” variable.

I hope that helps. Just let me know if you have any questions or are looking for something different.




So if I wanted them to have a higher chance to block when you attack so it will still block occasionally but also when you throw a punch it has a bit higher of a chance and maybe add other features like this would i

if key space pressed
set [random v] to (pick random (1) to (3))
if <(random) = (2)> then
set [what to do v] to [block]
stop [this script v]
end
blueybestie4789
Scratcher
4 posts

How do I make enemy AI

N8_D_GR8_1 wrote:

blueybestie4789 wrote:

I'm decently new and don't know how to make enemy AI in my boxing game or make it more intelligent. I don't want it to mindlessly chase the player I want it to have a brain and be able to decide whether to back up from a situation block attack or push forward but I have no idea how to do this
If you are looking for a simple solution, you can use the random block to make the enemy AI decide what to do. Here's an example:

define enemy decide what to do
set [random v] to (pick random (1) to (3))
if <(random) = (1)> then
set [what to do v] to [punch]
stop [this script v]
end
if <(random) = (2)> then
set [what to do v] to [block]
stop [this script v]
end
if <(random) = (3)> then
set [what to do v] to [move back]
stop [this script v]
end
...//add more as needed

You can call this function every once in a while and then have your enemy move according to its “what to do” variable.

I hope that helps. Just let me know if you have any questions or are looking for something different.


Also if i wanted them to feel more intelligent than that would i have to make certain reactions for when the player is in different places or preformes different things. Like if i was pushing towards the middle how would i make them react and play a little more defensive or try to stay different distances or play differently if they are at lower health? would i do the same thing but with invisible sprites in different areas marking where the player is relative to the map or areas so i could have the ai decide?
El_snak
Scratcher
66 posts

How do I make enemy AI

check out https://scratch.mit.edu/projects/450977183/ for ideas, yo can also use turbowarps'tailgating extension
N8_D_GR8_1
Scratcher
1000+ posts

How do I make enemy AI

Also if i wanted them to feel more intelligent than that would i have to make certain reactions for when the player is in different places or preformes different things. Like if i was pushing towards the middle how would i make them react and play a little more defensive or try to stay different distances or play differently if they are at lower health? would i do the same thing but with invisible sprites in different areas marking where the player is relative to the map or areas so i could have the ai decide?
I think a simple way of making it smarter would be to make a variable called “player aggressiveness”. You can use some data from the player's movements to affect the player aggressiveness variable. You can use the player aggressiveness variable to weigh the probability of what the enemy ai will do. Here's an example script:

define decide what enemy should do
get player aggressiveness::custom
set [random number v] to (pick random (0) to (100))//assuming the player aggressiveness variable ranges from 0 to 100
if <(random number) > (player aggressiveness)> then
set [enemy: what to do v] to [something aggressive]
else
set [enemy: what to do v] to [something defensive]
end

In this example code, if the player is not aggressive, then the “player aggressiveness” variable will be a low number, for example, 20. It is most likely that a random number from 0-100 will be greater than 20, and so most likely, the enemy will do something aggressive. Similarly, if the player aggressiveness value is high, then the enemy will probably do something defensive.

I am not sure how your game mechanics work, so I can't really say how you could get the player aggressiveness value, but here are a couple of examples.

Let's say that the player always has three options: attack, defend, or do nothing. You can store the last 10 player moves in a list and then calculate how aggressive those moves were. Here's some example code.

define get player aggressiveness
set [player aggressiveness v] to (50)
set [i v] to (1)
repeat (10)
if <(item (i) of [player's last 10 moves v]) = [attack]> then
change [player aggressiveness v] by [5]
else
change [player aggressiveness v] by [-5]
end
end

Here's another example using player movement. It uses two vectors: one from the player to the enemy, and another from the player in the direction he is moving. The function finds the angle between the two vectors and scales it between 0 and 100.

define get player aggressiveness
set [angle 1 v] to ([atan v] of (((enemy y) - (player y))/((enemy x) - (player x))))//angle from player to enemy
set [angle 2 v] to ([atan v] of ((player vy)/(player vx)))//angle of player movement
set [angle difference v] to ([abs v] of ((angle 1) - (angle 2)))//angle between
set [player aggressiveness v] to (((angle difference)/[180])*[100])//scaled to within 0-100

Powered by DjangoBB