P
Player12Coconut
Guest
How do i make an AI enemy shoot at the player? The AI enemy isn't suppose to move and only just shoot something at the player. I am new to Gamemaker so i don't really know much.
Yup, I do want it to shoot at random times. It's a spaceship game where the enemy's are moving past the spaceship making it look like the spaceship is moving forward. But the Enemy is not suppose to move around and only just rotate and shoot at the player every few seconds.I assume you want to shoot an object at the player at random times, so it's a bit less mechanic. I'd say you should set up a timer and when it reaches 0, create the object, and set the timer to a random number.
Upon reaching 0 with the timer, you should create your object then with object_create. Since you didn't tell us anything specific, there isn't anything specific I can say about that part, but using timers should do the trick.
alarm[0] = room_speed * 5; // Shoot every 5 seconds
var bullet = instance_create(x, y, obj_bullet); // Create a bullet and store its ID in the variable "bullet". We need its ID b/c we want to do a few more things with it.
with (bullet)
{
direction = point_direction(x, y, obj_player.x, obj_player.y); // Give the bullet a direction
speed = 6;
}
alarm[0] = room_speed * 5; // Reset this alarm so the enemy will shoot after 5 more seconds. This will loop.
In the photos is it suppose to look like this or this?If you want the bullet to move towards the player when shot, but not follow the player:
obj_enemy
Create Eventobj_enemyCode:alarm[0] = room_speed * 5; // Shoot every 5 seconds
Alarm[0] Event
If you want the bullet to be homing, you can use the function "move_towards_point" in the bullet's Step Event.Code:var bullet = instance_create(x, y, obj_bullet); // Create a bullet and store its ID in the variable "bullet". We need its ID b/c we want to do a few more things with it. with (bullet) { direction = point_direction(x, y, obj_player.x, obj_player.y); // Give the bullet a direction speed = 6; } alarm[0] = room_speed * 5; // Reset this alarm so the enemy will shoot after 5 more seconds. This will loop.
Edit: I just read you want to shoot at random times. Just vary the alarm. For example: alarm[0] = irandom_range(3, 6) * room_speed;
If you're using GM2, the function is called: instance_create_layer. So you'd code:
instance_create_layer(x, y, "Instances", obj_bullet);
I just realized where i was suppose to put the 2 codes. It works now but the lasers aren't shooting at the ship. I think i know what i did wrong but i'm not fully sure.If you're using GM2, the function is called: instance_create_layer. So you'd code:
Also, is WinterShipFighter the name of a ship or the name of the bullet? You want to create a bullet, not a ship, so put the name of the bullet inside instead of WinterShipFighter.Code:instance_create_layer(x, y, "Instances", obj_bullet);
One last thing: did you set the alarm in the enemy's Create Event?
Where do the lasers go?I just realized where i was suppose to put the 2 codes. It works now but the lasers aren't shooting at the ship. I think i know what i did wrong but i'm not fully sure.
Up.Where do the lasers go?
Hmm. Are you sure the direction is:
direction = point_direction(x, y, obj_player.x, obj_player.y);
is obj_player the bullet or the ship?Hmm. Are you sure the direction is:
Code:direction = point_direction(x, y, obj_player.x, obj_player.y);
Well, that's up to you. You can name things anything you want. But I'd think you'd want obj_player to be the player, and obj_bullet to be the bullet.is obj_player the bullet or the ship?
Nvm it seemed to be the ship. Now it goes for me but not in a way a bullet would go. i can't send the video but there is a bunch of enemies and they shoot the bullet towards the player but it doesn't shoot it from the front side. And when the laser hits the player it just stays there when i move to the left all of the bullets follow me to the left. I also want the enemy to turn at the ship then shoot.Well, that's up to you. You can name things anything you want. But I'd think you'd want obj_player to be the player, and obj_bullet to be the bullet right. Change any names you want in the resource tree. I use the "obj" prefix so that I know, when looking at my code, that I'm dealing with an object. Without a prefix, how would you know the difference between the ship's sprite and the ship's object index. So the prefix is just to make your life easier.
instance_destroy(other); // "other" holds the ID of the bullet that hit you. So we're destroying that bullet.
Ok. It was helpful but when you return how do i make the enemy shoot a laser from the front of the enemy only and make the enemy rotate to the player? And when the laser goes isn't in the screen anymore how do i make it destroy itself?Well, I got to go in a sec, but I try to help you before I do.
If you want the laser to destroy when it hits the player:
obj_player
Collision Event with obj_bullet
You may want to take away player HP or something else in this event.Code:instance_destroy(other); // "other" holds the ID of the bullet that hit you. So we're destroying that bullet.
Anyway, best of luck. Sorry if I wasn't very much help.
There is an Outside Room event. I'd open up the obj_laser object and add that event. Inside that event code: instance_destroy();And when the laser goes isn't in the screen anymore how do i make it destroy itself?
You can rotate the enemy using image_angle. So in the enemy's Step Event code:how do i make the enemy shoot a laser from the front of the enemy only and make the enemy rotate to the player?
image_angle = point_direction(x, y, obj_player.x, obj_player.y) // Make the enemy point towards the player
var dx, dy;
dx = lengthdir_x(5, image_angle);
dy = lengthdir_y(5, image_angle);
instance_create_layer(x + dx, y + dy, "Instances", obj_laser);
Ok. I think this is the last thing, how do you make the bullet stop moving around after its shot? the bullet follows after the ship after its shot.There is an Outside Room event. I'd open up the obj_laser object and add that event. Inside that event code: instance_destroy();
You can rotate the enemy using image_angle. So in the enemy's Step Event code:
This second part, "shoot a laser from the front" is a bit harder. There's a tutorial on here called "Shooting from a rotating object". But you need to use the lengthdir functions.Code:image_angle = point_direction(x, y, obj_player.x, obj_player.y) // Make the enemy point towards the player
Make sure the sprite is facing right in the image editor. You'd have to see how far the tip of the gun is from the center of the sprite. Let's say it's 5 pixels away:
I think that's right. You can mouse-middle-click to see what a function does.Code:var dx, dy; dx = lengthdir_x(5, image_angle); dy = lengthdir_y(5, image_angle); instance_create_layer(x + dx, y + dy, "Instances", obj_laser);
It shouldn't follow the player. Is there any code in the bullet's Step Event?Ok. I think this is the last thing, how do you make the bullet stop moving around after its shot? the bullet follows after the ship after its shot.
In the step event it there isIt shouldn't follow the player. Is there any code in the bullet's Step Event?
You still need to change "instance_create" to: instance_create_layer(x, y, "Instances", object_name_here); Also, I would remove that x+= 0, y += 3 code. That is why your laser leaves the room. Changing the y position in a positive direction moves it downwards. You can simply destroy the laser when it hits the player. For example, if in the player object:In the step event it there is
x += 0;
y += 3;
I added that so that way the bullets would go out of the screen if they missed the ship, because earlier they would follow the ship where ever it goes. It still follows the ship but it's also going down out of the screen. And there is
instance_destroy();
in the outside room event.
I also keep on getting this message.
In the game your suppose to dodge the lasers. And i already changed instance_create toYou still need to change "instance_create" to: instance_create_layer(x, y, "Instances", object_name_here); Also, I would remove that x+= 0, y += 3 code. That is why your laser leaves the room. Changing the y position in a positive direction moves it downwards. You can simply destroy the laser when it hits the player. For example, if in the player object:
Collision Event with Enemy Laser
instance_destroy(other); // Destroy the laser that hit this object.
That way, you don't even have to worry about moving the laser off-screen.
In your earlier post, you were saying that the lasers follow the player after they collide with the player. If you don't want that to happen, all you need to do is destroy the laser in a collision event (check my earlier posts). Also, as I was saying above, I'd prefix your resources. So all your objects could be "obj_something", all your sprites could be "spr_something". That way you know what resource your dealing with in your code. For example:In the game your suppose to dodge the lasers. And i already changed instance_create to
instance_create_layer(x, y, "Instances", RedLaser);
Ohh i already changed it so when the lasers hit the ship the lasers disappear. I renamed the ship to obj_WinterShipFighter. Basically now the lasers are going out of the screen but the problem is, if a enemy shoots a laser and it goes down aiming for the ship, the laser goes for the ship even after its show. Basically i want to freeze the laser from going anywhere except down after its shot. I decided not to turn the enemy and just make the laser a circleIn your earlier post, you were saying that the lasers follow the player after they collide with the player. If you don't want that to happen, all you need to do is destroy the laser in a collision event (check my earlier posts). Also, as I was saying above, I'd prefix your resources. So all your objects could be "obj_something", all your sprites could be "spr_something". That way you know what resource your dealing with in your code. For example:
I think you'd be much better off naming it: obj_WinterShipFighter.
Ah ok, so you decided you just want the laser to go down? Change the direction code to:Basically i want to freeze the laser from going anywhere except down after its shot.
direction = 270;
What exactly do you mean by this. If you mean you want the laser to go straight down but it chases the player, you must have kept some code in the lasers step event that makes it follow.if a enemy shoots a laser and it goes down aiming for the ship, the laser goes for the ship even after its show.
I wanted the laser to go straight down earlier but anyways. I also mispelled shot*. And that’s what I mean it’s following the player.Ah ok, so you decided you just want the laser to go down? Change the direction code to:
You can google "gamemaker directions images" or something like that. You'll see that 270 is straight down.Code:direction = 270;
What exactly do you mean by this. If you mean you want the laser to go straight down but it chases the player, you must have kept some code in the lasers step event that makes it follow.
It Works, Thank you! But i think i change my mind, this is what it looks like.Ah ok, so you decided you just want the laser to go down? Change the direction code to:
You can google "gamemaker directions images" or something like that. You'll see that 270 is straight down.Code:direction = 270;
What exactly do you mean by this. If you mean you want the laser to go straight down but it chases the player, you must have kept some code in the lasers step event that makes it follow.
Your game looks cool : ) Congrats.It Works, Thank you! But i think i change my mind, this is what it looks like.The circle things are the enemy/turrets and the red thing is the ship.
In the pic, the enemies are on the right side. Do you mean that want enemies on the left side as well?i kind of want the enemy to shoot from almost any side as the ship dodges them, but I'm not sure how to do that.
Thanks . Where the enemies are is fine. i change my mind about the laser shooting down. So i thought the laser will only come out of one side like the top of the enemy. so when the enemy appears in the screen it will rotate towards the ship and shoot at the ship from the top of it.Your game looks cool : ) Congrats.
In the pic, the enemies are on the right side. Do you mean that want enemies on the left side as well?
So your enemy has a gun barrel, and you want the gun to rotate towards the player, and then shoot at the player?So i thought the laser will only come out of one side like the top of the enemy. so when the enemy appears in the screen it will rotate towards the ship and shoot at the ship from the top of it.
Yes.So your enemy has a gun barrel, and you want the gun to rotate towards the player, and then shoot at the player?
Ok. What you should do is:Yes.
image_angle = point_direction(x, y, obj_player.x, obj_player.y);
The enemy is one object, endless your also talking about the laser.Ok. If the enemy is one object
I just rewrote that post. So yeah, just the enemy, not the laser. The post is above yours.The enemy is one object, endless your also talking about the laser.
how do i use mirror flip or rotate?Ok. What you should do is:
obj_turret // Or whatever you named the enemy
Step Event
In the sprite editor, make sure your turret is pointing to the right. You can use mirror flip or rotate to get it there.Code:image_angle = point_direction(x, y, obj_player.x, obj_player.y);
If you don't mind, post an image of the turret sprite.how do i use mirror flip or rotate?
If you don't mind, post an image of the turret sprite.
I can't seem to find IDE. What does it look like?Oh ok. Open up the sprite. Go to the IDE (at the very top of the screen). Click on "image". After that, scroll down to "Rotate All Frames Clockwise (90 Degrees)". That should make the turret face right (it's pointing up, so rotating 90 degrees clockwise should make it face right).
When you open up the sprite, if you look at the top of GameMaker, you can click on the word "Image". It then drops down a list of options.I can't seem to find IDE. What does it look like?
Is it in here?When you open up the sprite, if you look at the top of GameMaker, you can click on the word "Image". It then drops down a list of options.
It worked, Thanks! But the same problem happened, the laser is following the ship after it came out of the turret. It didn’t when I did the direction code but I stopped using the direction code to shoot the lasers down. Basically the laser spawned out of the turret then later on as the ship moves away the laser will turn then follow the ship.From there, hit "edit image". Then you will see "image" at the top center/right of the screen.
If the laser is following the ship, there must be some code in its Step Event. If you want, post all the laser's code: it's Create, Step, etc. Then, if you tell me exactly what you want the laser to do and not do, I think I'll be able to help you better.the laser is following the ship after it came out of the turret
Well it has only one event which is the Outside Room event and in it is.If the laser is following the ship, there must be some code in its Step Event. If you want, post all the laser's code: it's Create, Step, etc. Then tell me exactly what it is you do and don't want the laser to do. I think I'll be able to help you better if we do it like that.
Oh. Then the code that makes the laser follow the player must be in some other object. Which object do you think it's in? The player object? I'd look through the likely causes. It's probably some code like: move_towards_point(...). I have to go for the moment, but I'll check back. Good luck!Well it has only one event which is the Outside Room event and in it is.
instance_destroy();
Ok,Thanks! I tried looking into the player/ship but there isn't anything having to do with the laser i think, except the collision with the laser, but all there is inside is.Oh. Then the code that makes the laser follow the player must be in some other object. Which object do you think it's in? The player object? I'd look through the likely causes. It's probably some code like: move_towards_point(...). I have to go for the moment, but I'll check back. Good luck!
Well, I don't know then. But it's got to be in some object. I'd take a second look. Best of luck, let me know if you have anymore questions.Ok,Thanks! I tried looking into the player/ship but there isn't anything having to do with the laser i think, except the collision with the laser, but all there is inside is.
instance_destroy(other);
I'll try and find out what it is.Well, I don't know then. But it's got to be in some object. I'd take a second look. Best of luck, let me know if you have anymore questions.
Also do you know what this means in the photo?Well, I don't know then. But it's got to be in some object. I'd take a second look. Best of luck, let me know if you have anymore questions.