Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nerrma · Dec 18, 2016 at 08:01 AM · 2d game

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.

     }
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Masterio · Dec 18, 2016 at 09:58 AM 0
Share

(-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.
     }
 }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Endless runner spawning on top of each other? 0 Answers

ArgumentException: Input Button right is not setup. 1 Answer

Where could I start with making a level editor where you can create your own terrain in Unity 2D? 0 Answers

Gameobject not moving unless starting at 0 transform 0 Answers

2D snake game stop sprite from leaving screen 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges