Single line Flipping

As we already know GetAxis() returns value betwen -1 and 1, so why not use it?
There’s no need for separate method!

private void Run() {
    float moveDirection = CrossPlatformInputManager.GetAxis("Horizontal"); // value between -1 (left) and 1 (right)       
    myRigidBody.velocity = new Vector2(moveDirection * movingSpeed, myRigidBody.velocity.y); //multiply movement "direction" by speed
    transform.localScale = new Vector2(Mathf.Sign(moveDirection), 1f); // use direction to flip the sprite
}

Hi Serg, it’s because if we use a gamepad, horizontal move will use analog stick, wich will give to the variable moveDirection amounts between -1 and 1 with all possible numbers after the comas, like 0.256f for example. We want to flip the sprite even if we are moving very slow with the analog stick. Your example works perfectly with the keyboard though :slight_smile:

Privacy & Terms