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 awesomealvin · Jan 08, 2018 at 10:00 AM · rotationrotateparentingrotating

How to flip objects that are currently rotating?

In this gif, what I want is for the arms and bow to flip when the player is flipped, so it doesn't look like they are upside down.

I have tried making the arms a child of a parent, and then flipping the parent's x and y, but it didn't seem to do anything, except for rotating the arms when the main rotating has stopped.

This the code (in the Bow object) i am using for rotating the arms and bow:

     // Rotate Bow
         float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
         pivot.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         RotatePlayer();
 
         // Rotate Arm
         if (playerBowHoldShoulder != null) {
             playerBowHoldShoulder.rotation = Quaternion.AngleAxis(angle - 5.78f, Vector3.forward);
             playerBowDrawShoulder.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         }

The RotatePlayer() method just flips the player's Y rotation when the bow's xPos is greater than the player's xPos, and vice versa, which works fine as you can tell by the first gif.

This below is the hierarchy view of the Player object alt text Where the highlighted areas are the one's affected by the rotation you see in the gifs.

Any help or ideas would be appreciated, thanks! Let me know if I need to provide more info.

Comment
Add comment · Show 2
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 LukasLicek · Jan 08, 2018 at 10:14 AM 0
Share

@awesomealvin Idea: You can get Vector3.forward for player hands. Then check if its x value is positive or negative (looking right or left). In one of the cases switch their rotation in the same way as player objects.

Axis you need to check/flip may be different in your project :) Need further help? :)

avatar image awesomealvin LukasLicek · Jan 09, 2018 at 03:45 AM 0
Share

Yeah that's what I tried to the arms itself, but the rotation script keeps overriding the script that's trying to flip it. So that's why I used shoulders_pivot, but trying to flip that only affects it after the main rotation is not being used.

1 Reply

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

Answer by awesomealvin · Jan 11, 2018 at 09:27 AM

So I have kind of fixed it except I ran into another issue. But this pretty much resolves my initial problem:

First I also made the bow_pivot a child of the shoulders_pivot.

Then in the RotatePlayer(), it calls a function in the player object that flips the shoulders pivot.
Here's the code for that:

     bool RotatePlayer() {
         if (player != null && player.isDrawing) {
             float difference = transform.position.x - player.transform.position.x;
             if (difference >= 0) {
                 player.LookRight();
                 return true;
             } else {
                 player.LookLeft();
                 return false;
             }
         }
         return true;
     }

Returns true when facing right.
And here's the code for the LookLeft and LookRight:

 public void LookLeft() {
         Look(180f);
     }
 
     public void LookRight() {
         Look(0.0f);
     }
 
     private void Look(float angle) {
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
 
         // To flip the arm and bow
         if (isDrawing) {
             shouldersPivot.eulerAngles = new Vector3(angle, 0f, 0f);
         }
     }

Now with this and the original rotation code, the rotation won't flip. So changing the .rotation to .localRotation makes it work. I don't know why but it just does. So the arms and bow will flip, but when facing the left side, the rotation becomes inverted. So I changed the code a bit to solve that:

         // Rotate Bow
         float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
         pivot.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
         bool isLookingRight = RotatePlayer();
         angle = (isLookingRight) ? angle : -angle;
         pivot.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
 
         // Rotate Arm
         if (playerBowHoldShoulder != null) {
             playerBowHoldShoulder.localRotation = Quaternion.AngleAxis(angle - 5.78f, Vector3.forward);
             playerBowDrawShoulder.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
         }

And now the flip works correctly. So I guess I can resolve this question. But the new problem that you can see in the gif is that when shooting towards the left side, the rotation will look at the right side for one frame. I have been struggling with this issue for a couple hours and no idea how to fix it. Note that in the gif, only the first shot seems to have this issue, every other left side shot does. The gif just didn't capture the exact frame that the bug happens.

Comment
Add comment · Show 2 · 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 LukasLicek · Jan 11, 2018 at 11:26 AM 0
Share

I don't know much about rotations, so sadly I can't help you 100%

But I would intuitively say that this problem isn't just with the left side. You just don't see it on the right side because the rotation is already right?

avatar image awesomealvin LukasLicek · Jan 12, 2018 at 11:13 AM 0
Share

Ah, yeah that could be true, thanks! That should help me find the problem. I'll post a reply to the solution if I solve it.

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

101 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

Related Questions

How to rotate an object so it always faces up? 1 Answer

Rotation not working 3 Answers

How can i follow an object by 1-axis-rotation? 0 Answers

Rotation for 180 degrees of character 1 Answer

How do I call a function containing a while loop, inside the update function? 2 Answers


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