1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Skidding animation

Discussion in 'Programming' started by Joltout, Dec 1, 2019.

  1. Joltout

    Joltout Member

    Joined:
    Oct 3, 2018
    Posts:
    16
    So basically, I'm trying to get my character to slide in their skidding animation/image_index to a stop when either no directional keys are pressed (or both at the same time), or when the player changes direction and their momentum is still carried over (for example, sliding them right when they're trying to move left).

    And while I have the characters movement down fine, getting the animations for the sprite to work right is proving to be a pain in my rear. I've tried multiple ways of coding around it, but me being the newbie I am, I haven't found a solution. As far as I can understand, the code I've written prioritises the right side movement because it comes first in the code, so the animation works fine on the right, but when I move left, the character is just permanently in the sliding animation.

    Any help? :(


    Code:
    if (place_meeting(x,y+1,oWall))
    {
        if ((!hsp == 0) and (!key_right or !key_left))
        {
            sprite_index = N_Skid;
            image_index = 1;
        }
    }
    
     
  2. BB Scary

    BB Scary Member

    Joined:
    Oct 18, 2019
    Posts:
    2
    try small change
    if ((!hsp == 0) and (!key_right and !key_left))
     
  3. Joltout

    Joltout Member

    Joined:
    Oct 3, 2018
    Posts:
    16
    I tried that, and all that's changed is that now instead of sliding when moving left, my character just runs normally and doesn't slide at all

    I really don't know what I'm doing wrong
     
  4. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    996
    I don't think your intention is to invert hsp, but to check if it's not equal to 0.

    hsp != 0
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    !hsp == 0 is only true for moving right
    hsp != 0 is true for both left and right movement


    @OP Of you had something like
    Code:
    move = right-left;
    hsp += move*accel
    then you could check if sign(move) != sign(hsp).
     
  6. Joltout

    Joltout Member

    Joined:
    Oct 3, 2018
    Posts:
    16
    Code:
    if (place_meeting(x,y+1,oWall))
    {
        if sign(move) != sign(hsp)
        {
            sprite_index = N_Skid;
            image_index = 1;
           if (hsp == 0)
           {
               image_speed = 1;
               sprite_index = N_Idle;
           }
        }
    }
    
    That kind of worked, but the character is now stuck in an image_index = 1 idle sprite situation. Not sure how to get it back to an animation instead of a sprite lock, especially since I called the hsp == 0 index/speed earlier in its own block
     
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    Usually, your hsp isn't quite 0. What about checking if abs(hsp) is less than your acceleration speed? How are you accelerating/decelerating?
     
  8. Joltout

    Joltout Member

    Joined:
    Oct 3, 2018
    Posts:
    16
    Code:
    if (hsp < max_hsp) and (hsp > -max_hsp)
    {
        hsp += (move * movespeed);
    }
    This is basically my acceleration, where max_hsp = 3, move = key_left + key_right and movespeed = 0.2
    There's more to it than this obviously but that's the basics.


    Code:
    if (hsp > 0) && (key_left = 0) && (key_right = 0) && (place_meeting(x,y+1,oWall)) {hsp -= movespeed}
    if (hsp > max_hsp) && (key_left = 0) && (key_right = 0) && (place_meeting(x,y+1,oWall)) {hsp -= movespeed}
    The deceleration is basically a lot of if statements like these, alternating values depending on what keys are pressed, if !place_meeting, etc. A large wall of if statements in my code, seems messy and I'm sure there's a better way around it, but it works so I've kept it.

    All of that aside, I get the feeling like I could be super direct about it and spam if statements again, but again, that feels like bad practice and could cause a lot of problems and tripping up in the future
     
  9. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    if abs(hsp) < max_hsp


    The 0.2 is likely causing a floating point error. If you change it to 0.25, your code "if hsp==0" code should work. In either case, as I said, you should try using "if abs(hsp) < movespeed" instead of "if hsp==0" for that reason.


    Code:
    if abs(hsp) < max_hsp && place_meeting(x,y+1,oWall) {
        if move==0 {
            hsp -= movespeed*sign(hsp);} else {hsp += move*movespeed;
        }
     }
     
    Joltout likes this.
  10. Joltout

    Joltout Member

    Joined:
    Oct 3, 2018
    Posts:
    16
    Wait. So does that mean that I can't change the values of the variables without having a floating point error? Is there a way to fix this or revert it back down to the correct values by force? The values I have at the moment aren't set in stone, and any changes I do now just give off the previous problems as well (setting movespeed to 0.25 did fix it)

    Edit: I've found a weird fix for the floating point error. Apparently, the value can equal -0. So I just created a separate block of code that's literally just this to fix the problem:


    Code:
    if hsp = -0
    {
        hsp = 0;
    }
     
    Last edited: Dec 2, 2019
  11. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    Did you try the abs(hsp)<movespeed suggestion I gave you?
    Be sure you test your hsp=-0 code with a bunch of other "divisible" speeds. Try a speed of 1/3. Try a speed of 3/13. Try a speed of 3/14.
     
  12. Joltout

    Joltout Member

    Joined:
    Oct 3, 2018
    Posts:
    16
    Code:
    if abs(hsp) < max_hsp
    I did replace my code with this, yea. But the other bit didn't really work, since I'm assuming you just based it off of the 2 lines I gave and not the other, almost 20 lines, of similar if statements that're basically all the same aside from one variable changing each time.

    And yea, the divisible speeds don't work unless I change the max_hsp variable to a divisible number of the changed movespeed. But otherwise, it all seems to work fine now
     

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