• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by Bobfan · Sep 07, 2013 at 01:01 PM · vector3rotate objectrotatearoundtransform.rotatearound

Rotate a vector around a certain point.

I have already tried transferring my point into a transform, using t.RotateAround, and converting it back into a Vector, but in return, it is giving me a weird error. Instead of trying to fix it, I want to use a better method of doing this. So what would I have to do to rotate my Vector3 around a specific point?

Comment
reefwirrax
TakuanDaikon
leonardoraele
vexe
spinnerbox
Necronomicron
Guhanesh
FatCatz
Ant0nin
ahungrybear
timberap
JasonCG
RodrigoSeVeN
masta-yoda
SamFernGamer4k
And 2 more...

People who like this

17 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by aldonaletto · Sep 07, 2013 at 01:45 PM

If you want to rotate a vector, multiply it by a Quaternion. Supposing that you want to rotate a vector 60 degrees about the world X axis, for instance:

 var myVector: Vector3 = Quaternion.Euler(60, 0, 0) * Vector3.forward;

myVector becomes the vector (0,0,1) rotated 60 degrees about X.

But be aware that a vector indicates a direction in space - it's not tied to any specific position, thus rotating a vector around a point doesn't make any sense. If you actually want to rotate a point around a pivot instead, get its direction relative to the pivot and rotate it, then add the rotated direction to the pivot:

 function RotatePointAroundPivot(point: Vector3, pivot: Vector3, angles: Vector3): Vector3 {
   var dir: Vector3 = point - pivot; // get point direction relative to pivot
   dir = Quaternion.Euler(angles) * dir; // rotate it
   point = dir + pivot; // calculate rotated point
   return point; // return it
 }




Comment
Bobfan
reefwirrax
TakuanDaikon
valyard
leonardoraele
Stardog
Jesse_Pixelsmith
Habitablaba
vexe
z3nth10n
demid
zaniar
abilius_xxi
Swiftle
kreso
And 63 more...

People who like this

78 Show 18 · 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 Bobfan · Sep 07, 2013 at 03:26 PM 0
Share

Thanks for the advice.

avatar image reefwirrax · Dec 23, 2013 at 04:43 PM 2
Share

Can you help me convert the above one to c#? There is a bug in the first line.

 public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) {
   Vector3 dir = point - pivot; // get point direction relative to pivot
   dir = Quaternion.Euler(angles) * dir; // rotate it
   point = dir + pivot; // calculate rotated point
   return point; // return it
 }
avatar image aldonaletto · Dec 24, 2013 at 02:05 AM 0
Share

This code seems ok - what errors are you getting?

avatar image ShamusO aldonaletto · Apr 10, 2020 at 02:37 AM 0
Share

Hey so I am thinking of using this to add to my "camera follow" script, but I am having a hard time understanding how to apply it. In the script posted what is "Vector3 angles" equal to?

PS : Sorry for opening an old post but I have been looking all over on how to achieve this with no luck. Basically, I am trying to have the camera rotate around a plane/spacecraft from behind with the craft as the pivot.

avatar image reefwirrax · Dec 29, 2013 at 07:10 PM 0
Share

hi there, the main error is that i worked for 3-4 hours to figure out the rotation, and after many hours of angst, i came back to this page, i read the first 3 lines you wrote attentively and slowly, and then i had the required code in 2-3 minutes. :D

avatar image Yofurioso · Dec 30, 2013 at 03:23 PM 0
Share

A question, can this be used to rotate a vector or a transform so that it maintains it's current direction but with a new rotation?

Show more comments
avatar image

Answer by Guenter123987 · Jan 31, 2017 at 04:11 PM

If you need to use the RotateAroundPivotPoint function very often this could improve your performance:

 public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) {
    return Quaternion.Euler(angles) * (point - pivot) + pivot;
  }

It might work better because there are not so many variables in the code which have to be managed.

Comment
lukas_werz
ahungrybear
syscrusher
cxode
glenneroo
Lipoly
DavidMiranda
Demigiant
HologramInteractive
acidbubbles_anon
SamFernGamer4k

People who like this

11 Show 4 · 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 lukas_werz · Apr 08, 2017 at 12:22 PM 0
Share

Thank you!

avatar image Taylor-Libonati · Nov 22, 2017 at 02:03 AM 1
Share

Also if you want to be able to pass a quaternion or some angles:

     public static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) {
         return RotatePointAroundPivot(point, pivot, Quaternion.Euler(angles));
     }
 
     public static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Quaternion rotation) {
         return rotation * (point - pivot) + pivot;
     }
avatar image unity_dA5TCa2krrACsQ · Dec 24, 2017 at 03:53 AM 0
Share

I'm rotating around 0,0,0, so I'm using this method... Vector3 rotateAround(Vector3 point, Quaternion rotation){ return point * rotation; } It doesn't seem to work. Help?

avatar image DavidMiranda · Mar 01, 2019 at 11:02 AM 0
Share

Saved me :)

avatar image

Answer by FlightOfOne · Dec 28, 2017 at 07:01 PM

Thank you all for the above answers. Helped me a lot. but took some time to wrap my mind around this to fit this solution to what I needed to do. I wanted to have a transform move to a new point and rotate it to match this new point's rotation. But the original transform has a pivot that it needs to rotate around that is not at the center. Posting this here so anyone else that might want to do the same can benefit without having to spend too much time figuring it out.

 public class RotTester : MonoBehaviour
 {
     [SerializeField] Transform _pivotPosition;//parented to this transform
     [SerializeField] Transform _finalPosition;//I want to move this transform to here
 
     // Use this for initialization
     void Start()
     {
         //I want to move this object to the target Position
         //First move to target
         transform.position = _finalPosition.position; 
         //set the final position after rotating around the pivot and offset the pivot local position
         transform.position = RotatePointAroundPivot(_finalPosition.position, _pivotPosition.position, _finalPosition.rotation)- _pivotPosition.localPosition;
         //finally set the rotation to target rotation
         transform.rotation = _finalPosition.rotation; 
     }
 
 
     public Vector3 RotatePointAroundPivot(Vector3 _finalPosition, Vector3 _pivotPosition, Quaternion _finalRotation)
     {
         return _pivotPosition +(_finalRotation * (_finalPosition - _pivotPosition)); // returns new position of the point;
     }
 }

Comment

People who like this

0 Show 3 · 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 Taylor-Libonati · Dec 28, 2017 at 08:50 PM 0
Share

Glad you got it working! I think the link after "First move to target" is unneeded. RotatePointAroundPivot is already using the final position and applying it to transform.position. But other then that it looks legit.

avatar image FlightOfOne Taylor-Libonati · Dec 28, 2017 at 11:02 PM 0
Share

I thought the same, but it goes all over the place for me if I don't set the position first. Maybe I am not seeing something in rest of my code.

avatar image Taylor-Libonati FlightOfOne · Dec 29, 2017 at 12:43 AM 0
Share

oh odd. I could only see that happening if _pivotPosition is nested in transform or something. But if it ain't broke don't fix it I suppose!

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

37 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

Related Questions

Multiple Cars not working 1 Answer

How to make the transform to move smooth to the new radius when changing the radius while rotating around target? 0 Answers

Rotating around self axis by n degrees ?? 1 Answer

Joystick Rotation with Transform.RotateAround 0 Answers

How I can Converting a Direction Vector to Quaternion rotation? 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