How to flip a sprite?

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.

@script RequireComponent( CharacterController )

// This script must be attached to a GameObject that has a CharacterController
var moveTouchPad : Joystick;
var jumpTouchPad : Joystick;

var flip : System.Boolean;
var charMain : GameObject;
var charSprite : PackedSprite;
var forwardSpeed : float = 4;
var backwardSpeed : float = 4;
var jumpSpeed : float = 16;
var inAirMultiplier : float = 0.25;					// Limiter for ground speed while jumping

private var thisTransform : Transform;
private var character : CharacterController;
private var velocity : Vector3;						// Used for continuing momentum while in air
private var canJump = true;

function Start()
{
	// Cache component lookup at startup instead of doing this every frame		
	thisTransform = GetComponent( Transform );
	character = GetComponent( CharacterController );	

	// Move the character to the correct start position in the level, if one exists
	var spawn = GameObject.Find( "PlayerSpawn" );
	if ( spawn )
		thisTransform.position = spawn.transform.position;
}

function OnEndGame()
{
	// Disable joystick when the game ends	
	moveTouchPad.Disable();	
	jumpTouchPad.Disable();	

	// Don't allow any more control changes when the game ends
	this.enabled = false;
}

function Update()
{
	var movement = Vector3.zero;
	
	if (moveTouchPad.position.x > 0) {
		moveRight = true;
		movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
	}
	else
		moveRight = false;
		movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;

	// Apply movement from move joystick
	if ( moveRight == true ) {
		charSprite.PlayAnim(0);
	}
	
	if (moveRight == false) {	
	charSprite.PlayAnim(1);
	}

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 :stuck_out_tongue: Help is very appreciated!

-Blayke :slight_smile:

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

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); ?

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 :smile:

-Blayke :slight_smile:

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!

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…

@script RequireComponent( CharacterController )

// This script must be attached to a GameObject that has a CharacterController
var moveTouchPad : Joystick;
var jumpTouchPad : Joystick;

var moveRight : float = 0;
var charMain : GameObject;
var charSprite : PackedSprite;
var forwardSpeed : float = 4;
var backwardSpeed : float = 4;
var jumpSpeed : float = 16;
var inAirMultiplier : float = 0.25;					// Limiter for ground speed while jumping

private var thisTransform : Transform;
private var character : CharacterController;
private var velocity : Vector3;						// Used for continuing momentum while in air
private var canJump = true;

function Flipper() {
		if (moveRight == 0) {
		charSprite.PlayAnim(0);
	}
	
	if (moveRight == 1) {	
	charSprite.PlayAnim(1);
	}
	
}

function Start()
{
	// Cache component lookup at startup instead of doing this every frame		
	thisTransform = GetComponent( Transform );
	character = GetComponent( CharacterController );	

	// Move the character to the correct start position in the level, if one exists
	var spawn = GameObject.Find( "PlayerSpawn" );
	if ( spawn )
		thisTransform.position = spawn.transform.position;
}

function OnEndGame()
{
	// Disable joystick when the game ends	
	moveTouchPad.Disable();	
	jumpTouchPad.Disable();	

	// Don't allow any more control changes when the game ends
	this.enabled = false;
}

function Update()
{
	var movement = Vector3.zero;
	
	if (moveTouchPad.position.x > .5) {
		moveRight = 0;
		movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
		Flipper();
	}
	if (moveTouchPad.position.x < -.5) {
		moveRight = 1;
		movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;
		Flipper();
	}

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 :smile:

-Blayke :slight_smile:

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…

var facingLeft = true;

// Apply movement from move joystick:

if ( moveRight) { 

     if (facingLeft) {

          Transform.localScale.x *= -1; 
          facingLeft = false;
     }

} else {

     if (!facingLeft) {

          Transform.localScale.x *= -1; 
          facingLeft = true;
     }

}

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.

Ah! That fixed it! Oh my god, thank you so much man :smile: 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 :stuck_out_tongue:

THANK YOU!

-Blayke :smile:

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!

Will do, I’ll try my best to give back to this community… Thanks for the encouraging words! :smile:

-Blayke :slight_smile:

Deleted and moved to new thread.

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.

Thanks… that means I’m off to make a new thread! :stuck_out_tongue:

-Blayke :smile:

Hi.
In Unity 4.6 when u scale sprite with - it doesn’t disappear.
:slight_smile: