(SOLVED)How can I make an object face the right direction when on a path?

D

Den

Guest
Literally been trying to do this for like 3 weeks now lol I just moved on with other parts of my game haha
I really need to do this now though, I have tried everything I could think of, I have tried using xprevious, image_scale, path_speed. I dunno what else to try, could someone please help me out.
 
D

Den

Guest
The built-in direction variable is automatically updated for an object following a path. Use image_angle==direction
I have already tried this it just makes the object go upside down or move with the sprite in a vertical position. I have this at the min

if(path_spd == 1) {
image_xscale = -1;
}else {
image_xscale = 1;
}

but the issue with this is it's only changes when the object stops so it works but looks a bit clunky.
 

kamiyasi

Member
I have already tried this it just makes the object go upside down or move with the sprite in a vertical position.
Does the sprite correctly rotate with the path though? If it does but is just facing the wrong way, add or subtract 90 degrees such as image_angle == direction+90
 
D

Den

Guest
Does the sprite correctly rotate with the path though?
It does yes, and yeah i'm trying to get the correct value. When image_angle just = direction, the sprite is upside down but facing the right way.
I have tried taking away 90 and 180 but then it faces the wrong way XD guess I just need to play with some values lol dunno what other values I can try tbh
 
What do you mean? like make sure the sprite is facing right in the drawing?? because it already is facing right.
Yes, just make the drawing face right in your image file. 0 degrees = right in gm. 90 = up. If your image is facing right, you shouldn't have to add or subtract from its direction to "fix" the way the sprite is pointing on screen.
 
It kinda sounds like you want this for a platformer (or side-on) and everyone is giving you top-down advice. You may need to provide more information about your project.
Yeah, just noticed this was for a platformer. I thought it was top down, since he was using paths, lol.

Can't he just check whether hspeed is over zero?
 
D

Den

Guest
Yeah, just noticed this was for a platformer.
It kinda sounds like you want this for a platformer
Yeah my game is a side scroller (sorry should have said that) I have already tried hpeed, it doesn't work. I have been able to get the enemy facing the right way using:
Code:
if(path_spd == 1) {
   image_xscale = -1
}else {
  image_xscale = 1
}
but this way looks clunky coz it only changes when it stops (path_spd is a variable I made for the enemy's movement speed)
 
D

Den

Guest
Here's the download if you want to have a look at it. Working for me lol
Yeah sure thank you, i'll have a look. Edit: I can't open it XD ffs I swear something just doesn't want me to fix this issue hahah
 
G

Gillen82

Guest
That's what i'm looking for! why can't mine do that lol there must be something where it shouldn't be somewhere
That's the code from the previous thread. I copied and pasted to make sure I didn't make any errors. Double-check the name of the path within the code matches the path that you have created
 
D

Den

Guest
That's the code from the previous thread. I copied and pasted to make sure I didn't make any errors. Double-check the name of the path within the code matches the path that you have created
I'm making progress at last XD the code is more or less working now but it's switching the image_xscale every couple of seconds (The sprite keeps changing direction like every 2 or so seconds haha. Guess I need to tweak the timer a bit lol
 
G

Gillen82

Guest
I'm making progress at last XD the code is more or less working now but it's switching the image_xscale every couple of seconds (The sprite keeps changing direction like every 2 or so seconds haha. Guess I need to tweak the timer a bit lol
I just tested it again, changing both the length of the path and the player's speed. There shouldn't be any need to adjust the timer as it will calculate the right distance/time to turn. Her's an update from the last one to show this.

https://www.dropbox.com/s/c0q5rm0kfzmxtm2/Path.exe?dl=0

It's just 3 of the same object following the same path, but picking random speeds. But you will notiice that the turn timer for each is relevant to the distance that each has to travel.

Make sure there is no other code somewhere else that could be interfering with it. But on the plus side, you're getting there lol. If you need help with anything else give me a shout!!
 
Last edited by a moderator:
N

Never Mind

Guest
Not sure if this is what your trying to do, or if you still need help (skimmed this thread). I just decided to post some code:
------------------------------
if( abs(angle_difference(180, direction)) < 90){
image_xscale = -1;
}else{
image_xscale = 1;
}
------------------------------

- OR -

As HayManMarc said you can also do this by finding / comparing with the previous x location.

- OR -

You could also just use the built in speed variable as well. That way you can take a quick look at hspeed to determine which way to face:
------------------------------
if( hspeed < 0 ){
image_xscale = -1;
}else{
image_xscale = 1;
}
------------------------------

If this isn't what your looking for, please ignore.

EDIT - fixed stupid mistake (accidentally wrote yscale instead of xscale in the else's)
 
Last edited by a moderator:
D

Den

Guest
I just tested it again, changing both the length of the path and the player's speed. There shouldn't be any need to adjust the timer as it will calculate the right distance/time to turn. Her's an update from the last one to show this.
Oh ok man that makes sense, I'll try figure it out hopefully.
 
D

Den

Guest
give me a shout!!
Literally have no idea how to sort it anymore lol been at it way to long now, starting to think if I should just scrap paths and find another way to set up a decent patrol system :bash:
 
G

Gillen82

Guest
Literally have no idea how to sort it anymore lol been at it way to long now, starting to think if I should just scrap paths and find another way to set up a decent patrol system :bash:
Can you post your code and I'll have a look at it for you?
 

HayManMarc

Member
I would change sprite facings by using either Never Mind 's angle_difference method, or use x_previous and then compare it to your current x position.
 
D

Den

Guest
Can you post your code and I'll have a look at it for you?
Code hasn't really changed since the last time I showed you man. It's like the image xscale is just changing every couple of secs, it's facing the correct direction lol that's why I thought it was the timer lol coz there is no other code effect the image_xscale
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
The xprevious thing didn't work because GMS updates this value before step event (I also had problems before).... try with xprevious... and put the code in the begin step (I think it is here the right place) or the end step.
 
W

Wraithious

Guest
The easiest way is to make sure when you make your sprite that the first sub image of your sprite is facing to the right, then use image_angle = direction;
 
D

Den

Guest
The easiest way is to make sure when you make your sprite that the first sub image of your sprite is facing to the right, then use image_angle = direction;
Hey I have tried this and it works it's just when the enemy is moving left it flips upside down lol. Know of anyway to fix this?
 
N

Never Mind

Guest
Try this
------------------------------
if( abs(angle_difference(180, direction)) < 90){
image_xscale = -1;
}else{
image_xscale = 1;
}
------------------------------
 
D

Den

Guest
Try this
------------------------------
if( abs(angle_difference(180, direction)) < 90){
image_xscale = -1;
}else{
image_xscale = 1;
}
------------------------------
Hey man, thanks I got the angle difference code to work! AT LAST!! haha
Thanks so much !
 
W

Wraithious

Guest
Hey I have tried this and it works it's just when the enemy is moving left it flips upside down lol. Know of anyway to fix this?
You can add a sub image to your sprite that's upside down and if direction is between 90 and 270 show that image
 

Japster

Member
@kamiyasi - Man! - I have tried LOADS of stuff to get my snake-like enemy's segments rotating with path, but nothing worked... image_angle just wasn't changing....

Then I spotted your golden piece of advice about "direction" during a sprite following a path ("The built-in direction variable is automatically updated for an object following a path. Use image_angle = direction ") - worked like a charm! - Necro'ing a thread I guess, but hey, credit where it's due! :D

Thanks!
 
Top