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

having multiple origin points

K

krazi

Guest
Hi,

I'm using the drag and drop feature in GM2 and I have a top view plane with multiple guns on it. How can I have multiple orgin points so that my bullets will be coming out from different areas of the plane?

Thanks,

krazi
 
You'll need to write some code I fear, I don't see any equivalent Drag n Drop actions.

You'll want to setup some x and y offsets which represent the locations of the guns relative to the sprite origin of your plane.

So if your plane's sprite origin is at the middle center of your sprite, and you want to setup a gun on the wing, work out the distance in x and y to the location of the gun.

1583950834974.png

Red Plus = sprite origin
Green Plus = gun location
Offset x = -3
Offset y = -10 (I wrote 10 on the image by mistake)

You can the calculate the position of the gun in the room by using lengthdir_x() and lengthdir_y() functions.

Plane Create Event:
Code:
gun_offset_x = -3;
gun_offset_y = -10;
gun_angle_offset = point_direction(x, y, gun_offset_x, gun_offset_y);
gun_distance_offset = point_distance(x, y, gun_offset_x, gun_offset_y);
Plane Step Event:
Code:
gun_x = x + lengthdir_x(gun_distance_offset, gun_angle_offset - direction);
gun_y = y + lengthdir_y(gun_distance_offset, gun_angle_offset - direction);

// Now we have the guns actual location to the plane, we can create bullets at this location
var _bullet = instance_create_layer(gun_x, gun_y, "Instances", obj_bullet);
_bullet.direction = direction; // Fire the bullet in the direction the plane is facing
_bullet.image_angle = direction // Make sure the bullet sprite image also points in the correct direction
_bullet.speed = 10;
This is just an example for one gun. If you have 5 guns, you probably don't want to create separate variables for every position, so you would want to store the x and y offsets in an array
Code:
gun_offsets = [ -3, -10, -3, 10, -15, 0 ]; // Three guns - two on the wings and one at the rear of the plane

gun_angle_offsets[0] =  point_direction(x, y, gun_offsets[0,0] , gun_offsets[0,1]);
gun_distance_offsets[0] = point_distance(x, y, gun_offsets[0,0], gun_offsets[0,1]);

// and so on for the rest of the guns...
And then in the step event you would use the array values instead of the hard coded variables as well.

You would place all this in a Drag n Drop Execute Code actions.

There is another thread with further examples and solutions for this kind of situation you can read up on.
 
Top