getting upside down change left to right and vice versa problem

Hi there, Sorry the title seems not very clear because my English is not that good .

I’m new to Unity , and still trying stuff just for test purpose , So My player moves right and left as he supposed to . But when he gets upside down the right becomes left and left becomes the right, so when moving right he goes left and vice versa ; here is my code :

if (Input.touchCount > 0) {
if(Input.GetTouch(0).phase == TouchPhase.Moved)// To know if the player is sliding his/her finger
            {
                if(Input.GetTouch(0).deltaPosition.x > 0) // is player moving right
                transform.Translate(new Vector2(Time.deltaTime *Speed,0));
                else if(Input.GetTouch(0).deltaPosition.x < 0) // is player moving left
                    transform.Translate(new Vector2(- Time.deltaTime *Speed,0));

            }
}

( I remember watching the solution on some tutorial back then but can’t figure it out )

this is not fliping code :smile: try invert (change to opposite) your boolean for flip (if you have one).

I didn’t understand you , the problem here is When the player gets upside down the right becomes left and left becomes right

How are you turning the character? I’m assuming by rotation. If you look at it in the inspector it is probably flipping the character on the z axis which would put the characters right side on the left of the screen & its left side in the right of the screen so that the movement is correct for the character even though it looks wrong to you.
E.g. If you did a handstand by going forward your right & left would be the same. If you went into a handstand by rolling left or right (half a cart wheel) then everything would be reversed to anyone looking.

1 Like

Sorry for misleading , by right and left I meant when player moves right or left ( x axis transformation )

thanks anyway

You need a way to tell when the player is either “upside right” or “upside down”. Perhaps just a bool like “bool isUpsideDown = false” or something like that, and each update, check if the players z rotation has gone beyond the amount needed to be upside down, and if so, set isUpsideDown to true. Then when your applying your motion to the player, if isUpsideDown is true, you need to instead add the opposite direction of X movement!

Hope that all makes sense.

1 Like

I tried that once , but failed
thanks for replying

I found the solution that worked for me credits to ’ Dan Puzey ’ :
From the documentation

If relativeTo is left out or set to Space.Self the movement is applied relative to the transform’s local axes.

You are applying your movement relative to the object you are moving so, when the object is upside down, it’s “left” is the user’s “right.”

transform.Translate(new Vector2(Time.deltaTime *Speed,0), Space.World);
1 Like

Interesting solution! Glad you got it working.

Good luck!

1 Like