Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

How to make projectiles and make the enemy shoot the player?

Jason

Awesome Bro

Okay so now I've got the basic platforming down, except I've still not mastered walking up and down slopes yet (If anyone could answer that too, it'd be great)

But I've got to the point where the player sort of will need a gun...

So I've just had a mess about, I can make it so when I press a button, a bullet shoots from the players torso... (Haven't set the x and y, not sure lol)

But anyways, I've found a glitch too, it only works when I face right, can't get it to work when I face left (Done an if statement to check variable "direction" and see if it's 0 (Shoots right) or 180 (Shoots left)) and it will NOT shoot left whatsoever, but anyways, whenever I shoot while facing right, if I hold it on jump, my player jumps, stops, shoots, jumps stops shoots, etc. etc. ... but without touching the floor, so they can fly offscreen, but then respawn back at the start...

Can anyone possibly help with that, I'd just like a basic event that'll allow the player to shoot left when facing left, and shoot right when facing right, although I'm thinking of disabling shooting if the player moves, lol.

Also, is there a way to check the players position in accordance to another object, so I can make it when the player goes close enough to an enemy, they'll shoot at 'em, I think I can get the whole "follow the player and carry on shooting" thing, well, I can probably make it follow the player, not sure about shooting.

Sorry if it sounds big or whatever, but meh, I'm not really picking GM up as quickly as I'd have liked to, and GML is a whole different story...
 
What I did was set a variable when the sprite changed directions.

So...

press left key
move left
set variable "direct" to 1

press right key
move right
set variable "direct" to 2

press spacebar
if direct == 1
create moving intance of "obj_bullet", direction = left
if direct == 2
create moving instance of "obj_bullet", direction = right
 

Jason

Awesome Bro

Well it's working, sort of...

It now shoots in the right direction, but I'm having a problem with the jumping as I said in my first post, where, if I jump and press the shoot button (S), it shoots in midair (I don't want this anyway), and lets you jump again, so you can jump offscreen...

Any ideas here?

Edit---

Also, is there a way to put a wait function into an event?
 
Jbrist":a3b11a3a said:
Well it's working, sort of...

It now shoots in the right direction, but I'm having a problem with the jumping as I said in my first post, where, if I jump and press the shoot button (S), it shoots in midair (I don't want this anyway), and lets you jump again, so you can jump offscreen...

Any ideas here?

Edit---

Also, is there a way to put a wait function into an event?
I don't understand what you mean completly. What are the buttons you use for shooting and jumping? They are not the same?
Anyways I only use GML so You'll have to put this text in a execute code action (I'll call it 'code box' from here) that is inside the step event. (The code box is found under Control -> Execute Code, when editing the object).
I'll add comment so you understand better. All text behind '//' is comments and can be copied together with the code to the code box.
First the jumping and gravity:
//Adds the gravity and removes the gravity if the player is standing on solid ground
//I'll explain this a little better so hopefully everyone can understand exactly how this works.
//The 'place_free(x,y+1)' checks if the place under the player is free of solid objects. If the place is free it returns 1 (true) and else 0 (false).
//What does all this returns 1 or 0 mean? It means that if 1 is returned 'place_free(x,y+1)' is the same as '1'. I'll write the code and show another example.
gravity=place_free(x,y+1)*0.2;
//Remember that if the place is free it returns 1. This means that if the place is free 'gravity=place_free(x,y+1)*0.2;' is exactly the same as 'gravity=1*0.2;'
//And if the place is not free 'gravity=place_free(x,y+1)*0.2;' is the same as gravity=0*0.2;
//This means that if the place under the player is free gravity = 0.2 and otherwise gravity is 0.

//Time for jumping!
//Check if the player is standing on solid ground and if the player presses space make the character jump
//The 'vk_space' can be changed with 'ord("X")' (replace the X with any capital letter you want so you press that letter instead of X)
//The '!' symbol equals not so 'if !place_free(x,y+1)' = 'if not place_free(x,y+1) so the returned true or false becomes mirrored (sorry for poor choice of word).
if !place_free(x,y+1) and keyboard_check_pressed(vk_space){
//Make the player flyyyy! Change the -4 to any negative number you like depending on how much speed you want your char to have.
vspeed=-4;}
It became a couple of rows text for so little code but I hope I explained it good.
Time for the shooting:
//If you WANT shooting in mid-air remove the place_free code.
//This code checks if the player is on the ground and if the player pressed the S button.
if !place_free(x,y+1) and keyboard_check_pressed(ord("S"){
//Create the bullet! The 'temp=' is so we can edit it's speed later.
temp=instance_create(x,y,obj_bullet);
//now add speed to the bullet
temp.speed=3;
//and add the direction in which it's moving depending on the direction of the player
if direct=1{
temp.direction=180;}
if direct=2{
temp.direction=0;}}
I'm sorry it's only code. But GML is so much better than Drag & Drop once you get familiar to it. And the sooner you do it the better really. It even becomes easier to organize big games with GML since all the actions built up a pretty big pile very fast otherwise.
If there is something you want explained just ask. But I'd reccomend looking it up in the GM documentation first.
http://www.yoyogames.com/gamemaker/docs
 

Jason

Awesome Bro

Hmm, well I've tried both of them, I got an error with the shooting one on line 3, which is;

if !place_free(x,y+1) and keyboard_check_pressed(ord("S"){

And the jumping one just didn't work whatsoever, so neither of them had any effect on my game o.O

I'm going to run down what I've now got, and what I want;

Got;
What I've got is so that when you press A or D (Using WASD), it turns a variable named "shooting_direction" to 1 (For left) or 0 (For right).
When I press CTRL, it turns a variable called "can_shoot" to 1, whereas if I let go of it, it turns it to 0.
I've made animations for when the player is looking left and presses CTRL, and when they are looking right etc.
I've made it so if "shooting_direction" is 1, the bullet goes left, if it's 0, the bullet goes right.
HOWEVER, when I press CTRL, no matter which way I'm facing, it switches to the facing left animation of him holding a gun, and when I press Space to shoot, it shoots to the right.
I've tried multiple combinations to try and get this work, but I end up with the same results, and am now VERY fustrated since I've been at it since, well, the 12th, almost a whole month away, spending ATLEAST 2 hours a day almost each day trying to figure it out, so you can obviously see how pissed off I am with it.

Want;
I want it so when the player is facing left, a variable called "shooting_direction" will be 1, when they're facing right, it will be 2.
When the player presses/holds CTRL, it will turn a variable on called "can_shoot", and show the animation of him holding his gun to the left if the variable "shooting_direction" is 1, and the animation of him holding his gun to the right if the variable "shooting_direction" is 0.
I also want so that when I press Space, it will check to see if "can_shoot" is on, and then check to see if "shooting_direction" is equal to 1, if so, it will shoot the bullet left, but if not, it'll check to see if "shooting_direction" is equal to 0 (It can only be either), and it will shoot the bullet right.
However, when I jump, I'd like it to turn "can_shoot" off, since the player can't jump in midair.

It all seems so easy to put from words into GM, but it's SO hard and really fustrating, I'm thinking about just giving up the whole idea, but it's for a college assignment and it's due in two weeks, so if I can't do it, I'm pretty much screwed... lol.
 
Sorry about the shooting, missed a parenthesis.
Should look like this:
if !place_free(x,y+1) and keyboard_check_pressed(ord("S")){
I didn't try the codes, just wrote them of my head.
As for the jumping you need to provide more information, or better; post your game here.
Have you done any tutorials and looked at examples on ex the GMC? If not there is a platform tutorial on http://www.yoyogames.com/make/tutorials
And here's a lot of stuff too http://gmc.yoyogames.com/index.php?s=48 ... owforum=28
I suggest you do all the tutorials that are relative to your game in some way in the first link and look at some examples on the second link and try to fix whatever problems you're experiencing.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top