Search Unity

2D Character Flipping Problem

Discussion in '2D' started by devonjacobsen, Feb 14, 2016.

  1. devonjacobsen

    devonjacobsen

    Joined:
    Nov 12, 2015
    Posts:
    30
    Hey, I'm making a 2D Platformer game, and what I need it to do is when I press down a key once, it flips the character (in this case a rectangle), onto its side, (which I have figured out), and then when I press it again it'll flip back into a standing position. (The problem) Any help is much appreciated!
    Code (CSharp):
    1. // Flip
    2.  
    3.         if (Input.GetKey(KeyCode.F))
    4.         {
    5.  
    6.             GetComponent<Transform>().eulerAngles = new Vector3( 0, 0, 0);
    7.  
    8.      
    9.         }
    10.         else if (Input.GetKeyDown(KeyCode.F))
    11.         {
    12.  
    13.             GetComponent<Transform>().eulerAngles = new Vector3( 0, 0, 90);
    14.    
    15.         }
    16.     }
     
    Last edited: Feb 14, 2016
  2. Q-ro

    Q-ro

    Joined:
    Feb 14, 2016
    Posts:
    10
    What you need to do is set up a flag (a bool) variable that stores if your character is currently standing up or not, in other words

    Code (CSharp):
    1. bool _isStanding = true; //I assume your character starts in a upright position
    then on your code you do the following :


    Code (CSharp):
    1.  
    2. if (Input.GetKey(KeyCode.F))
    3. {
    4. if(_isStanding)
    5. {
    6. GetComponent<Transform>().eulerAngles = newVector3( 0, 0, 0);
    7. _isStanding = false;
    8. }
    9. else
    10. {
    11. GetComponent<Transform>().eulerAngles = new Vector3( 0, 0, 90);
    12. _isStanding = true;
    13. }
    14. }
    15.  
     
    Last edited: Feb 16, 2016
  3. devonjacobsen

    devonjacobsen

    Joined:
    Nov 12, 2015
    Posts:
    30

    Thanks for the help! I put in the code, but the character, (again a rectangle), flips onto its side once I press F once, but then still wont flip back when I press F again, any more tips? I've been struggling with this all day! I previously used a rotation, but it wasn't a snap to that position, and for this game, that's something I need. Thanks again for the help!
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    So now, what you want to happen is you press the F key, and the character turns 90 degrees and sort of lays down, then you hit F again, and they return to a standing position?

    If so, sort of like Q-ro said, you need to differentiate between the press from when the character is standing, and laying down. So keep a bool, and if that bool is true do one type of motion, if its false, do another type of motion.

    Code (csharp):
    1.  
    2. // inside of the Update() method/function
    3. if(Input.GetKeyDown(Keycode.F))
    4. {
    5.   if(_isStanding) // this is a bool variable
    6.   {
    7.     GetComponent<Transform>().eulerAngles = new Vector3( 0, 0, 90);
    8.     _isStanding = false;
    9.   }
    10.   else
    11.   {
    12.     GetComponent<Transform>().eulerAngles = new Vector3( 0, 0, 0);
    13.     _isStanding = true;
    14.   }
    15. }
    16.  
     
  5. devonjacobsen

    devonjacobsen

    Joined:
    Nov 12, 2015
    Posts:
    30


    Ok so, I used the code, but the character still does not flip back when I press F again. Basically what happens, is the characters Z starts off at 90, then when you press F, goes to 0 (aka laying down). And then what I want is for it to basically go to back to normal on another press of F, but that part still isnt working. Here's my code, when the number were as above the character didn't flip at all, but when I reversed them he flipped onto his side on one press. Again, the second press still does nothing.

    Code (CSharp):
    1. // Flip
    2.  
    3.         bool _isStanding = true;
    4.  
    5.         if (Input.GetKeyDown(KeyCode.F))
    6.         {
    7.  
    8.             if(_isStanding)
    9.             {
    10.                 GetComponent<Transform>().eulerAngles = new Vector3( 0, 0, 0);
    11.                 _isStanding = false;
    12.             }
    13.             else
    14.             {
    15.                 GetComponent<Transform>().eulerAngles = new Vector3(0, 0, 90);
    16.                 _isStanding = true;
    17.             }
    18.        }
     
  6. devonjacobsen

    devonjacobsen

    Joined:
    Nov 12, 2015
    Posts:
    30

    Oh, and it's in the update, in case you were wondering.
     
  7. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Ok. Try sticking a debug statement in there to see what the presses are doing - if they are infact trying to flip it when pressing the key the second time.

    Something like this:
    Code (CSharp):
    1. bool _isStanding = true;
    2.         if (Input.GetKeyDown(KeyCode.F))
    3.         {
    4.             if(_isStanding)
    5.             {
    6.                 GetComponent<Transform>().eulerAngles = new Vector3( 0, 0, 0);
    7.                 _isStanding = false;
    8.                Debug.Log("1");
    9.             }
    10.             else
    11.             {
    12.                 GetComponent<Transform>().eulerAngles = new Vector3(0, 0, 90);
    13.                 _isStanding = true;
    14.                Debug.Log("2");
    15.             }
    16.        }
    And lets see if it actually seems to be working as far as that goes, which it should do a console print of "1" and on the second press "2", and if that works, maybe debug the transform like this:

    Code (CSharp):
    1. bool _isStanding = true;
    2.  
    3.         if (Input.GetKeyDown(KeyCode.F))
    4.         {
    5.  
    6.             if(_isStanding)
    7.             {
    8.                 GetComponent<Transform>().eulerAngles = new Vector3( 0, 0, 0);
    9.                 _isStanding = false;
    10.                Debug.Log("New Pos 1: " + GetComponent<Transform>().eulerAngles);
    11.             }
    12.             else
    13.             {
    14.                 GetComponent<Transform>().eulerAngles = new Vector3(0, 0, 90);
    15.                 _isStanding = true;
    16.                Debug.Log("New Pos 2: " + GetComponent<Transform>().eulerAngles);
    17.             }
    18.        }
     
  8. devonjacobsen

    devonjacobsen

    Joined:
    Nov 12, 2015
    Posts:
    30

    I did the debug, and it's only showing 1's on both presses.
     
  9. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    So it seems the script is only getting to the first section. Could you paste the whole update method where this takes place just so I can see it fully? I figure it must be just a little typo somewhere or something...

    EDIT: Oh something occured to me - your not setting that bool inside of the update are you? The bool you should create and assign at the beginning of the script, and not within the update method, or each update it will be reset to true!
     
  10. devonjacobsen

    devonjacobsen

    Joined:
    Nov 12, 2015
    Posts:
    30
    OMG THANK YOU SO MUCH! IT FINALLY WORKS AFTER SUCH A STUPID MISTAKE! cx Sorry, if you haven't noticed I'm a beginner but thank you so much! I can finally move onto more pressing parts of my game, thanks again.
     
  11. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    No problem, we all gotta start somewhere. Good luck!
     
  12. devonjacobsen

    devonjacobsen

    Joined:
    Nov 12, 2015
    Posts:
    30
    Thanks you too! Posted another question, for another problem I'm having if you wouldn't mind helping. I think you're pretty good all around, since I've looked around, and you seem to be pretty active.
     
  13. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Haha, well there are plenty other members around here who could code like the wind compared to me, but I'll try and help if I can.
     
  14. Q-ro

    Q-ro

    Joined:
    Feb 14, 2016
    Posts:
    10
    lol, as i was reading the posts i was starting to think "did he put the declaration of the bool in the update method" oh well, glad you could solve your problem :D.

    By the way, how do you post code like that ? i haven't been able to figure it out yet.
     
  15. devonjacobsen

    devonjacobsen

    Joined:
    Nov 12, 2015
    Posts:
    30
    What do you mean Q-ro? Oh, you mean post in the code? In the text input theres an inset button, next to the drafts button, and if you click it, it gives you the option to insert code! :D
     
  16. Q-ro

    Q-ro

    Joined:
    Feb 14, 2016
    Posts:
    10
    Nice, thanks a lot, :D.