Bullet image_angle issue

lyra_bee

Member
Hi guys, I have an issue where my bullet is firing sideways at my player from the enemy and I'm not sure why. Here is my code:


CREATE:
GML:
speed = 5 ;

xpos = O_Player.x;
ypos = O_Player.y;

direction = point_direction(x, y, xpos, ypos);
STEP:
Code:
image_angle = direction;
I've tried putting the image angle in the create too. The reason why the rest of the code is in the create it that I only want the bullet to aim to where the player was at the time of firing, not follow the player. I also thought it might be something to do with where the origin was on the sprite of the bullet but it doesn't make a difference where I move it.

Any help would be appreciated.

Cheers,

Bee
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
Direction 0 is right. Is your sprite facing to the right?
Do you have any Draw event? If so, make sure that when you're drawing the sprite, you're drawing it rotated.
 

lyra_bee

Member
my sprite is facing downwards, which is why I'm confused - am also quite a newbie. Which way would I need to rotate it on the draw event?

I don't currently have a draw event for this - does that make a difference to whether it will work properly?
 
Use the image editor to rotate your sprite to face right. If you're going to programmatically rotate a sprite, it always needs to be facing right as an actual sprite.
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
Either it needs to be facing right, or you have to offset its rotation accordingly - e.g. if it's facing down, add 90 to the rotation it's being drawn at to make it default to right.

You don't need a Draw event if it's facing right. If it is not facing right, you do need a Draw event and follow the line above.
 

lyra_bee

Member
It works! I don't get where you are seeing that's I'm programmatically rotating it to the right exactly, sorry for the silly question.

Bee
 

woods

Member
I don't get where you are seeing that's I'm programmatically rotating it
Code:
create event
direction = point_direction(x, y, xpos, ypos);

step event
image_angle = direction;
the sprite image is originally facing down (270)
you create the bullet with direction player.x/y
in the step event you change the image_angle of the bullet to face the target location //here is where you are programmatically rotating it


sorry for the silly question.
...no such thing ;o)
 
So, I was simplifying the issue before when I said you need to rotate the sprite in the image editor to the right. As Tsuka pointed out, you can account for the sprite facing any direction by adding an offset to your code.

The way that directions in GMS work is that 0 degrees is "naturally" facing right, 90 degrees is facing up, 180 degrees is facing left and 270 degrees is facing down. So let's say that you shoot a bullet to the right. The direction is set to 0 and you set the image_angle to the direction, which means that image_angle won't rotate the sprite (to put it another way, it will rotate the sprite by 0 degrees). Then if you shoot up, the direction is set to 90, and image_angle will take the sprite and rotate it by 90 degrees counter-clockwise to "face" the direction.

What this means is that when you draw your sprite, before you apply any code to it at all, that is considered it's orientation if it is moving to the right. So if you have a sprite that faces downwards and you shoot it to the right (which would be setting direction and image_angle to 0) that sprite won't be rotated at all (it will have 0 added to it's rotation), so it will move right but it will appear as though it is facing down. If you then shoot that sprite upwards, so the direction and image_angle is 90, the sprite will be rotated counter-clockwise to the right (as it will have 90 added to it's direction). A downwards facing sprite rotated 90 degrees counterclockwise will end up looking as though it is facing to the right (even though it's direction is the "up" direction).

This is why I said that you should draw your sprite as though it is facing to the right. It naturally aligns the sprite with GMS' native directions.

As Tsuka added, you could manually add 90 to image_angle (so image_angle = direction+90) which would make the downwards facing sprite rotate correctly. This works because, as I said before, a sprite that has it's image_angle set to the right actually has an image_angle of 0, so a downwards facing sprite won't get rotated at all, but if you manually add 90 to it each time you set image_angle, then the sprite would be rotated by 0+90 and end up facing right because of that added 90 degree turn that you manually added.
 
Last edited:
Top