• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Shooting in GameMaker studio 2

S

Sevonka

Guest
Hello everyone, how to make shoot in a gamemaker studio 2, what would the shots be flying, where my character is looking, control on the WASD keys. I say in advance, I use physics, and I need it(sry for eng lang)

This my code(i use scripts)
Code:
event_inherited();
player_input();
if(right_move) {
    phy_position_x += speed_player;
    sprite_index = sprite_player_right;
    image_speed = 1;
}
if(left_move) {
    phy_position_x -= speed_player;
    sprite_index = sprite_player_left;
    image_speed = 1;
}
if(down_move){
    phy_position_y += speed_player;
    sprite_index = sprite_player_down;
    image_speed = 1;
}
if(up_move){
    phy_position_y -= speed_player;
    sprite_index = sprite_player_up;
    image_speed = 1;
}

if(!right_move and !left_move and !up_move and !down_move) {
    image_speed = 0;
    image_index = 0;
}
 

Attachments

Paskaler

Member
You can use point_direction(phy_position_x, phy_position_y, mouse_x, mouse_y) to get the direction from the player to the mouse position.
 
S

Sevonka

Guest
You can use point_direction(phy_position_x, phy_position_y, mouse_x, mouse_y) to get the direction from the player to the mouse position.
I do not need the player to shoot where the mouse is, I need him to shoot where he looks
 

Paskaler

Member
I do not need the player to shoot where the mouse is, I need him to shoot where he looks
Oops! Sorry.
Here's a simple way, without introducing new variables:
Code:
switch sprite_index {
case spr_player_up:
var projectile_dir = 90;
// create projectile
break;
case spr_player_down:
var projectile_dir = 270;
// create projectile
break;
case spr_player_left:
var projectile_dir = 0;
// create projectile
break;
case spr_player_right:
var projectile_dir = 180;
// create projectile
break;
}
Sorry for no indentation, I'm on mobile.
 
B

Badger

Guest
As? point_direction(phy_position_x, phy_position_y, ???_x, ???_y)
This depends on your game. You're going to have to think about it. Are you trying to let the player shoot in 360 degrees? Is he only able to shoot in 4 directions?
 
S

Sevonka

Guest
Oops! Sorry.
Here's a simple way, without introducing new variables:
Code:
switch sprite_index {
case spr_player_up:
var projectile_dir = 90;
// create projectile
break;
case spr_player_down:
var projectile_dir = 270;
// create projectile
break;
case spr_player_left:
var projectile_dir = 0;
// create projectile
break;
case spr_player_right:
var projectile_dir = 180;
// create projectile
break;
}
Sorry for no indentation, I'm on mobile.
And where to copy it? In key down space?(screen)
 
S

Sevonka

Guest
This depends on your game. You're going to have to think about it. Are you trying to let the player shoot in 360 degrees? Is he only able to shoot in 4 directions?
He only able to shoot in 4 directions
 
B

Badger

Guest
Code:
//Lets assume that SPACEBAR will shoot
if(keyboard_check_pressed(vk_space)){
 //Paskaler's code here
}
If I'm being honest, it sounds like you are attempting a project that is beyond your skill level. I suggest working your way through a bunch of tutorials, and reading the manual as much as you can. I bet a lot of what you are trying to accomplish can be found in tutorials/demos.
 
S

Sevonka

Guest
Code:
//Lets assume that SPACEBAR will shoot
if(keyboard_check_pressed(vk_space)){
 //Paskaler's code here
}
If I'm being honest, it sounds like you are attempting a project that is beyond your skill level. I suggest working your way through a bunch of tutorials, and reading the manual as much as you can. I bet a lot of what you are trying to accomplish can be found in tutorials/demos.
obj_bullet just create and not flyupload_2017-11-30_0-20-20.png
Code:
if(keyboard_check_pressed(vk_space)) {
    point_direction(phy_position_x, phy_position_y, mouse_x, mouse_y);
    instance_create_depth(x, y, 0, obj_bullet);
}
 
B

Badger

Guest
Have you written any code at all for your bullet object? Why would the bullet fly in a direction if you have not told it to do that?
 
S

Sevonka

Guest
Have you written any code at all for your bullet object? Why would the bullet fly in a direction if you have not told it to do that?
no, because I know how to create a shooting in all directions, but I do not know how to only 4 directions
 
B

Badger

Guest
obj_bullet just create and not flyView attachment 14727
Code:
if(keyboard_check_pressed(vk_space)) {
    point_direction(phy_position_x, phy_position_y, mouse_x, mouse_y);
    instance_create_depth(x, y, 0, obj_bullet);
}
That is not the proper way to use that code. The point_direction() function returns a value that should be used. Just putting point_direction above instance_create is doing nothing at all.
Go ahead and read this: https://docs.yoyogames.com/source/d...e/maths/vector functions/point_direction.html
That should give you a better understanding of what the function actually does. After reading that, go ahead and read the entire manual.
When I first started with Game Maker, my general rule was: If I have to copy/paste code that I do not understand, then I am doing something that is too complex for me. Start small, man.

It is better to learn and thoroughly understand the code, than to just ask people to write code for you. That way, if you ever have a problem/bug, you will be able to fix it yourself. Also, you will be able to tweak code to your liking. You won't be able to do that if you don't get what's going on in the slightest. There are TONS of tutorials online for Game Maker. Go through as many of them as you can. Good luck!
 
S

Sevonka

Guest
That is not the proper way to use that code. The point_direction() function returns a value that should be used. Just putting point_direction above instance_create is doing nothing at all.
Go ahead and read this: https://docs.yoyogames.com/source/dadiospice/002_reference/maths/vector functions/point_direction.html
That should give you a better understanding of what the function actually does. After reading that, go ahead and read the entire manual.
When I first started with Game Maker, my general rule was: If I have to copy/paste code that I do not understand, then I am doing something that is too complex for me. Start small, man.

It is better to learn and thoroughly understand the code, than to just ask people to write code for you. That way, if you ever have a problem/bug, you will be able to fix it yourself. Also, you will be able to tweak code to your liking. You won't be able to do that if you don't get what's going on in the slightest. There are TONS of tutorials online for Game Maker. Go through as many of them as you can. Good luck!
You know which code will be correct, tell me it is simple, and then I'll figure it out myself. I understand what you want, that I myself would understand that I do not, but it will be much faster if you tell me how correctly, and after that I will understand what's what
 
B

Badger

Guest
Code:
 If (keyboard_check_pressed(vk_space)){
 switch(sprite_index){
  case spr_player_up:{
   var dir=90;
  }break;
  case spr_player_down:{
   var dir=270;
  }break;
  case spr_player_left:{
   var dir=180;
  }break;
  case spr_player_right:{
   var dir=0;
  }break;
 }
 instance_create(x+lengthdir_x(10,dir),y+lengthdir_y(10,dir),obj_bullet); 
 }
This will create a bullet in the direction you are facing. This will not tell the bullet to start flying. You will have to program your bullet object to do that.
 

nesrocks

Member
Can't you do it if you do this?

Replace
Code:
instance_create(x+lengthdir_x(10,dir),y+lengthdir_y(10,dir),obj_bullet);
With...
Code:
var mybullet = instance_create(x+lengthdir_x(10,dir),y+lengthdir_y(10,dir),obj_bullet);
mybullet.speed = 2;
 
B

Badger

Guest
Can't you do it if you do this?

Replace
Code:
instance_create(x+lengthdir_x(10,dir),y+lengthdir_y(10,dir),obj_bullet);
With...
Code:
var mybullet = instance_create(x+lengthdir_x(10,dir),y+lengthdir_y(10,dir),obj_bullet);
mybullet.speed = 2;
For sure. There are lots of ways this can be done. Personally I wouldn't even have that switch statement in there. Also, I believe that his bullets are physics objects, so the normal "speed" might not work for him
 
Top