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

Discussion in 'Programming' started by Den, Feb 8, 2017.

  1. Den

    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.
     
  2. kamiyasi

    kamiyasi Member

    Joined:
    Jul 22, 2016
    Posts:
    239
    The built-in direction variable is automatically updated for an object following a path. Use image_angle==direction
     
    alexhinton likes this.
  3. Den

    Den Guest

    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.
     
  4. kamiyasi

    kamiyasi Member

    Joined:
    Jul 22, 2016
    Posts:
    239
    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
     
    alexhinton likes this.
  5. Den

    Den Guest

    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
     
  6. RichHopefulComposer

    RichHopefulComposer Member

    Joined:
    Jun 20, 2016
    Posts:
    1,376
    Just edit your sprice so that it's facing right in the editor. Then it'll face the correct direction.
     
  7. Den

    Den Guest

    What do you mean? like make sure the sprite is facing right in the drawing?? because it already is facing right.
     
  8. Gillen82

    Gillen82 Member

    Joined:
    Jan 30, 2017
    Posts:
    199
  9. RichHopefulComposer

    RichHopefulComposer Member

    Joined:
    Jun 20, 2016
    Posts:
    1,376
    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.
     
  10. HayManMarc

    HayManMarc Member

    Joined:
    Jun 21, 2016
    Posts:
    890
    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.
     
    RichHopefulComposer likes this.
  11. RichHopefulComposer

    RichHopefulComposer Member

    Joined:
    Jun 20, 2016
    Posts:
    1,376
    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?
     
  12. HayManMarc

    HayManMarc Member

    Joined:
    Jun 21, 2016
    Posts:
    890
    Only if he's using the 'speed' variable. If he's using pixel increments, he might have to use an x_previous variable.
     
  13. Den

    Den Guest

    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)
     
  14. Den

    Den Guest

    Yeah I tried that it doesn't seem to work for me. Thanks anyways though
     
  15. Gillen82

    Gillen82 Member

    Joined:
    Jan 30, 2017
    Posts:
    199
    Here's the download if you want to have a look at it. Working for me lol

    http://bit.ly/2knbhcR
     
  16. Den

    Den Guest

    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
     
  17. Gillen82

    Gillen82 Member

    Joined:
    Jan 30, 2017
    Posts:
    199
  18. Den

    Den 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
     
  19. Gillen82

    Gillen82 Member

    Joined:
    Jan 30, 2017
    Posts:
    199
    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
     
  20. Den

    Den 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
     
  21. Gillen82

    Gillen82 Member

    Joined:
    Jan 30, 2017
    Posts:
    199
    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: Feb 8, 2017
  22. Never Mind

    Never Mind Member

    Joined:
    Oct 17, 2016
    Posts:
    212
    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: Feb 9, 2017
    Den and HayManMarc like this.
  23. Den

    Den Guest

    Oh ok man that makes sense, I'll try figure it out hopefully.
     
    Gillen82 likes this.
  24. Den

    Den 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:
     
  25. Gillen82

    Gillen82 Member

    Joined:
    Jan 30, 2017
    Posts:
    199
    Can you post your code and I'll have a look at it for you?
     
  26. HayManMarc

    HayManMarc Member

    Joined:
    Jun 21, 2016
    Posts:
    890
    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.
     
  27. Den

    Den Guest

    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
     
  28. xDGameStudios

    xDGameStudios Member

    Joined:
    Sep 15, 2016
    Posts:
    579
    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.
     
  29. Wraithious

    Wraithious Member

    Joined:
    Jun 24, 2016
    Posts:
    1,166
    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;
     
  30. Den

    Den 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?
     
  31. Never Mind

    Never Mind Member

    Joined:
    Oct 17, 2016
    Posts:
    212
    Try this
    ------------------------------
    if( abs(angle_difference(180, direction)) < 90){
    image_xscale = -1;
    }else{
    image_xscale = 1;
    }
    ------------------------------
     
  32. Den

    Den Guest

    Hey man, thanks I got the angle difference code to work! AT LAST!! haha
    Thanks so much !
     
  33. Wraithious

    Wraithious Member

    Joined:
    Jun 24, 2016
    Posts:
    1,166
    You can add a sub image to your sprite that's upside down and if direction is between 90 and 270 show that image
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice