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

adding an item that moves with a character

T

teamrocketboyz

Guest
Hello all, im currently working on a project and i need a little help regarding an object that i want to move with the player and stay in the exact same position.

so far i have an object and upon the create event i have added

Code:
instance_create_layer(x,y,"instances_turrets",o_gun)
this creates the gun exactly in the center of the enemy sprite and this is fine.

within the gun object i have added

Code:
move_snap(o_enemy.x,o_enemy.y)
this makes the gun follow the enemy perfectly. the problem is that if i add another o_enemy then that enemy doesnt have a gun i think its snapping 2 guns to the first enemy. how can i make it so that it only snaps to the object that created it?
 

Kyon

Member
Hello all, im currently working on a project and i need a little help regarding an object that i want to move with the player and stay in the exact same position.

so far i have an object and upon the create event i have added

Code:
instance_create_layer(x,y,"instances_turrets",o_gun)
this creates the gun exactly in the center of the enemy sprite and this is fine.

within the gun object i have added

Code:
move_snap(o_enemy.x,o_enemy.y)
this makes the gun follow the enemy perfectly. the problem is that if i add another o_enemy then that enemy doesnt have a gun i think its snapping 2 guns to the first enemy. how can i make it so that it only snaps to the object that created it?
Yes you're exactly right, because move_snap(o_enemy), all object gun snap to the first created enemy in the room.
Try this instead:

CREATE:
Code:
mygun=instance_create_layer(x,y,"instances_turrets",o_gun); //make the gun a variable
STEP (or END STEP):
Code:
mygun.x=x; mygun.y=y;

So now your enemy has his own gun variable, and sticks that created gun to his own x and y positions.
Make sure you remove the move_snap in the gun object. :)
 

Slyddar

Member
The gun needs to know which enemy to follow.
In the enemy create where you are creating the gun you can let it know who to follow.
Code:
var  inst = instance_create_layer(x,y,"instances_turrets",o_gun);
//set the instance to follow
inst.follow = id;
I've never used move_snap like that, so feel free to try it, but I'd just assign the gun to the enemy like this, (in the gun objects step)
Code:
with(follow) {
  other.x = x;
  other.y = y;
}
 
Top