Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to flip a sprite?

Discussion in 'Scripting' started by klaymator14, Mar 2, 2010.

  1. klaymator14

    klaymator14

    Joined:
    Feb 3, 2009
    Posts:
    153
    I am using SpriteManager2, is there an easy way to flip the sprite itself when I want it to change direction? As in like if I make it go left, it will flip the sprite animation so it will be opposite? I tried making a second animation with the flipped frames (Not efficient). And here is the first section of my code handling the flipping.

    Code (csharp):
    1.  
    2. @script RequireComponent( CharacterController )
    3.  
    4. // This script must be attached to a GameObject that has a CharacterController
    5. var moveTouchPad : Joystick;
    6. var jumpTouchPad : Joystick;
    7.  
    8. var flip : System.Boolean;
    9. var charMain : GameObject;
    10. var charSprite : PackedSprite;
    11. var forwardSpeed : float = 4;
    12. var backwardSpeed : float = 4;
    13. var jumpSpeed : float = 16;
    14. var inAirMultiplier : float = 0.25;                 // Limiter for ground speed while jumping
    15.  
    16. private var thisTransform : Transform;
    17. private var character : CharacterController;
    18. private var velocity : Vector3;                     // Used for continuing momentum while in air
    19. private var canJump = true;
    20.  
    21. function Start()
    22. {
    23.     // Cache component lookup at startup instead of doing this every frame     
    24.     thisTransform = GetComponent( Transform );
    25.     character = GetComponent( CharacterController );   
    26.  
    27.     // Move the character to the correct start position in the level, if one exists
    28.     var spawn = GameObject.Find( "PlayerSpawn" );
    29.     if ( spawn )
    30.         thisTransform.position = spawn.transform.position;
    31. }
    32.  
    33. function OnEndGame()
    34. {
    35.     // Disable joystick when the game ends 
    36.     moveTouchPad.Disable();
    37.     jumpTouchPad.Disable();
    38.  
    39.     // Don't allow any more control changes when the game ends
    40.     this.enabled = false;
    41. }
    42.  
    43. function Update()
    44. {
    45.     var movement = Vector3.zero;
    46.    
    47.     if (moveTouchPad.position.x > 0) {
    48.         moveRight = true;
    49.         movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
    50.     }
    51.     else
    52.         moveRight = false;
    53.         movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;
    54.  
    55.     // Apply movement from move joystick
    56.     if ( moveRight == true ) {
    57.         charSprite.PlayAnim(0);
    58.     }
    59.    
    60.     if (moveRight == false) {  
    61.     charSprite.PlayAnim(1);
    62.     }
    63.  
    64.  
    Could anyone help? Lately I have been asking for a lot, and I don't want to come off as a pest.. I tried helping myself but failed miserably... by the way I am a 14 year old developer... and new to Javascript itself, I can't seem to get it to work as magically as I did with Actionscript :p Help is very appreciated!

    -Blayke :)
     
  2. klaymator14

    klaymator14

    Joined:
    Feb 3, 2009
    Posts:
    153
    I hate bumping threads, I'm sorry... I just can't get it to work... It just keeps on flipping the sprite every frame. No one has an idea of how to get it to work?

    -Blayke
     
  3. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    Hi, I have to confess to knowing nothing about SpriteManager2, so sue me if this does not work, but as a generic suggestion, perhaps you could multiply the Xscale of the sprite's transform by -1 ?

    Transform.localScale.x *= -1;

    .. to flip it over?

    EDIT: Oh sorry, didn't read your question correctly. The code you have posted looks OK, could the problem be what happens when you .PlayAnim(1/2); ?
     
  4. klaymator14

    klaymator14

    Joined:
    Feb 3, 2009
    Posts:
    153
    I tried doing that at first... but since it puts the texture onto a plane, it makes it completely disappear. So my only solution was to make a second animation of my character walking to the left. I tried using a boolean, but it seems to be flipping between each animation every frame. Thanks for noticing my post! It's hard when there are so many people replying on this forum :D

    -Blayke :)

    EDIT: Actually, on further note, that line of code seems to flip it correctly! But now it flips every single frame lol... one step closer!
     
  5. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    Right.

    Yes well it will flip it over every update, because every update moveRight == true.

    so you are saying:

    if moveRight then flip, if moveRight then flip, and so on...

    What you were doing before was saying:

    if moveRight then playAnimation, if moveRight then playAnimation

    therefore your problem is almost certainly inside the charSprite.PlayAnim() function - look in there...
     
  6. klaymator14

    klaymator14

    Joined:
    Feb 3, 2009
    Posts:
    153
    Code (csharp):
    1.  
    2. @script RequireComponent( CharacterController )
    3.  
    4. // This script must be attached to a GameObject that has a CharacterController
    5. var moveTouchPad : Joystick;
    6. var jumpTouchPad : Joystick;
    7.  
    8. var moveRight : float = 0;
    9. var charMain : GameObject;
    10. var charSprite : PackedSprite;
    11. var forwardSpeed : float = 4;
    12. var backwardSpeed : float = 4;
    13. var jumpSpeed : float = 16;
    14. var inAirMultiplier : float = 0.25;                 // Limiter for ground speed while jumping
    15.  
    16. private var thisTransform : Transform;
    17. private var character : CharacterController;
    18. private var velocity : Vector3;                     // Used for continuing momentum while in air
    19. private var canJump = true;
    20.  
    21. function Flipper() {
    22.         if (moveRight == 0) {
    23.         charSprite.PlayAnim(0);
    24.     }
    25.    
    26.     if (moveRight == 1) {  
    27.     charSprite.PlayAnim(1);
    28.     }
    29.    
    30. }
    31.  
    32. function Start()
    33. {
    34.     // Cache component lookup at startup instead of doing this every frame     
    35.     thisTransform = GetComponent( Transform );
    36.     character = GetComponent( CharacterController );   
    37.  
    38.     // Move the character to the correct start position in the level, if one exists
    39.     var spawn = GameObject.Find( "PlayerSpawn" );
    40.     if ( spawn )
    41.         thisTransform.position = spawn.transform.position;
    42. }
    43.  
    44. function OnEndGame()
    45. {
    46.     // Disable joystick when the game ends 
    47.     moveTouchPad.Disable();
    48.     jumpTouchPad.Disable();
    49.  
    50.     // Don't allow any more control changes when the game ends
    51.     this.enabled = false;
    52. }
    53.  
    54. function Update()
    55. {
    56.     var movement = Vector3.zero;
    57.    
    58.     if (moveTouchPad.position.x > .5) {
    59.         moveRight = 0;
    60.         movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
    61.         Flipper();
    62.     }
    63.     if (moveTouchPad.position.x < -.5) {
    64.         moveRight = 1;
    65.         movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;
    66.         Flipper();
    67.     }
    68.  
    69.  
    Ok, I think I got it working. So now I have a function that contains the Boolean, works pretty well, it flips. But now... It doesn't play the animation until after lifting my finger. Darn! So close! Thanks for the help again! You've been my biggest help :D

    -Blayke :)
     
  7. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    OR... if you want to do it the way I said, then you can use a flag to prevent it happening every update, a bit like this....

    Code (csharp):
    1.  
    2.  
    3. var facingLeft = true;
    4.  
    5. // Apply movement from move joystick:
    6.  
    7. if ( moveRight) {
    8.  
    9.      if (facingLeft) {
    10.  
    11.           Transform.localScale.x *= -1;
    12.           facingLeft = false;
    13.      }
    14.  
    15. } else {
    16.  
    17.      if (!facingLeft) {
    18.  
    19.           Transform.localScale.x *= -1;
    20.           facingLeft = true;
    21.      }
    22.  
    23. }
    24.  
    25.  

    Which is effectively saying:

    i. User wants me to move to the right?
    ii. OK, but am I facing left now? if yes, flip me, if not, don't flip me

    go back to i.
     
  8. klaymator14

    klaymator14

    Joined:
    Feb 3, 2009
    Posts:
    153
    Ah! That fixed it! Oh my god, thank you so much man :D Thanks for being so nice to me... I thought I was being rude for trying to get people to do work so I can get results I wanted... Thank you so much! I wish I could do something nice back to you... but not sure what that would be, haha :p

    THANK YOU!

    -Blayke :D
     
  9. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    You are welcome.

    The best thing you can do in return in my opinion, is to get reasonably comfortable at using Unity, and remember to occasionally give something back.

    So next time you see someone struggling with "How to flip Sprite direction?" like this, YOU can help them out...

    As you are only 14, you will probably get better at this stuff than most of us oldies, so we'll be pleased to have you here helping us out shortly!
     
  10. klaymator14

    klaymator14

    Joined:
    Feb 3, 2009
    Posts:
    153
    Will do, I'll try my best to give back to this community... Thanks for the encouraging words! :D

    -Blayke :)
     
  11. klaymator14

    klaymator14

    Joined:
    Feb 3, 2009
    Posts:
    153
    Deleted and moved to new thread.
     
  12. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Actually, it's much better if you do create separate threads for separate questions. You're more likely to get a good response if you do that.
     
  13. klaymator14

    klaymator14

    Joined:
    Feb 3, 2009
    Posts:
    153
    Thanks... that means I'm off to make a new thread! :p

    -Blayke :D
     
  14. skvic

    skvic

    Joined:
    Sep 19, 2014
    Posts:
    1
    Hi.
    In Unity 4.6 when u scale sprite with - it doesn't disappear.
    :)