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

Creating a bullet relative to a rotated sprite

TheOnlyWRT

Member
Hello!

I have a top-down shooter where the player shoots towards the mouse with both the player and bullet sprite rotating towards the mouse. However, I am trying to get the bullet to spawn at the tip of the gun on the player's sprite, but I am having a real hard time doing that. The code I currently have is:

GML:
//create the bullet
var bulletX = x - lengthdir_x(80, mouse_x);
var bulletY = y + lengthdir_y(60, mouse_y);
with (instance_create_layer(bulletX, bulletY, "Player", objectBullet)) {

    //bullet code in here is not important

}
The specs of the sprite are given below (the white dot off the tip of the gun is where I would like the bullet to spawn). The sprite's anchor is set to the middle center:

Screen Shot 2020-05-11 at 4.53.43 PM.png

The position (74,49) is where the white dot is relative to the top left corner of the sprite.

And an in-game image of the issue (shows how the bullets are spawning way off currently):

Screen Shot 2020-05-11 at 4.50.37 PM.png

Thanks in advance for the help!
 

chamaeleon

Member
I have a top-down shooter where the player shoots towards the mouse with both the player and bullet sprite rotating towards the mouse. However, I am trying to get the bullet to spawn at the tip of the gun on the player's sprite, but I am having a real hard time doing that. The code I currently have is:
GML:
//create the bullet
var bulletX = x - lengthdir_x(80, mouse_x);
var bulletY = y + lengthdir_y(60, mouse_y);
with (instance_create_layer(bulletX, bulletY, "Player", objectBullet)) {
    //bullet code in here is not important
}
lengthdir_x() and lengthdir_y() takes a direction/angle as the second argument. mouse_x and mouse_y are positions.. It is quite possibly what you require is to use image_angle in both calls. Also, typically the length used in the calls is the same value (because the distance from point A to point B doesn't change just because you want to find out how far to move in the x and y directions, respectively), but you're using 80 vs 60.
 

Niels

Member
just set a (var bulletY = y + lengthdir_y(60, mouse_y) + offset (offset should be the y different between tip of gun and y origin of the object)
 
Top