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

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!
 
  • Wow
Reactions: Mut
H

Homunculus

Guest
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.
 

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!
 

Slyddar

Member
is there a way to point an object and stretch it? stretch it to the object you are pointing it to?
I would set the origin to be at the bottom center, then use point_direction to set the image_angle to the direction of the object you are wanting to 'aim' at. Then you can increase the image_yscale over time and it will appear to stretch to the target.
 
Top