[SOLVED] Why are my bullets shooting from the center of my character?

T

TGSP

Guest
So in the game I have, my gun shoots it's bullets from the center of the character instead of my gun, what exactly would be the reason for this, I will prove the step event for my gun below:

Code:
//Gravity

if place_free(x,y+1){gravity=0.7 gravity_direction=270}else{gravity=0 gravity_direction=270};

//Gun Grounded



//Gun Pickup

if place_meeting(x,y,obj_potatoman) and keyboard_check_pressed(ord("F"))
{
alarm[0] = 60
};

if alarm[0] = 60
{
obj_ketchup_pistol.x = obj_potatoman.x; obj_ketchup_pistol.y = obj_potatoman.y
};

//Gun Release

if place_meeting(x,y,obj_potatoman) and keyboard_check_pressed(ord("R")) and alarm[0] = 60 and sprite_index = spr_ketchup_pistolR
{
direction = 90; speed = 6; alarm[0] = 0; image_angle = 0;
}

else

if place_meeting(x,y,obj_potatoman) and keyboard_check_pressed(ord("R")) and alarm[0] = 60 and sprite_index = spr_ketchup_pistolL
{
direction = 90; speed = 6; alarm[0] = 0; image_angle = 180;
};

//Gun Image Rotation

if place_meeting(x,y,obj_potatoman) and alarm[60]
{
image_angle = point_direction(obj_potatoman.x, obj_potatoman.y, mouse_x, mouse_y);
};

//Gun Shoot Mechanic

firecounter = firecounter + 1;

if place_meeting(x,y,obj_potatoman) and (mouse_check_button_pressed(mb_left) or mouse_check_button(mb_left)) and firecounter >= firerate and alarm[0] = 60
{
var bullet = instance_create(x, y, obj_ketchup_bullet);
bullet.speed =+15;
bullet.direction = image_angle;
bullet.image_angle = image_angle;
firecounter = 0
};
 

obscene

Member
It creates it exactly where you tell it to....

instance_create(x, y, obj_ketchup_bullet);

Use your gun's x and y instead.
 
F

Fishman1175

Guest
It creates it exactly where you tell it to....

instance_create(x, y, obj_ketchup_bullet);

Use your gun's x and y instead.
He's rotating the gun around a central X and Y, so his sprite origin is probably in the center.

In this case, figure out how long your gun is and do something like
Code:
instance_create(lengthdir_x(gun_length,image_angle),lengthdir_y(gun_length,image_angle),obj_ketchup_bullet)

Obj_potatoman? This game sounds awesome
 
T

TGSP

Guest
He's rotating the gun around a central X and Y, so his sprite origin is probably in the center.

In this case, figure out how long your gun is and do something like
Code:
instance_create(lengthdir_x(gun_length,image_angle),lengthdir_y(gun_length,image_angle),obj_ketchup_bullet)

Obj_potatoman? This game sounds awesome
This may be a dumb question, bear with me I'm a beginner, but what exactly what I put in the place of " gun_ length ". I would just put the amount of pixels in length of the gun? But I hope this game turns out to be awesome ;)
 
M

Mholmes3038

Guest
I think gun length is reffering to the width of the gun so it knows where to place the bullet. So your bullet has a starting point, the end of the barrel would logically be where it would start of "fire".
 
T

TGSP

Guest
TGSP
So yes.. "put the amount of pixels in length of the gun".
Okay, so I did that but now none of my bullets will be created, what exactly am I doing wrong?

Code:
if place_meeting(obj_ketchup_pistol.x,obj_ketchup_pistol.y,obj_potatoman) and (mouse_check_button_pressed(mb_left) or mouse_check_button(mb_left)) and firecounter >= firerate and alarm[0] = 60
{
var bullet = instance_create(lengthdir_x(8,image_angle),lengthdir_y(6,image_angle),obj_ketchup_bullet);
bullet.speed =+15;
bullet.direction = image_angle;
bullet.image_angle = image_angle;
firecounter = 0
};
 
N

Never Mind

Guest
lengthdir_x https://docs.yoyogames.com/source/dadiospice/002_reference/maths/real valued functions/lengthdir_x.html
lengthdir_y https://docs.yoyogames.com/source/dadiospice/002_reference/maths/real valued functions/lengthdir_y.html
First off you should try to understand those functions. You're going to get a weird result by supplying them each with a different length argument.
To project a point forward an amount at a certain angle you should hand them both the same length.

But that's not even your main problem. Take a close look at the x and y positions your giving to instance_create:
-------------------------------------------------------------------------------------------------
instance_create(lengthdir_x(8,image_angle),lengthdir_y(6,image_angle),obj_ketchup_bullet);
-------------------------------------------------------------------------------------------------
Nowhere do you use the current location of obj_potatoman. That is the problem.
What you want is:
-------------------------------------------------------------------------------------------------
instance_create( x + lengthdir_x(8,image_angle), y + lengthdir_y(6,image_angle), obj_ketchup_bullet);
-------------------------------------------------------------------------------------------------
lengthdir_x/y just help you determine the extra bits to add. You still need to add them to the players position to find the final location.

If your familiar with math those functions are just doing this:
-------------------------------------------------------------------------------------------------
length * cos(angle)
length * sin(angle)
-------------------------------------------------------------------------------------------------
 
F

Fishman1175

Guest
In addition to what Never Mind said,

You changed this line
Code:
if place_meeting(obj_ketchup_pistol.x,obj_ketchup_pistol.y,obj_potatoman) and (mouse_check_button_pressed(mb_left) or mouse_check_button(mb_left)) and firecounter >= firerate and alarm[0] = 60
for some reason. If the bullet isn't being created it's because this IF statement is being false.

Also, the length probably shouldn't be two different numbers. You have 8 and 6. See my attached image. The green cross signifies the origin of the sprite. The red line represents the length of the gun. Use the same number in both lengthdir_x and lengthdir_y
 

Attachments

T

TGSP

Guest
lengthdir_x https://docs.yoyogames.com/source/dadiospice/002_reference/maths/real valued functions/lengthdir_x.html
lengthdir_y https://docs.yoyogames.com/source/dadiospice/002_reference/maths/real valued functions/lengthdir_y.html
First off you should try to understand those functions. You're going to get a weird result by supplying them each with a different length argument.
To project a point forward an amount at a certain angle you should hand them both the same length.

But that's not even your main problem. Take a close look at the x and y positions your giving to instance_create:
-------------------------------------------------------------------------------------------------
instance_create(lengthdir_x(8,image_angle),lengthdir_y(6,image_angle),obj_ketchup_bullet);
-------------------------------------------------------------------------------------------------
Nowhere do you use the current location of obj_potatoman. That is the problem.
What you want is:
-------------------------------------------------------------------------------------------------
instance_create( x + lengthdir_x(8,image_angle), y + lengthdir_y(6,image_angle), obj_ketchup_bullet);
-------------------------------------------------------------------------------------------------
lengthdir_x/y just help you determine the extra bits to add. You still need to add them to the players position to find the final location.

If your familiar with math those functions are just doing this:
-------------------------------------------------------------------------------------------------
length * cos(angle)
length * sin(angle)
-------------------------------------------------------------------------------------------------
So I read up on how these functions work, and I applied the "x + lengthdir_x" and the "y + length_y". I also changed the both numbers to 6 so they were the same number. And still my character is shooting from the center of his sprite when I pick up my gun to shoot. Would this have anything to do with my end step event for the gun which is:

Code:
if place_meeting(x,y,obj_potatoman) and mouse_x >= bbox_right and alarm[0] = 60
{
obj_ketchup_pistol.sprite_index = spr_ketchup_pistolR; obj_ketchup_pistol.x = obj_potatoman.x + 20; obj_ketchup_pistol.y = obj_potatoman.y + 1
};
if place_meeting(x,y,obj_potatoman) and mouse_x <= bbox_right and alarm[0] = 60
{
obj_ketchup_pistol.sprite_index = spr_ketchup_pistolL; obj_ketchup_pistol.x = obj_potatoman.x - 20; obj_ketchup_pistol.y = obj_potatoman.y + 1
};
 
T

TGSP

Guest
After all this, my post still stands...
I actually tried your method again, it looks as if I didn't do it correct the first time. It works now, when telling which x and y coordinate I wanted it to go to I must have made some sort of error that I didn't catch. It now shoots from the gun at all times, not the center of my character sprite. Thanks for posting this again to make me go back and try to make it work again. Lol this could have all been resolved yesterday if I payed close enough attention to what I was doing.
 

obscene

Member
I was wondering what all this other stuff was about lol.

But seriously, now that you are shooting from the center of the gun, you could apply the lengthdir method these guys were showing you to shoot from the tip of the gun by adding the distance from the gun's origin to the tip of the gun and the image_angle of the gun. But I wasn't going to say any of that until you got past step 1. :)
 
N

Never Mind

Guest
obscene you must have looked closely at TGSP's code in the original post.
The way you restated yourself made it seem like everyone else was misinformed, and I knew there was some context I was missing.

So I went back to re-read and yes, I totally missed something and assumed this was for a top down game. There's a couple lines for gravity at the top, so more than likely the gun object has a different y position than the player object since it's a side-view.

That makes sense with what obscene said right? BUT.. actually no
Look at what TGSP was doing:
-------------------------------------------------------------------------
obj_ketchup_pistol.x = obj_potatoman.x; obj_ketchup_pistol.y = obj_potatoman.y
-------------------------------------------------------------------------
So what obscene said originally would have made no difference..


EXCEPT NOW that TGSP decided to change his code half way through :
-------------------------------------------------------------------------
obj_ketchup_pistol.x = obj_potatoman.x + 20; obj_ketchup_pistol.y = obj_potatoman.y + 1
-------------------------------------------------------------------------

and so
What obscene said is suddenly true.

@TGSP

When you change things in code.. other things sometimes need to change in other code. :rolleyes: <- the worst way I could have said that?
But yeah seems like your learning a lot here. I'm sure you'll find lengthdir_x/y useful as you get used to them.
 
Last edited by a moderator:
Top