GML How to make an object face another object?

Alexir

Member
I know this is an insanely easy thing to do, but I can not, for the life of me, figure it out. I'm fairly new to Gamemaker studio 2, and I don't know how to make an object face towards another object constantly, while they move. I have a player character, and an enemy who is holding a weapon, and I want that enemies weapon to constantly be pointing towards the player character, while both the enemy and the player character move around. I don't believe attaching my current code will do any help, considering I have barely any that could be useful. Please help!
 

Homunculus

Member
you need to set the image_angle variable in the enemy object to an angle relative to the player. You can do that using point_direction(x1, y1, x2, y2).
Code:
//step
image_angle = point_direction(x, y, obj_player.x, obj_player.y);
Please note that your enemy sprite should be facing right for this to work properly, since right is considered to be 0°. Moreover, you are just changing how the sprite is rotated, not the actual direction of movement of the enemy.
 

Slyddar

Member
If it's a 2d platformer you can use this in the enemies step event, if both characters have their sprites facing right.
Code:
image_xscale = sign(o_player.x - x);
 

Alexir

Member
you need to set the image_angle variable in the enemy object to an angle relative to the player. You can do that using point_direction(x1, y1, x2, y2).
Code:
//step
image_angle = point_direction(x, y, obj_player.x, obj_player.y);
Please note that your enemy sprite should be facing right for this to work properly, since right is considered to be 0°. Moreover, you are just changing how the sprite is rotated, not the actual direction of movement of the enemy.
Awesome, it worked, thank you!
 
Top