Legacy GM [SOLVED] Weapon's barrel face the moving enemy

B

Barack Pálinka

Guest
Hey guys,in game maker i've created a weapon and an enemy objecct and i want the weapon to face the moving enemy,so basically i want the weapon's barrel to face the enemy.

I have tried it with this code:
if(instance_nearest(x, y, obj_enemy)) {
image_angle = point_direction(x, y, obj_enemy.x, obj_enemy.y);
}

Thanks in advance.
 

Jakylgamer

Member
instance_nearest() will return and id so you can use it like this
Code:
//first make sure the enemy exists to prevent errors
if instance_exists(obj_enemy) {
   //assign a local variable to the nearest enemy
   var nearest = instance_nearest(x,y,obj_enemy);
   image_angle = point_direction(x,y,nearest.x,nearest.y);
}
 
B

Barack Pálinka

Guest
instance_nearest() will return and id so you can use it like this
Code:
//first make sure the enemy exists to prevent errors
if instance_exists(obj_enemy) {
   //assign a local variable to the nearest enemy
   var nearest = instance_nearest(x,y,obj_enemy);
   image_angle = point_direction(x,y,nearest.x,nearest.y);
}
Thanks,but it's still not facing the enemy,the weapon turns to the enemy's direction really slowly but not facing it directly.
The red square's are the enemies,and the tank is the weapon.
 

Attachments

Alexx

Member
Which direction is the default sprite pointing?
Can you provide a screenshot of this sprite asset?

Your default sprite's direction may be the issue here. It should be pointing right.

edit:ninja'd by NicoFIDI
 
B

Barack Pálinka

Guest
for this code to work your sprite should have the tank gun pointing to the right ( -> ) in the sprite image.
Thanks man,that was the problem :D i rotated the sprite to the right and it works :D
 
Top