• 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!

GameMaker how shoot in the direction u are looking?

K

Kurumiseenpai

Guest
hi, how i can make that my character shoot in the direction he is looking?

this is my code

//write in player create

firingdelay = 0;

//this is write on player step

firingdelay = firingdelay -1;

if ((keyboard_check(ord("S")))) && (firingdelay < 0)
{
firingdelay = 5;
with (instance_create_layer(x,y,"Instances",oArmauno))
{
speed = 25;
direction = other.image_angle;
image_angle = direction;
}
}

//and in the bullet event i create just a post-draw with this

if (place_meeting(x,y,oplayerdue)) instance_destroy ();
if (place_meeting(x,y,oWall)) instance_destroy ();
 

kburkhart84

Firehammer Games
It looks like that's already going to happen, looking at the code in the with(instance_create_layer(x,y,"Instances",oArmauno)){} stuff...assuming that oArmauno is a bullet object. You are setting the bullet's direction based on the other.image_angle, and then setting the bullet's image_angle to the same as the direction, so it should face the same direction as the player and also move in that direction.

Since you haven't actually said what the issue is, and what it is doing...I don't know how to help you, but it looks fine to me.
I also think the stuff you have in the post-draw event would be better off in the step event. You could possibly also just use a collision event with those objects instead if you preferred, either way works. The post-draw event isn't really meant for anything where you would create/destroy objects, and could mess things up since the drawing system is doing a loop through all instances, and you are messing up that loop. The step event is immune to that problem, as are collision events.
 

woods

Member
direction = other.image_angle;

if the obj_player is facing left/right with -image_xscale... would that mess this up?
 

Laura Bravo

Member
Not sure what you refer to but if you are using -1 in xscale to flip a sprite it only changes the sprite not the object direction.
 

woods

Member
Not sure what you refer to but if you are using -1 in xscale to flip a sprite it only changes the sprite not the object direction.
thanks, thats what i thought

@OP
it would be easier to help if we had a look at how you are moving your player as well... i got a hunch you are using -image_xscale and not setting direction ;o)
 
K

Kurumiseenpai

Guest
here the whole code @woods





//muoversi
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_jump = keyboard_check(ord("W"));







//calcolo movimenti
var _move = key_right - key_left;
hsp = _move * walksp;
vsp = vsp + grv;

if (place_meeting(x,y+1,oWall)) && (key_jump)
{
vsp = -jumpsp
}







//collisioni
if (place_meeting(x+hsp,y,oWall))
{
while (!place_meeting(x+sign(hsp),y,oWall))
{
x = x + sign (hsp);

}
hsp = 0;

}
x = x + hsp;

if (place_meeting(x,y+vsp,oWall))
{
while (!place_meeting(x,y+sign(vsp),oWall))
{
y = y + sign (vsp);

}
vsp = 0;

}
y = y + vsp;






//run animation
if (keyboard_check(ord("A")))
{

sprite_index = rununo;
}
if (keyboard_check(ord("D")))
{
sprite_index = rununo;
}

if (keyboard_check (vk_nokey))
{
sprite_index = playeruno;
}

if (keyboard_check(ord("A")))
{
image_xscale = -0.3;
}

else
{
image_xscale = 0.3;
}





//bugfix
if(place_meeting(x,y, oWall)) {
for(var i = 0; i < 1000; ++i) {
//Destra
if(!place_meeting(x + i, y, oWall)) {
x += i;
break;
}
//Sinistra
if(!place_meeting(x - i,y, oWall)) {
x -= i;
break;
}
//Sopra
if(!place_meeting(x, y - i, oWall)) {
y -= i;
break;
}
//Sotto
if(!place_meeting(x, y + i, oWall)) {
y += i;
break;
}
//Alto destra
if(!place_meeting(x + i, y - i, oWall)) {
x += i;
y -= i;
break;
}
//Alto sinistra
if(!place_meeting(x - i, y - i, oWall)) {
x -= i;
y -= i;
break;
}
//Basso destra
if(!place_meeting(x + i, y + i, oWall)) {
x += i;
y += i;
break;
}
//Basso sinistra
if(!place_meeting(x - i, y + i, oWall)) {
x -= i;
y += i;
break;
}
}
}









//sparare
firingdelay = firingdelay -1;

if ((keyboard_check(ord("S")))) && (firingdelay < 0) && (keyboard_check(ord("A")))
{
firingdelay = 5;
with (instance_create_layer(x,y,"Instances",oArmauno))
{
speed = 25;
direction = -1;

}
}
 
K

Kurumiseenpai

Guest
So...

See this part here where you're using image_xscale and not image_angle to turn the sprite?

Why did you expect image_angle to have useful information in it?
Sorry but i rly suck at this, can u write me the code fixed pls? it be very helpful
 

FrostyCat

Redemption Seeker
If image_xscale holds the facing direction, then use that to compute where to shoot at, not image_angle.
GML:
with (instance_create_layer(x,y,"Instances",oArmauno))
{
    hspeed = sign(other.image_xscale)*25;
}
 
K

Kurumiseenpai

Guest
If image_xscale holds the facing direction, then use that to compute where to shoot at, not image_angle.
GML:
with (instance_create_layer(x,y,"Instances",oArmauno))
{
    hspeed = sign(other.image_xscale)*25;
}
HI! tx, now it works, but i cant figured out flip the sprite of the bullet if i look on left, i do
if (image_xscale = -x)
{
sprite_index = armauno
image_xscale = -1
}
BUT I THINK IS RLY WRONG
 
Top