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 callen · Feb 14, 2014 at 06:04 AM · rotationtransformparenting

X-Flip is making my rotation break?

Hi, I'm trying to make a twin-stick player like in Nuclear Throne, meaning a side-facing sprite with a gun that rotates around him 360 degrees. The graphics x-flip when past 90/-90 degrees, but the x-flip/angle combo isn't working as I'd expect...

The gun's image (on a quad) is a child of my "gun" object, which is the game object I apply the rotation to. In the normal orientation, the "gun" is positioned at the arm-pivot point, and rotation happens around this point just fine.

When flipped, the gun rotates from a weird random pivot (not on the arm). When I look at everything in the scene view, the gun is at the correct arm-pivot point, like it should work. This is the confusing part if I change the angle of the gun object by a tiny amount (0.00001) in the properties window it snaps into the correct position and orientation.

So I don't see any issues with the math, and unity "fixes" it when I edit the angle directly in the properties window - any ideas why the rotation gets messed up in-game?

(note: While looking for an answer, I only found the suggestion to rotate around the Y-axis instead. I'd prefer to not do this for a variety of reasons, including my quads disappearing and my z-ordering getting flipped.)

EDIT: Since you asked, here's some code. It's split between two scripts (one for the character, another for the gun). Here's the character's x-flip code, the facex var is created from the inputs, so you face the way you are moving/shooting. FlipX is a constant with values (-1, 1, 1).

 if (facex != 0 && facex > 0 != transform.localScale.x > 0)
     transform.localScale = Vector3.Scale(transform.localScale, FlipX);
   

  

And here's the code on the weapon which rotates it (and its child with the image quad).

 float ang = Mathf.Rad2Deg * Mathf.Atan2(aim.y, aim.x);
 if(ang > 90 || ang < -90)
     ang = Mathf.Sign(ang) * 180 - ang;
 Quaternion q = Quaternion.AngleAxis(ang, Vector3.forward);
 transform.rotation = q;

 

I think the math is correct, because as I pointed out things get 'fixed' when I enter identical values at runtime in the inspector. It's almost like there is some kind of Apply() method I need to call... but that isn't the case right?

Comment
Add comment · Show 4
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 robertbu · Feb 14, 2014 at 07:59 AM 0
Share

Can we see the rotation code for rotating the gun? How are you flipping your player?

avatar image callen · Feb 15, 2014 at 04:17 PM 0
Share

I have added code as requested. I hope it helps.

avatar image erick_weil · Feb 15, 2014 at 04:36 PM 0
Share

try use transform.Rotate(speed*Time.deltaTime,0,0) ins$$anonymous$$d your math.

avatar image callen · Feb 15, 2014 at 04:57 PM 0
Share

I'll assume you meant (0,0,speed*Time.deltaTime) to rotate around the Z axis. I already tried this, and it doesn't fix anything.

2 Replies

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

Answer by callen · Feb 15, 2014 at 05:41 PM

Hopefully someone can come up with a better solution than this, but I managed to fix it using a trick I saw here: http://2dtoolkit.com/forum/index.php?topic=3370.0

Every frame, I can do this on my weapon script and it makes everything work the way it is supposed to work.

             gameObject.SetActive(false);
             gameObject.SetActive(true);

Of course, this incurs a performance hit averaging around .35ms according to stopwatch. So if I need to animate 12+ networked players using this code, it will probably not be a viable solution. Any real solution would be appreciated!!!

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
0

Answer by saulth · Aug 25, 2016 at 03:01 PM

Same problem. Have parent object with x scale = -1. Want to set child positions and rotations, but positioning is not mirrored, and rotations get broken. My work-around is: before updating child rot/pos set the parent scale to 1,1,1 then set child rot/pos, then set parent scale to -1,1,1 which then properly mirrors all children. Using Unity 5.4.

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

Changing the parent transform of the First Person Controller 1 Answer

Moving child transform also moves parent rigidbody 0 Answers

How to instantiate on custom rotation? 1 Answer

transform.Rotate For an Certain Amount of Time 1 Answer

Unity Rotate Raycast on Quaternion 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