Different way to flip player

You could also set the X scale value to 1 or -1 (something I tried before watching this video) but your flip horizontal is easier.

UPDATE: I simplified your code for Flip() to make it more readable:
private void Flip(){
if(direction.X < 0){Sprite3DNode.FlipH=true;}
else if(direction.X > 0){Sprite3DNode.FlipH=false;}
}

Here is a more simplified version of Flip()

private void Flip()
{
    if(Velocity.X != 0)
         spriteNode.FlipH = Velocity.X<0;
 }
1 Like

Yeah, that is simpler, but I used direction instead of Velocity because direction does not need to be continuously polled in the Physics Process function it only needs to change when the input changes.

Beep boop:

private void Flip()
{
    _spriteNode.FlipH = _spriteNode.FlipH ? _direction.X <= 0 :  _spriteNode.FlipH = _direction.X < 0;
}

(For the uninitiated, thats a ternary conditional operator and it roughly translates to "if _spriteNode.FlipH is true then set its value to true if direction.X <= 0 otherwise set it tru if direction.X is < 0. If you map out the boolean space, you’ll see it works every time… I’m using _ for private variables as per usual C# linting standard)

I’m sure ternary operators are great I just never felt the need to use them because I can accomplish the same thing (and more) with if else statements. But that’s just me.

ternary operators are super handy, thanks for this!

Would it be okay to do the flip in the input method as shown below instead of having a flip method that is called for each frame?

 public override void _Input(InputEvent @event)
    {
        direction = Input.GetVector(GameConstants.INPUT_MOVE_LEFT, GameConstants.INPUT_MOVE_RIGHT,
                                    GameConstants.INPUT_MOVE_FORWARD, GameConstants.INPUT_MOVE_BACKWARD);

        if (direction == Vector2.Zero)
        {
            animPlayerNode.Play(GameConstants.ANIM_IDLE);
        }
        else
        {
            animPlayerNode.Play(GameConstants.ANIM_MOVE);

            // flip the sprite or put back to normal based on which action was done
            this.sprite3D.FlipH = Input.IsActionPressed(GameConstants.INPUT_MOVE_LEFT);                      
        }           
    }

Well I see my solution doesn’t work with the changes coming up.

Experimentation is Never going to be a bad thing, I’m glad you tried it and later discovered how it didn’t work. It’s great for learning. Good on you bigblue

1 Like

Privacy & Terms