- Home /
2D Unity scale reverse script not working (C#)
My Unity script does not seem to work. The console only logs "The arm has turned right." and the left feature does not work. The lines with the comment "Makes the sprite invisible" are also causing issues. I'm trying to make the player and the arm dynamically switch as the player moves the arm around so the player does not have his arm around his back the wrong way around.
Script:
void Awake () {
originalPlayerScaleX = Player.localScale.x;
}
void Update () {
armRot = Arm.rotation.z * 100; //Setting the amount of arm rotation every frame.
float playerScale = Player.localScale.x; //Checking the player scale every frame.
if (armRot >= 76 && armRot <= -76) {
Player.localScale = new Vector3(-originalPlayerScaleX, 0, 0); //Makes the sprite invisible
Arm.localScale = new Vector3(0, -originalPlayerScaleX, 0); //Makes the sprite invisible
Debug.Log("The arm has turned left"); //Remember to delete after testing is done.
}
if (armRot <= 76 && armRot >= -76)
{
Player.localScale = new Vector3(originalPlayerScaleX, 0, 0); //Makes the sprite invisible
Arm.localScale = new Vector3(0, originalPlayerScaleX, 0); //Makes the sprite invisible
Debug.Log("The arm has turned right"); //Remember to delete after testing is done.
}
}
}
(-originalPlayerScaleX, 0, 0) should be (-originalPlayerScaleX, 1, 1)
you scale other axis to 0 so its vey thin and invisible always
the best way to do this aproach is:
keep the original scale in Vector3 var:
private Vector3 _oryginalScale;
void Awake() { _oryginalScale = Player.localScale; }
next : use this vector in udpate function:
Player.localScale = new Vector3 (-_oryginalScale.x, _oryginalScale.y, _oryginalScale.z);
or
Player.localScale = new Vector3 (_oryginalScale.x, _oryginalScale.y, _oryginalScale.z);
same for arm if requierd
One more thing: use the 'else' keyword ins$$anonymous$$d second if, you dont need to check again if first condition is false. And second thing: you can optimized it by add the bool variable like: isRight = true;
then when you flip the transform right then set it to true when flip to left set it to false. Next add to the if(!isRight) statement and make second else if(isRight)
and inside the body of both statements add the : isRight = false or true; depends from currect flip state.
Finally something like that:
private Vector3 _oryginalPlayerScale;
private Vector3 _oryginalArmScale;
private bool _isRight = true;
void Awake ()
{
_oryginalPlayerScale = Player.localScale;
_oryginalArmScale = Arm.localScale;
}
void Update ()
{
// set left side
if (armRot >= 76 && armRot <= -76)
{
if(!_isRight)
return;
Player.localScale = new Vector3(- _oryginalPlayerScale.x, _oryginalPlayerScale.y, _oryginalPlayerScale.z); // Flip the scale ($$anonymous$$us the axis x, y or z)
Arm.localScale = new Vector3(- _oryginalArmScale.x, _oryginalArmScale.y, _oryginalArmScale.z); // Flip the scale ($$anonymous$$us the axis x, y or z)
_isRight = false;
Debug.Log("The arm has turned left"); // Remember to delete after testing is done.
}
// set right side
else
{
if(_isRight)
return;
Player.localScale = new Vector3(_oryginalPlayerScale.x, _oryginalPlayerScale.y, _oryginalPlayerScale.z); // Flip the scale
Arm.localScale = new Vector3(_oryginalArmScale.x, _oryginalArmScale.y, _oryginalArmScale.z); // Flip the scale
_isRight = true;
Debug.Log("The arm has turned right"); // Remember to delete after testing is done.
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612102903im_/https://answers.unity.com/themes/thub/images/avi.jpg)