Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

getting upside down change left to right and vice versa problem

Discussion in '2D' started by Flava_Sava, Sep 10, 2015.

  1. Flava_Sava

    Flava_Sava

    Joined:
    Aug 1, 2015
    Posts:
    12
    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 :
    Code (CSharp):
    1. if (Input.touchCount > 0) {
    2. if(Input.GetTouch(0).phase == TouchPhase.Moved)// To know if the player is sliding his/her finger
    3.             {
    4.                 if(Input.GetTouch(0).deltaPosition.x > 0) // is player moving right
    5.                 transform.Translate(new Vector2(Time.deltaTime *Speed,0));
    6.                 else if(Input.GetTouch(0).deltaPosition.x < 0) // is player moving left
    7.                     transform.Translate(new Vector2(- Time.deltaTime *Speed,0));
    8.  
    9.             }
    10. }
    ( I remember watching the solution on some tutorial back then but can't figure it out )
     
    Last edited: Sep 11, 2015
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    this is not fliping code :D try invert (change to opposite) your boolean for flip (if you have one).
     
  3. Flava_Sava

    Flava_Sava

    Joined:
    Aug 1, 2015
    Posts:
    12
    I didn't understand you , the problem here is When the player gets upside down the right becomes left and left becomes right
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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.
     
    vakabaka likes this.
  5. Flava_Sava

    Flava_Sava

    Joined:
    Aug 1, 2015
    Posts:
    12
    Sorry for misleading , by right and left I meant when player moves right or left ( x axis transformation )

    thanks anyway
     
  6. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    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.
     
    theANMATOR2b likes this.
  7. Flava_Sava

    Flava_Sava

    Joined:
    Aug 1, 2015
    Posts:
    12
    I tried that once , but failed
    thanks for replying
     
  8. Flava_Sava

    Flava_Sava

    Joined:
    Aug 1, 2015
    Posts:
    12
    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."

    Code (CSharp):
    1. transform.Translate(new Vector2(Time.deltaTime *Speed,0), Space.World);
     
    theANMATOR2b likes this.
  9. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    Interesting solution! Glad you got it working.

    Good luck!
     
    Deleted User likes this.