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 Help is very appreciated!
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?
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
-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!
@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
Ah! That fixed it! Oh my god, thank you so much man 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