The angle difference is usually the facing direction subtracted by the target direction: var facingMinusTarget = facingDir - targetDir The problem is that Game Maker changes angles from 359° to 0° (assuming you’re incrementing the angle) and that throws off calculations. This part was personally the hardest for me. Here’s a picture to visualize (turret is objToTurn):Ĭalculating the Difference between Target and Facing Direction var targetDir = point_direction(objToTurn. It takes in the object’s position and the target’s position and returns the direction pointing from the object to target (which we already dubbed “target direction”). This step is relatively easy if you know Game Maker’s point_direction() function. Apply angular rotation towards the target so that the target direction is the facing direction.Calculate the angle difference between the direction derived from step 1 ( let’s call “target direction”) and the turret’s facing direction.Calculate the target’s direction with respect to the turret’s position.There are three steps involved when gradually rotating an object ( let’s call it a turret for now) towards a target object: This only pertains to Game Maker and thus uses the GML language. I figured a tutorial would be necessary for something that isn’t so obvious to implement. I had the basic algorithm down, but because of the way Game Maker handled angles, the turrets would behave strangely when you would hover around the 0°/360° point with respect to the turret. It seems that it wasn’t as simple as I thought it was.
If you move too fast, it should turn towards you gradually, not instantly. I wanted them to rotate towards you given a maximum rotation speed. When I was developing enemies in Yxi, I was trying to get turrets to aim at you, but I didn’t want them to possess “instant aim”.