Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Xysch · Feb 23, 2015 at 10:43 PM · c#transformspritedirectionlocalrotation

Sprite Direction using transform.localRotation probelm

So I am using a little bit of code here which is supposed to flip the sprite upside down when the enemy or player goes upside down just to keep it looking upright. The code doesn't seem to pass the if statements because the Boolean doesn't change and the sprites don't flip. If you could help me out I'd appreciate it, thanks!

         public bool FacingRight = true;
 
         void Update ()
         {
                 if (transform.localRotation.z > 90 && FacingRight == true) {
                         if (transform.localRotation.z < 270 && FacingRight == true) {
                                 transform.localScale += new Vector3 (0, -1f, 0);
                                 FacingRight = false;
                         }
                 } else if (transform.localRotation.z < 90 && FacingRight == false) {
                         if (transform.localRotation.z > 270 && FacingRight == false) {
                                 transform.localScale += new Vector3 (0, -1f, 0);
                                 FacingRight = true;
                         }
                 }
         }
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 siaran · Feb 23, 2015 at 11:10 PM 0
Share

not an answer to your question, but the fact that you have

 if (transform.localRotation.z > 90 && FacingRight == true) {
  if (transform.localRotation.z < 270 && FacingRight == true) {

ins$$anonymous$$d of

   if (
   transform.localRotation.z > 90 
   && transform.localRotation.z < 270
   && FacingRight == true
   ){

for no reason kind of bothers me xD (Sorry, just really jumped out at me, will try to answer your question now)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by maccabbe · Feb 24, 2015 at 01:11 AM

You want to use transform.localrotation.eulerAngles.z or transform.rotation.eulerAngles.z instead of transform.localrotation.z.

Rotations are stored as quaternions but most times you want to use them like Vector3 (as it is in the unity inspector) so use .eulerAngles to easily convert them to Vector3.

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

Answer by siaran · Feb 23, 2015 at 11:18 PM

Ok, number of problems:

1) .localRotation is a quaternion, you cannot use it like you are doing here - quaternion x,y,z are NOT euler angles.

2) .localRotation is relative to an object's parent. Does your object even have a parent? If not, there is no point in using localRotation.

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

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

20 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

Related Questions

Shooting script C# 1 Answer

How to rotate an object to face the direction it's going? 1 Answer

Sprite is not shown moving 1 Answer

Distribute terrain in zones 3 Answers

Unity crashing and LocalScale not working 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