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

SOLVED creating an object at the tip of a rotating object

WasabiHeat

Member
I've been learning GMS with Shaun Spalding's platformer tutorial (big props it's some good stuff) but one thing he did kind of irks me and it's that the bullets fired from the player character's gun are created at the origin of the gun instead of at the tip of it. With the way he coded the game the speed of the bullet moving makes it look like it's coming from the front of the gun, but I intentionally made my bullets slower as I wanted them to arc/have gravity, but that means that it looks like this when I shoot:

(The FPS doesn't do it justice, but notice the white muzzle flash appearing on the gun; it's actually part of the bullet and just the first frame of it's animation)

The code used to rotate the gun is super simple, just:
GML:
image_angle = point_direction(x,y,mouse_x,mouse_y);
and the code for creating the bullets is:
Code:
with (instance_create_layer(x,y,"Bullets",oBullet)) {
        speed = 10;
        grv = 0.4;
        vsp = 1;
        direction = other.image_angle + random_range(-3,3);
        image_angle = direction;
    }
As far as i'm concerned I only need to do something to the X & Y parts of the with statement, but i've been scratching my head trying to figure out exactly what to do. Some help would be greatly appreciated!
 

Slyddar

Member
lengthdir_x and lengthdir_y are the functions you want to use.
Try this:
Code:
var _len = 20;
var _x = lengthdir_x(_len, image_angle);
var _y = lengthdir_y(_len, image_angle);
with (instance_create_layer(x + _x, y + _y,"Bullets",oBullet)) {
...
 

WasabiHeat

Member
lengthdir_x and lengthdir_y are the functions you want to use.
Try this:
Code:
var _len = 20;
var _x = lengthdir_x(_len, image_angle);
var _y = lengthdir_y(_len, image_angle);
with (instance_create_layer(x + _x, y + _y,"Bullets",oBullet)) {
...
This worked just as I needed it to, thanks! I guess I have more to learn about lengthdir.
 
Top