I need help with a tutorial

I am trying to finish the Walking Astronaut tutorial and I do not understand this step

if self.dir.x > 0 then                                     -- [2]
        anim = hash("right")
    elseif self.dir.x < 0 then
        anim = hash("left")
    elseif self.dir.y > 0 then
        anim = hash("back")
    elseif self.dir.y < 0 then
        anim = hash("front")
    end

“Test against the movement direction and set the anim variable depending on the value of the X and Y component in the direction vector.”

I’m not sure what they mean by “test against the movement directions” please help me :frowning:

Self.dir is a vector with values indicating in which direction the astronaut is moving. X value meaning horizontal movement and y value meaning vertical movement. Positive x value is right, negative is left. Positive y value is up and negative is down.

What the snippet of code does is to check the current direction of movement and playing the correct animation.

1 Like

This line essentially means “depending on the movement direction, we want to play different animations. Therefore, we test which movement direction we currently have, which we can find by checking if we are moving forwards (Y > 0), backwards (Y < 0), left (X < 0) or right (X>0), we then play a different animation.”

Does that make it clearer?

1 Like

If you want to dive into the vector stuff mentioned in the answers above we you can take a look at the movement tutorial that covers that.

2 Likes