• 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 oscar70701 · Feb 15, 2019 at 02:17 PM · 2drotation2d gameplayerflip

Flip the player with arm rotation

video of the problem :

https://www.youtube.com/watch?v=plYY5GvNPrk&feature=youtu.be

need to add a flip

arms script :

public class ArmsRot : MonoBehaviour {

 private bool m_FacingRight = true;  // For determining which way the player is currently facing.
 public GameObject MyPlayer;

 void Update () 
 {
     //Arm Rotation
     Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
     difference.Normalize ();
     float rotationZ = Mathf.Atan2 (difference.y, difference.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Euler (0f, 0f, rotationZ);
     if (rotationZ < -90 || rotationZ > 90) 
     {
         if (MyPlayer.transform.eulerAngles.y == 0) 
         {
             transform.localRotation = Quaternion.Euler (180, 0, -rotationZ);
             Flip ();
         } else if (MyPlayer.transform.eulerAngles.y == 180){
             transform.localRotation = Quaternion.Euler (180, 180, - rotationZ);
             Flip ();
         }
     }
 }
 private void Flip()
 {
 // Switch the way the player is labelled as facing.
 m_FacingRight = !m_FacingRight;

 // Multiply the player's x local scale by -1.
 Vector3 theScale = MyPlayer.transform.localScale;
 theScale.x *= -1;
 MyPlayer.transform.localScale = theScale;
 }

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Magso · Feb 15, 2019 at 04:19 PM

Use Mathf.Clamp on rotationZ.

 rotationZ = Mathf.Clamp(rotationZ, -90, 90);

Comment
Add comment · Show 1 · Share
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 oscar70701 · Feb 15, 2019 at 04:52 PM 0
Share

where i need to use this ?

avatar image
0

Answer by Ymrasu · Feb 15, 2019 at 07:08 PM

First, if your arm is a child of your player then the arm gets flipped as well, so you'll need to account for that. Second, the way you set up your flip checks can cause you to call Flip() repeatedly.

 void Update()
     {
         //Arm Rotation
         Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
 
         
         // also check if you are facing the other way
         // so you do not call Flip() when you dont need to
         if (Mathf.Abs(rotationZ) > 90f && m_FacingRight) {
                 Flip();
         }
         else if (Mathf.Abs(rotationZ) < 90f && !m_FacingRight) {
                 Flip();
         }
 
         // Since your player's local scale is fliped
         // you'll need to adjust the rotation too
         if (!m_FacingRight) {
             rotationZ += 180;
         }
 
         // lastly apply the rotation
         transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
     }

Let me know if this updated Update() is not working out for you.

Comment
Add comment · Show 1 · Share
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 oscar70701 · Feb 15, 2019 at 07:45 PM 0
Share

Thanks very cool ! (:

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

246 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Zig-zag movement and rotation 1 Answer

Localscale-flipped 2D character retains original rotation since 5.4 2 Answers

2D rotation question 0 Answers

Looking at target in 2D 1 Answer

Rotate/Flip Image in Unity 2d using Virtual Joystick 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