- Home /
Flip scripts not working anymore?
in 2D, I'm using a script to flip the sprite horizontally to show the player he is facing the other direction and not moonwalking, however this recently stopped working and I have no idea why. Are there any variables that would prevent a script which inverts the scale of an object from working? This is a snippet of the script I am using (your typical flip script)
if (h < 0 && facingRight)
{
FlipX();
print("Flip Left!");
}
if (h > 0 && !facingRight)
{
FlipX();
print("Flip Right!");
}
function FlipX(){
facingRight = !facingRight;
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
}
O$$anonymous$$ A new update - I disabled the animator and NOW the sprite will flip. Clearly I can't just leave the animator since I need animations, but its a step in the right direction...
Answer by Xavier78 · Apr 15, 2014 at 10:51 PM
well it kinda looks like you are making an loop with faceRight that will all ways = !faceright after the first time, you might want
Flipx(){ if (faceRight) { faceright=!faceright; } else { !faceright = faceright; }
untested
other then that to little script, i would also try copying the script into a new one, just encase corruption.
I was thinking corruption too, but I've moved everything into a new script which has literally nothing but the requirements to get the character moving and to flip and ... it doesn't flip. I tried what you said, didn't help.
here is the entirety of the $$anonymous$$imalist script I just made:
#pragma strict
var Speed : float = 4f;
var isGrounded : boolean;
var canClimb : boolean;
var groundedEnd : Transform;
var climbingEnd : Transform;
var h : float = 0.0;
var v : float = 0.0;
var facingRight : boolean = true;
var facingUp : boolean = true;
function Start () {
}
function FixedUpdate () {
movement();
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
}
function movement(){
if (h!= 0 && !canClimb){
transform.Translate (Vector2(h * Speed * Time.deltaTime,0));
}
if (h < 0 && facingRight)
{
Flip();
print("!facingRight");
}
else if (h > 0 && !facingRight)
{
Flip();
print("facingRight");
}
}
function Flip ()
{
if(facingRight==true){
facingRight = false;
transform.localScale = new Vector2(transform.localScale.x * -1, 1f);
}
else if(facingRight==false){
facingRight = true;
transform.localScale = new Vector2(transform.localScale.x * -1, 1f);
}
}
and also, you can test this script to see what where and why each thing is being called with
#pragma strict
var Speed : float = 4f;
var isGrounded : boolean;
var canClimb : boolean;
var groundedEnd : Transform;
var climbingEnd : Transform;
var h : float = 0.0;
var v : float = 0.0;
var facingRight : boolean = true;
var facingUp : boolean = true;
function Start () {
}
function FixedUpdate () {
movement();
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
print("1");
}
function movement(){
print("2");
if (h!= 0 && !canClimb){
print("3");
transform.Translate (Vector2(h * Speed * Time.deltaTime,0));
}
if (h < 0 && facingRight)
{
print("4");
Flip();
print("!facingRight");
}
else if (h > 0 && !facingRight)
{
print("5");
Flip();
print("facingRight");
}
}
function Flip ()
{
print("6");
if(facingRight==true){
facingRight = false;
transform.localScale = new Vector2(transform.localScale.x * -1, 1f);
print("7");
}
else if(facingRight==false){
print("8");
facingRight = true;
transform.localScale = new Vector2(transform.localScale.x * -1, 1f);
}
}
hmmm this is interesting. using this to test, here's what I'm getting:
1 & 2 are always being called, ok thats fine.
3 is only called when you start moving, but stacks up quickly.
$$anonymous$$oving to the left calls 4,6 and 7 once. 4 to initiate that the character needs to flip, 6 to begin the flip function, and then 7 to actually flip it to the left. Also - facing Right boolean is false once you do this, BUT the character is still facing right.
$$anonymous$$oving back to the right calls 5, 6, and 8 once. 5 to initiate that the character needs to flip, 6 to begin the flip function, then 8 to actually flip it to the right. The facing Right boolean is true now, and the character is facing right still, having never flipped anyway.
I don't understand why it's not actually flipping if it's calling every function once. It isn't looping. The boolean is switching.
Doing the same thing with uanmanarmy's "$$anonymous$$ike's version" script but changing it to JS and it calls all the numbers still. And even though the scale says -1, the character's scale never actually becomes -1.
Here's the modified C# -> JS script I'm using:
var theScale : Vector2;
function Start () {
theScale = transform.localScale;
}
function Flip (){
print("6");
facingRight = !facingRight;
print("7");
theScale.x *= -1;
print("8");
transform.localScale = theScale;
print("9");
about ready to give up, nothing seems to fix the issue...
I even tried modifying the script to specifically target the player by declaring it as a transform and then applying the player.localScale, and that does nothing.
well the last thing i can say to help is see if something else is moving it, or try to call the flip in a different way, i will try to put it onto something to see, but i am not using sprites in my project right now, also try to not use the * -1, see if just setting the number manually works.
aaaahhhh, now this is a guess, but i think that the base object that you are flipping has an animation on its rotation, witch might be stopping all this, to solve this try putting your character into a empty object and then make this script flip that. also make sure the animation isn't flipping completely around also.
Because the script did work with out the animation that means it works, just to figure out what is wrong with animation.
Answer by uanmanarmy · Apr 15, 2014 at 10:42 PM
This is my Mike's version (c#)
void Flip ()
{
facingRight = !facingRight; // flipping to other direction
Vector3 theScale = transform.localScale; //get the local scale
theScale.x *= -1; // flip the x axis
transform.localScale = theScale; // apply back to local scale
}
I saw that script, tried it out and its not working either. I really don't know what to do, this happened suddenly. It WAS working for $$anonymous$$ONTHS and suddenly I try to add a new script to allow for crouching and bam, it stops working. Removed said script, still doesn't work, and now I'm just completely at a loss.
I used it Sometime ago and it's still working, it's impossible to not work
This is my work with this script
sorry but its just not working for me, i'm using JS so I can't use your script line for line, however with a little editing I got it in and it looks like its working, but its just not actually changing the character's transform. TheScale value changes as I flip, but it won't affect the character itself.
Did you figure this out? I'm using $$anonymous$$ike's code as posted above and it's just happened to me. It's been working flawlessy for weeks, now all of a sudden it doesn't flip, yet everything is being called correctly and the bool is reporting as flipped. Something is forcing the scale back to 1 when I haven't changed that code since implementation!
Is there a way to print out what is changing the scale? Unless the game is paused, something is updating the scale to 1 every frame, ins$$anonymous$$d of -1 (even whilst facingRight is false).
EDIT: The problem was in the Animator. After commenting out all code not relevant to the running I still didn't find the issue. I found out it was an animation in my Animator that was causing the issue. Once I deleted it, it began functioning normally again. I'm not sure how it was possible, but that was the culprit.
I got the same problem after I added a new animation to my project. $$anonymous$$y new animation had two parts: jumping and scaling to smaller. Ins$$anonymous$$d of deleting the animation, I just delete the part of scaling. And it worked normally.
So I think the reason is that I set the scale to some fixed values in the animation. And i have to find another way to scale my character in animation.
Your answer
Follow this Question
Related Questions
2D spaceship rotation 0 Answers
2D Box Collider doesn't flip with Sprite 3 Answers
Sprite flip movement script not working! HELP! 0 Answers
LocalScale not working idealy 1 Answer
How to do a sprite flip 1 Answer