GameMaker Bullets angle when player is moving

PlayerOne

Member
Title says it all. I'm trying to get the player to shoot properly but the bullets keep moving at angle everytime the player character moves. Trying to make a metal slug style system where you shoot 4 ways. Any help is appreciated. I'm sure it's something simple I'm overlooking.

Code:
var _xx=0
var _yy=+(-24*aim)
var _xscale = image_xscale;
var _rotation=_rota

if (alarm[0]=-1) // If alarm is <= 0
{

   if global.data[# 1,6]>0 // <<< Player ammo
   {

   var bullet = instance_create_depth(x+_xx, y+_yy, 5, global.data[# 3,6]); // <<< Create bullet and assign var
   audio_play_sound(global.data[# 3,7],0,0) //Play Audio when bullet is created.   

   with(bullet) // <<< With created bullet
   {
   speed=10*_xscale+(2*_xscale);
   direction=_rotation+irandom_range(-global.p_spread,global.p_spread);
   }

   global.data[# 1,6]--; // <<< Subtract player ammp
   alarm[0]=30

   }
   else
   {
   
   //audio_play_sound(snd_gunshot_mainC,0,0) //Play Audio when bullet is created.   

   }
 
Z

Zachary_tzy

Guest
Title says it all. I'm trying to get the player to shoot properly but the bullets keep moving at angle everytime the player character moves. Trying to make a metal slug style system where you shoot 4 ways. Any help is appreciated. I'm sure it's something simple I'm overlooking.

Code:
var _xx=0
var _yy=+(-24*aim)
var _xscale = image_xscale;
var _rotation=_rota

if (alarm[0]=-1) // If alarm is <= 0
{

   if global.data[# 1,6]>0 // <<< Player ammo
   {

   var bullet = instance_create_depth(x+_xx, y+_yy, 5, global.data[# 3,6]); // <<< Create bullet and assign var
   audio_play_sound(global.data[# 3,7],0,0) //Play Audio when bullet is created. 

   with(bullet) // <<< With created bullet
   {
   speed=10*_xscale+(2*_xscale);
   direction=_rotation+irandom_range(-global.p_spread,global.p_spread);
   }

   global.data[# 1,6]--; // <<< Subtract player ammp
   alarm[0]=30

   }
   else
   {
 
   //audio_play_sound(snd_gunshot_mainC,0,0) //Play Audio when bullet is created. 

   }
I suspect what you need to do is in your bullet object you will need it to move in the direction your player is moving to give it the illusion of a metal slug bullet. Because when you move, and i'm assuming your camera follows your player, the bullets don't move to the side for example and will look like they shoot at an angle.
 

PlayerOne

Member
I suspect what you need to do is in your bullet object you will need it to move in the direction your player is moving to give it the illusion of a metal slug bullet. Because when you move, and i'm assuming your camera follows your player, the bullets don't move to the side for example and will look like they shoot at an angle.
Not sure how then. Because the bullets need to shoot in accordance with the player aiming in the direction while moving at the same time. My only fix for it would be to add movement to the player's bullet to makeup the difference to ensure it shoots properly. However that's only a hotfix.
 
Z

Zachary_tzy

Guest
Not sure how then. Because the bullets need to shoot in accordance with the player aiming in the direction while moving at the same time. My only fix for it would be to add movement to the player's bullet to makeup the difference to ensure it shoots properly. However that's only a hotfix.
I guess that's the way it could be done, but might take too much processing power if each bullet needs to move. Another idea is you could have a fixed object from the player that follows the player's axis depending on where he is aiming, and have the bullets move_towards_point() to it. I'm not sure if that takes up less power to execute than each bullets responding to key check events but you can try it out.
 

Slyddar

Member
Depending on your setup, maybe you should pass the image_angle of the player or gun/aim to the bullet when it's created. Basically set the bullet.image_angle to bullet.direction, after you've set the direction. Also once you pass the direction and image_angle, the player should have no further input to the bullet, and all player movements shouldn't effect the bullet, unless you have code elsewhere that make them interact.
 
N

NeZvers

Guest
If you set angle and direction during creation and don't touch anything then that should be it. You don't need to reference the player object anywhere after that.
 

TheouAegis

Member
Why is x not offset but y is? Is this code in the player or in the gun? You should be offsetting the bullets when they are created by the angle of the aiming at the same distance. To avoid the player walking into the bullet, create the bullet at the end of the step event after you have already handled player movement.
 

PlayerOne

Member
If you set angle and direction during creation and don't touch anything then that should be it. You don't need to reference the player object anywhere after that.
Sorry, should've mentioned it's a sidescroller not a top down type of game.

Why is x not offset but y is? Is this code in the player or in the gun? You should be offsetting the bullets when they are created by the angle of the aiming at the same distance. To avoid the player walking into the bullet, create the bullet at the end of the step event after you have already handled player movement.
I was messing with the _xx and _yy to get a different result, so far nothing. That said I'm trying to shoot in 4 directions with my current setup (side scroller) and have the bullets created at the end of the weapon. Also the bullet is created in the user_event1 when global mouse button is pressed. I'm not using the mouse_x or mouse_y because I'm trying to use w and s to aim up and down.

Tinkered with it some more and was able to bullet to fire correctly from left and right, but not up and down. Trying to make the vspeed work correctly at the moment.

Code:
//Step:
aim=(aim_up+aim_down)
_rota = 90*(aim*round(image_xscale)) // <<< Calculation of weapon rotation
Code:
//User event 0

var _xx=45*image_xscale;
var _yy=(-45*aim)//+(crouch*14);
var _xscale = image_xscale;
var _rotation=_rota

if (alarm[0]=-1) // If alarm is <= 0
{

   if global.data[# 1,6]>0 // <<< Player ammo
   {
   
   //audio_play_sound(global.data[# 3,2],0,0) //Play Audio when bullet is created.
   with (instance_create_depth(x+_xx, y+_yy, 5, global.data[# 3,0])) // <<< Bullet
   {
   image_xscale = other.image_xscale;
   hspeed = sign(image_xscale)*15;
   }

       global.data[# 1,6]--; // <<< Subtract player ammo
       alarm[0]=30 // <<< Alarm is more than 0

   }
   else
   {
   
   //audio_play_sound(snd_gunshot_mainC,0,0) //Play Audio when bullet is created.   

   }


}
 
M

maru_th_undrtkr

Guest
I Put this code into the "Bullet Object"

Code:
direction=point_direction(o_player.x, o_player.y, x, y);
speed=40;
Example - This is how it looks in action.

Edit: Sorry guys, I know its a simple method, but it gets the job done for me.
 
N

NeZvers

Guest
It doesn't matter if it's sidescroller or not. It works THE SAME. There's another way to do the same.
Code:
var bullet =instance_create_depth(x+_xx, y+_yy, 5, global.data[# 3,0]);
bullet.image_xscale = image_xscale;
bullet.hspeed = sign(image_xscale)*15;
It's anything you need. Even with() call should be ok.
Your bullet shouldn't have anything in step event that references something from the player that could influence movement. Only collision detection should be there. Basically set it and forget it, let the bullet move.
 

PlayerOne

Member
It doesn't matter if it's sidescroller or not. It works THE SAME. There's another way to do the same.
Code:
var bullet =instance_create_depth(x+_xx, y+_yy, 5, global.data[# 3,0]);
bullet.image_xscale = image_xscale;
bullet.hspeed = sign(image_xscale)*15;
It's anything you need. Even with() call should be ok.
Your bullet shouldn't have anything in step event that references something from the player that could influence movement. Only collision detection should be there. Basically set it and forget it, let the bullet move.
It doesn't seem to matter. I scrapped the whole weapon angle, remade it from scratch, and yet the bullet angles as before. Could be a lag issue with the camera object. :confused:
 

TheouAegis

Member
Not sure why you are trying to obfuscate ballistics. Are you aiming up or down? If yes set hspeed to 0 and vspeed to 15*(aim_up+aim_down); otherwise set vspeed to 0 and hspeed to 15*image_xscale. Assuming your aiming is either straight up, straight down, left and right.
 

Slyddar

Member
The image shows the player moving right while aiming up, yet the bullets move to the right or left and not straight up as you would see in games like contra.
Maybe I'm misunderstanding from the image, but the player is moving to the right, and the bullets are being created at the position the player was, and traveling vertically. They are not moving left or right, but the camera is. Their x position is remaining unchanged.
 
S

Sabrina Stoakes

Guest
Hey! Alrighty, so one simple thing I like to do that I think you will like is to create everything like this in the object that will be shooting.

Code:
shoot = instance_create(x,y,YourBullet)
shoot.image_angle = ///Player image angle
shoot.direction = ///Player direction
shoot.speed = ///Bullet Speed
 
N

NeZvers

Guest
You didn't really explain that problem is shooting up. From your text, I understood that bullet flies other direction as soon player turns around.
CODE]
var vinput = key_down - key_up; //your up and down buttons or maybe you already have such variable
var dir = point_direction(0, 0, image_xscale, input);
var bullet =instance_create_depth(x+_xx, y+_yy, 5, global.data[# 3,0]);
bullet.direction = dir;
bullet.image_angle = dir;
bullet.speed = 15; // better to use a variable instead of "magic number" 15 or set speed in bullets create event
[/CODE]
 
Top