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

SOLVED Turn character to a target direction taking the closest side.

Gasil

Member
Hello, would you help me with this please? With top view, the character has tank movement, so it rotates with left and right to change directions and goes forward with up, etc. With a script, I'm trying to make it rotate to a given target to face it upon examination, but with the way I have set up things, the character will always rotate counterclockwise, so sometimes it looks silly taking the longest turn to face the target.

On step event I have this:

GML:
if(dir >= 360)
    dir -= 360;
else if(dir < 0)
    dir += 360;
This will keep dir from positive 359 to 0. I did this to prevent dir to increase or decrease indefinitely if character rotates too much to a single direction, I thought it could make things more easier to code in direction related stuff, but I'm not sure if it's necessary.

GML:
    var trgtDir, turnSpd;
    trgtDir = argument0;
    turnSpd = argument1;
  
    with(obj_player)
    { 
        //Turn player object.
        dir = lerp(dir, trgtDir, turnSpd);
    }
This is as much as I could write in the script. If player dir is 15, and target dir is 350, naturally I'd want the character to decrease dir to 0 and further so it wraps around with the step event code and reach 350, cause it's the closest route, but currently it will go all the way from 15 to 350. I'm not sure how to accomplish that dinamically or what values to compare to give it a negative turnSpd when necessary.


Thanks.
 

Gasil

Member
I don't suppose you're familiar with the angle_difference function? It's exactly what you want.
Alright, I tried to use it with lerp and I sucked at it but the manual's example does exactly what I needed using min function instead. After some fiddling around I'll eventually figure it out, thanks a lot for your help.
 
Top