GML [SOLVED] Help with create_instance_layer and bullet spawn point (lengthdir issues probably)

W

woyosensei

Guest
Hello guys, first timer here :)
I'm trying to solve my issue by myself for a while now, but can't figure it out. Not enough experience yet. So, basically I have a sprite from OpenGameArt. My pivot point is in the middle of the baseball cap of that character. I've also "moved" all frames I need from different size of each sprite into one-sized square (don't know if that matters, but just in case I've done it anyway). That makes shooting a little bit different from what I've seen in Shauns or Heartbeats tutorials as they have usually bullet spawn at the end of a gun in the middle of a sprite itself. In my case the gun is part of a player character and is not in straight line from the centre. Also it can't be just flipped like single gun. My attempt to do it is a failure (from my point of view) because the "spawn point" for a bullet is not in the place where it should be (gun barrel) but it moves with player character. Here is the code I've done so far:
Code:
    var dir = point_direction(x, y, mouse_x, mouse_y);
    var x_offset = lengthdir_x(100, dir);
    var y_offset = lengthdir_y(40, dir); // these should put the bullet spawn point
    //at the end of a gun barrel, but it does only for a certain angle
   
    var bullet = instance_create_layer(x+x_offset, y+y_offset, "Instances", o_gunBullet);
    bullet.direction = dir + random_range(-recoil, recoil);
    bullet.image_angle = dir;
I don't know how to solve that issue. Sometimes the bullet spawn point is exactly as it should be, when i turn the character (i.e. 90 degrees) then spawn point is moving closer or further from the player.

I realise that the solution is stupidly simple, but at the beginning everything looks like rocket science. Can anyone help? Did I missed some info? Please let me know if I did. It would be much appreciated.
Thank you very much guys
Best Regards
 

jo-thijs

Member
Hello guys, first timer here :)
I'm trying to solve my issue by myself for a while now, but can't figure it out. Not enough experience yet. So, basically I have a sprite from OpenGameArt. My pivot point is in the middle of the baseball cap of that character. I've also "moved" all frames I need from different size of each sprite into one-sized square (don't know if that matters, but just in case I've done it anyway). That makes shooting a little bit different from what I've seen in Shauns or Heartbeats tutorials as they have usually bullet spawn at the end of a gun in the middle of a sprite itself. In my case the gun is part of a player character and is not in straight line from the centre. Also it can't be just flipped like single gun. My attempt to do it is a failure (from my point of view) because the "spawn point" for a bullet is not in the place where it should be (gun barrel) but it moves with player character. Here is the code I've done so far:
Code:
    var dir = point_direction(x, y, mouse_x, mouse_y);
    var x_offset = lengthdir_x(100, dir);
    var y_offset = lengthdir_y(40, dir); // these should put the bullet spawn point
    //at the end of a gun barrel, but it does only for a certain angle
  
    var bullet = instance_create_layer(x+x_offset, y+y_offset, "Instances", o_gunBullet);
    bullet.direction = dir + random_range(-recoil, recoil);
    bullet.image_angle = dir;
I don't know how to solve that issue. Sometimes the bullet spawn point is exactly as it should be, when i turn the character (i.e. 90 degrees) then spawn point is moving closer or further from the player.

I realise that the solution is stupidly simple, but at the beginning everything looks like rocket science. Can anyone help? Did I missed some info? Please let me know if I did. It would be much appreciated.
Thank you very much guys
Best Regards
Hi and welcome to the GMC!

Suppose the bullet should spawn at position (x + dx, y + dy) when the player is facing directly to the right,
then you can use this code:
Code:
    var dir = point_direction(x, y, mouse_x, mouse_y);
    var angle = point_direction(0, 0, dx, dy) + dir;
    var length = point_distance(0, 0, dx, dy);
    
    var bullet = instance_create_layer(x + lengthdir_x(length, angle), y + lengthdir_y(length, angle), "Instances", o_gunBullet);
    bullet.direction = dir + random_range(-recoil, recoil);
    bullet.image_angle = dir;
 
W

woyosensei

Guest
Oh damn, yeah! It works perfectly! Thank you very much! At the beginning I was wondering what dx and dy means, but then I read your answer again and obviously it's my coordinates on sprite itself where the spawn point should be from origin point.
Thank you so much!
 
Top