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 /
  • Help Room /
avatar image
0
Question by trsh · Apr 09, 2020 at 06:18 AM · transformrotatepivot

How do I rotate a game object around a custom pivot?

A popular answer is to use transform.rotateAround, but I want to set certain angles rather the adding rotation. Could not find any good solutions for this :(

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 streeetwalker · Apr 09, 2020 at 10:10 AM 0
Share

@trsh I've update my answer to you a few times because I had to rethink - doing this off the top of my head.

1 Reply

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

Answer by streeetwalker · Apr 09, 2020 at 09:12 AM

HI @trsh, I've updated this because I had to re-think, again! The general problem is you need to get th object back to it's zero rotation. Here is the simplest solution yet

   1. get the offset distance to the pivot
          distance =  (object pos - pivot pos).magnitude
   2. move the object back to it's zero rotation - you have to decide 
                   what axis that is on - here, I'm using World Positive Right axis
          object pos = pivot pos + Vector3.right * distance

After doing that, the object has been moved back to it's zero rotation with respect the pivot. Then apply your desired angle to RotateAround

Previous solutions: In order to start from zero, You'll have to calculate the offset angle back what ever axis you consider zero rotation, and the rotateAround using the inverse that angle first ; Then rotateAround will start from no rotation.

What rotateAround does is this:

 1.  get the desired rotation as a quaternion
 2.  get the object offset vector to the pivot:  
           offset vector  = object pos - pivot pos
 3.  Rotate the offset vector
            rotated vector = offset vector * desired rotation
 4.  Move the object to new offset
            object pos = rotated vector + pivot pos  

So you see, because you use a quaternion multiplication to rotate the offset, which has the affect of taking the current rotation (already represented in the offset vector) and applying the new rotation, you will still need zero out the current rotation before doing all that.

So to calculate the inverse angle you need to apply - let us say positive x is the axis you consider zero:

  1. direction vector  = object pos - pivot pos
  2. inverse angle = Vector3.Angle( direction vector,  Vector3.up* )
  3.  the apply the ***negative of inverse angle to rotateAround
  ** you need to decide what frame of reference you are using World or local
       Vector3.up is the World frame on the Y axis in 3D
  *** whether or not you need to apply the negative of the angle depends on the
        order of the parameters in Vector3.angle

I believe you can shortcut the process by taking the result of 2 above, convert it to a quaternion, and multiplying your desired rotation by the Quaternion inverse of it, but I'd have to think about that further.

I believe that corrects everything I wrote before!

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

235 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

Related Questions

when trying to rotate, the object transforming itself 0 Answers

Rotating an object with VR controller 1 Answer

How to translate an object in an unknown angle ? 1 Answer

Move Single Bone With Script? 0 Answers

Rotation speed of the player (Paper Mario like) 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