AI Shooting

4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€46.38
List Price:  €64.93
You save:  €18.55
£39.89
List Price:  £55.85
You save:  £15.96
CA$68.39
List Price:  CA$95.75
You save:  CA$27.36
A$75.62
List Price:  A$105.88
You save:  A$30.25
S$67.65
List Price:  S$94.72
You save:  S$27.06
HK$390.63
List Price:  HK$546.91
You save:  HK$156.28
CHF 45.32
List Price:  CHF 63.46
You save:  CHF 18.13
NOK kr540.83
List Price:  NOK kr757.20
You save:  NOK kr216.37
DKK kr346.01
List Price:  DKK kr484.44
You save:  DKK kr138.43
NZ$83.08
List Price:  NZ$116.32
You save:  NZ$33.23
د.إ183.61
List Price:  د.إ257.06
You save:  د.إ73.45
৳5,489.47
List Price:  ৳7,685.71
You save:  ৳2,196.23
₹4,174.70
List Price:  ₹5,844.92
You save:  ₹1,670.21
RM236.90
List Price:  RM331.68
You save:  RM94.78
₦70,962.30
List Price:  ₦99,352.90
You save:  ₦28,390.60
₨13,872.15
List Price:  ₨19,422.12
You save:  ₨5,549.97
฿1,835.14
List Price:  ฿2,569.35
You save:  ฿734.20
₺1,612.71
List Price:  ₺2,257.93
You save:  ₺645.21
B$257.06
List Price:  B$359.91
You save:  B$102.84
R919.21
List Price:  R1,286.96
You save:  R367.75
Лв90.71
List Price:  Лв127.01
You save:  Лв36.29
₩68,279.30
List Price:  ₩95,596.49
You save:  ₩27,317.18
₪186.20
List Price:  ₪260.69
You save:  ₪74.49
₱2,872.25
List Price:  ₱4,021.38
You save:  ₱1,149.12
¥7,785.79
List Price:  ¥10,900.73
You save:  ¥3,114.94
MX$837.71
List Price:  MX$1,172.87
You save:  MX$335.15
QR181.98
List Price:  QR254.79
You save:  QR72.81
P681.01
List Price:  P953.47
You save:  P272.46
KSh6,621.46
List Price:  KSh9,270.57
You save:  KSh2,649.11
E£2,369.41
List Price:  E£3,317.36
You save:  E£947.95
ብር2,858.54
List Price:  ብር4,002.19
You save:  ብር1,143.64
Kz41,809.30
List Price:  Kz58,536.37
You save:  Kz16,727.06
CLP$46,240.75
List Price:  CLP$64,740.75
You save:  CLP$18,500
CN¥361.16
List Price:  CN¥505.65
You save:  CN¥144.49
RD$2,895.12
List Price:  RD$4,053.41
You save:  RD$1,158.28
DA6,726.94
List Price:  DA9,418.25
You save:  DA2,691.31
FJ$113.51
List Price:  FJ$158.92
You save:  FJ$45.41
Q388.49
List Price:  Q543.91
You save:  Q155.42
GY$10,421.54
List Price:  GY$14,591
You save:  GY$4,169.45
ISK kr6,972.10
List Price:  ISK kr9,761.50
You save:  ISK kr2,789.40
DH500.60
List Price:  DH700.89
You save:  DH200.28
L887.29
List Price:  L1,242.28
You save:  L354.98
ден2,857.41
List Price:  ден4,000.60
You save:  ден1,143.19
MOP$400.79
List Price:  MOP$561.14
You save:  MOP$160.35
N$921.79
List Price:  N$1,290.58
You save:  N$368.79
C$1,832.69
List Price:  C$2,565.92
You save:  C$733.22
रु6,654.99
List Price:  रु9,317.52
You save:  रु2,662.52
S/185.34
List Price:  S/259.50
You save:  S/74.15
K193.05
List Price:  K270.29
You save:  K77.23
SAR187.48
List Price:  SAR262.49
You save:  SAR75.01
ZK1,362.81
List Price:  ZK1,908.05
You save:  ZK545.23
L230.78
List Price:  L323.12
You save:  L92.33
Kč1,156.66
List Price:  Kč1,619.42
You save:  Kč462.75
Ft17,987.75
List Price:  Ft25,184.29
You save:  Ft7,196.54
SEK kr541.71
List Price:  SEK kr758.43
You save:  SEK kr216.72
ARS$44,114.95
List Price:  ARS$61,764.46
You save:  ARS$17,649.51
Bs345.50
List Price:  Bs483.72
You save:  Bs138.22
COP$194,137.21
List Price:  COP$271,807.63
You save:  COP$77,670.42
₡25,482.48
List Price:  ₡35,677.52
You save:  ₡10,195.03
L1,227.34
List Price:  L1,718.38
You save:  L491.03
₲374,060
List Price:  ₲523,713.94
You save:  ₲149,653.93
$U1,921.90
List Price:  $U2,690.81
You save:  $U768.91
zł199.36
List Price:  zł279.12
You save:  zł79.76
Already have an account? Log In

Transcript

Here in this lecture, we are going to implement AI shooting for the enemy ships. We want the enemy ships to keep shooting as they come down. So for that we'll be using alarms so that they keep shooting. But we also need to make it so that they only shoot when the player is in front of them, otherwise they should not shoot. So to achieve that, we have to use some ad. This is the x coordinate of the player and this is the y coordinate.

For this we only need the X and here's the enemy's x coordinate. You can see that as the player gets closer to being in front of the enemy, the difference between the enemy's x and the players x reduces This was when the enemy should be able to shoot. So to make the enemy shoot, we just have to check whether the difference between the enemies x and the players x is below a certain value. If it is then the enemy should shoot. So here's what we will do, we will get the difference between the players x and the enemies x. So let's write that as Oh player x minus x. writing this in Game Maker will get the player in stances x coordinate.

So this will get the difference but it may be more stream or negative depending on where the player is, we only want a positive value. So around a subtraction or add brackets and outside those brackets I'll type ABS doing that will make it so that only a positive value can be returned. So if the difference is negative, say minus 28, it will give us a positive value like 28. So this way we get the distance between the players x and the enemies x, then we can use that value to check if the enemy can shoot. So let's open up Game Maker. I've imported a sprite for the enemies bullet, make sure its origin is at the middle center.

Now for the enemies bullets, I'll go to a bullet right click on it and select Duplicate. It will create a new object with the same events. I'll name this one oh bullet enemy. I'll assign the sprite to it. Now I'll open is creative. And you can see that the speed here is minus 20 because the player's bullets are supposed to move up but the enemy's bullets need to go down.

So I'll just remove the minus sign from here. Now this bullet will go downwards. Here you have a collision event with the enemy versus this is the enemy's bullet, it needs to hit the player. So right click on the event, select Change event, then go on the collision and select or player. And now this is a collision event with the player. So when this bullet hits the player, the players SP will go down.

Now we'll open a player, I'll go to a square demand or create a new variable with the assigned variable action, it will be SP with a value of five. So this is a player's health. And we'll come back to the subject in the next lecture. Now we'll open Oh enemy and go to its creative and here are the set alarm countdown action altitude from zero to 60. That alarm will be used for the shooting. So I'll add the event for that alarm.

First of all, I'll add the variable action. In place of the variable I'll type this so this console distance between the x and the enemy's x Now we need to make sure that this distance is low enough so that the enemy could shoot. So I'll change the condition to less and in the value I'll enter 128. So the distance should be smaller than 128. Now let's create an instance action and attach it to this condition for the object as let's call it enemy, the position will be the enemies x and y and the layer will be bullish. So if top layer is in front of the enemy, it creates a bullet in stance.

Now whether or not this condition is true, we want the alarm to keep looping. So it's like the get random number action and drop it below the condition so that it's placed outside of it. I'll use the section to set a random value for the alarm. So I'll change the symbol to integer I'll set the minimum to 30 and the maximum to 60. Now in the target, normally, I would use a temporary variable and then use that variable in an alarm action, but you can actually start the alarm directly from here without using the alarm action. So you have to type alarm square brackets and then the alarm number inside them.

So this will get a random value between 30 and six And directly assign it to the alarm. Now run the game. You can see that the enemy shoot when the player is in front of them, otherwise they don't. Now let's move on to the next lecture we will work on the player being head

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.