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 DigitalPowers · Feb 11, 2011 at 01:27 AM · vector3rotate-around

Vector3 Rotate Around

I have been using Transform.RotateAround to rotate a transform around a pivot but now I need to rotate a Vector3 around a point and I am stumped how to do it.

I am honistly considering backing up a transform's position and rotate and using it to hold my point but that would be poor programing. So my question is, is there a function to rotate a Vector3 around a pivot?

Thanks

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

2 Replies

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

Answer by Peter G · Feb 11, 2011 at 03:33 AM

I don't know of any built-in function, but you could create your own. Here's a quick example of something you could do.

static function RotateAroundPoint(point : Vector3, pivot : Vector3, angle : Quaternion) : Vector3 { var finalPos : Vector3 = point - pivot; //Center the point around the origin finalPos = angle * finalPos; //Rotate the point.

  finalPos += pivot;
  //Move the point back to its original offset. 

  return finalPos;

}

You can create the angle using Quaternion.Euler();

var newPos = SomeClass.RotateAroundPoint(Vector3.forward, Vector3.zero, Quaternion.Euler(0, 90, 0) );
Debug.Log(newPos);
//Prints (1, 0, 0) or is it (-1, 0, 0 )?  Something like this. :)
Comment
Add comment · Show 5 · 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 Peter G · Feb 11, 2011 at 04:23 AM 0
Share

I'm not sure why I got voted down. If its a math error, please tell me so I can fix it, but I think it is all sound even if not the most efficient method.

avatar image DigitalPowers · Feb 11, 2011 at 06:02 AM 0
Share

Thank you peter this was exactly the thing i was looking for it is working perfectly.

avatar image miketucker · Sep 26, 2011 at 12:21 PM 0
Share

slightly more optimized:

 function RotateAroundPoint(point : Vector3, pivot : Vector3, angle : Quaternion) : Vector3 {
     return angle * ( point - pivot) + pivot;
 }
avatar image Berzee · Sep 03, 2013 at 02:39 PM 0
Share

Works spiffingly! Thanks, $$anonymous$$isters Peter G and $$anonymous$$iketucker.

avatar image Scn64 · Mar 20, 2014 at 11:16 PM 0
Share

Thanks Peter! I've been trying to figure this out for hours and this solution works perfectly!

avatar image
8

Answer by WidmerNoel · Apr 30, 2015 at 06:44 PM

I think the solutions provided above can be improved a little more. These are extension methods, which behave as if they actually were memebers of Vector3 and Transform. (As if RotateAround would still exist)

 public static class RotateAroundPivotExtensions {
     //Returns the rotated Vector3 using a Quaterion
     public static Vector3 RotateAroundPivot(this Vector3 Point, Vector3 Pivot, Quaternion Angle) {
         return Angle * (Point - Pivot) + Pivot;
     }
     //Returns the rotated Vector3 using Euler
     public static Vector3 RotateAroundPivot(this Vector3 Point, Vector3 Pivot, Vector3 Euler) {
         return RotateAroundPivot(Point, Pivot, Quaternion.Euler(Euler));
     }
     //Rotates the Transform's position using a Quaterion
     public static void RotateAroundPivot(this Transform Me, Vector3 Pivot, Quaternion Angle) {
         Me.position = Me.position.RotateAroundPivot(Pivot, Angle);
     }
     //Rotates the Transform's position using Euler
     public static void RotateAroundPivot(this Transform Me, Vector3 Pivot, Vector3 Euler) {
         Me.position = Me.position.RotateAroundPivot(Pivot, Quaternion.Euler(Euler));
     }
 }
 
 /*USAGE:
 *
 * myGameObject.transform.RotateAroundPivot(CenterAsVector3, AngleAsVector3); //Modifies the Transform (immediate rotation)
 * myGameObject.transform.position.RotateAroundPivot(CenterAsVector3, AngleAsVector3); //Returns the rotated position (preview rotation)
 *
 */

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Instantiating at Collision Point. 2 Answers

Cannot implicitly convert type 'int' to 'UnityEngine.Vector3' 3 Answers

pendulum simulation with force vectors 2 Answers

vector3 current postion +1 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