Auto Facing Sprites (RTS)(GMS)

B

BlackeyeI

Guest
I'm currently making an RTS (Real Time Strategy) game and i'm having trouble knowing how to make the sprites face the left or right in which they're going. You can place an object called goto they will travel to it and stop when they touch it. I would like to know how to make the sprite index change (.e.g. sprite index 0 = left, sprite index 1 = right) when it's going left or right.

Suggestions: Just in case you don't think you know where to start here are some ideas that I think could work.
1# If image_angle is 180o left or right then change to sprite index 0 or 1 (left or right).
2# Somehow tell the object to see if it's physically moving in a direction, if true then change sprite index to 0 or 1 (left or right).
3# Make the unit objects calculate if object goto is on the left or right side of them and then face that way (sprite index to 0 or 1).


NOTE: I don't want them to rotate, just face left or right.

Thank you for having the time to read and think about this question. Thanks :)
 
Try using the built in direction variable.

The code might look something like this: (in the draw event)
Code:
if (direction < 90 && direction >= 270) //If facing within 90 degrees in either direction to the right
{
sprite_index = spr_unit_right;
}
else if (direction >= 90 && direction < 270) //If facing within 90 degrees in either direction to the left
{
sprite_index = spr_unit_left;
}
(I think I did that right, but it was from memory, so maybe not.)
 
B

BlackeyeI

Guest
Try using the built in direction variable.

The code might look something like this: (in the draw event)
Code:
if (direction < 90 && direction >= 270) //If facing within 90 degrees in either direction to the right
{
sprite_index = spr_unit_right;
}
else if (direction >= 90 && direction < 270) //If facing within 90 degrees in either direction to the left
{
sprite_index = spr_unit_left;
}
(I think I did that right, but it was from memory, so maybe not.)
I see where you're gonig with this. It 50% worked.

I can make him change to left but when he goes right he stays left ._______.
I gotta figure this out
 
B

BlackeyeI

Guest
I've probably got some code laying around that doesn't like it :l
 
OH! I just noticed something. Try this instead:
Code:
if (direction < 90 || direction >= 270) //If facing within 90 degrees in either direction to the right
{
sprite_index = spr_unit_right;
}
else if (direction >= 90 && direction < 270) //If facing within 90 degrees in either direction to the left
{
sprite_index = spr_unit_left;
}
With the && at the top, it would never be true. Use the || (or) instead.

Ha ha,
Most of the time I opt for the top down approach so I can just use image_angle = direction. But that doesn't look as good.
 
B

BlackeyeI

Guest
OH! I just noticed something. Try this instead:
Code:
if (direction < 90 || direction >= 270) //If facing within 90 degrees in either direction to the right
{
sprite_index = spr_unit_right;
}
else if (direction >= 90 && direction < 270) //If facing within 90 degrees in either direction to the left
{
sprite_index = spr_unit_left;
}
With the && at the top, it would never be true. Use the || (or) instead.

Ha ha,
Most of the time I opt for the top down approach so I can just use image_angle = direction. But that doesn't look as good.
Congradulations you solved it :D!
Thank you very much woo hoo!!!!!!!
 
Top