Comments and answers for "Rotate a vector around a certain point."
http://answers.unity.com/questions/532297/rotate-a-vector-around-a-certain-point.html
The latest comments and answers for the question "Rotate a vector around a certain point."Comment by ShamusO on ShamusO's comment
http://answers.unity.com/comments/1716746/view.html
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.Fri, 10 Apr 2020 02:37:00 GMTShamusOComment by DavidMiranda on DavidMiranda's answer
http://answers.unity.com/comments/1607712/view.html
Saved me :)Fri, 01 Mar 2019 11:02:57 GMTDavidMirandaComment by dougn7 on dougn7's answer
http://answers.unity.com/comments/1453264/view.html
Thanks Aldo! You help us a lot!Thu, 11 Jan 2018 12:35:33 GMTdougn7Comment by Taylor-Libonati on Taylor-Libonati's comment
http://answers.unity.com/comments/1447781/view.html
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!Fri, 29 Dec 2017 00:43:23 GMTTaylor-LibonatiComment by FlightOfOne on FlightOfOne's comment
http://answers.unity.com/comments/1447764/view.html
I thought the same, but it goes all over the place for me if I don't set the position first. $$anonymous$$aybe I am not seeing something in rest of my code.Thu, 28 Dec 2017 23:02:35 GMTFlightOfOneComment by Taylor-Libonati on Taylor-Libonati's answer
http://answers.unity.com/comments/1447736/view.html
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.Thu, 28 Dec 2017 20:50:44 GMTTaylor-LibonatiAnswer by FlightOfOne
http://answers.unity.com/answers/1447707/view.html
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;
}
}Thu, 28 Dec 2017 19:01:49 GMTFlightOfOneComment by unity_dA5TCa2krrACsQ on unity_dA5TCa2krrACsQ's answer
http://answers.unity.com/comments/1446188/view.html
Thank You!!! Very helpfull.Sun, 24 Dec 2017 03:56:38 GMTunity_dA5TCa2krrACsQComment by unity_dA5TCa2krrACsQ on unity_dA5TCa2krrACsQ's answer
http://answers.unity.com/comments/1446187/view.html
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?Sun, 24 Dec 2017 03:53:23 GMTunity_dA5TCa2krrACsQComment by Taylor-Libonati on Taylor-Libonati's answer
http://answers.unity.com/comments/1434008/view.html
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;
}Wed, 22 Nov 2017 02:03:29 GMTTaylor-LibonatiComment by lukas_werz on lukas_werz's answer
http://answers.unity.com/comments/1337216/view.html
Thank you!Sat, 08 Apr 2017 12:22:34 GMTlukas_werzAnswer by Guenter123987
http://answers.unity.com/answers/1306696/view.html
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.Tue, 31 Jan 2017 16:11:55 GMTGuenter123987Comment by carrotstien on carrotstien's answer
http://answers.unity.com/comments/1244135/view.html
sweet! I looked and found the answer so quickly. Thanks!Thu, 15 Sep 2016 14:12:24 GMTcarrotstienComment by syscrusher on syscrusher's answer
http://answers.unity.com/comments/1177853/view.html
Thanks for posting this. I was able to adapt this to solve a problem I was having drawing a procedural gizmo in the editor.Thu, 28 Apr 2016 04:22:42 GMTsyscrusherComment by SpacePilot1000 on SpacePilot1000's answer
http://answers.unity.com/comments/1160429/view.html
This was exactly the info I was looking for, thanks muchly!Thu, 24 Mar 2016 22:20:52 GMTSpacePilot1000Comment by Captain-Awesome on Captain-Awesome's answer
http://answers.unity.com/comments/1086602/view.html
@aldonaletto I know I'm commenting two years after you wrote this answer, but thankyou sooooo much.Fri, 23 Oct 2015 02:53:07 GMTCaptain-AwesomeComment by meat5000 on meat5000's answer
http://answers.unity.com/comments/1083354/view.html
@Zen$$anonymous$$icro You made your model in Blender and didnt apply a rotation fix?Sat, 17 Oct 2015 09:04:25 GMTmeat5000Comment by ZenMicro on ZenMicro's answer
http://answers.unity.com/comments/1083011/view.html
@aldonaletto - Dude, you are the man :) this made for a very clean 4 lines of code for me to get a complete orbit cam :) Appreciate your contributions here!
Here is my slightly modified code (of my orbit cam, not the function I got from you and @reefwirrax 's C# rewrite), so thanks both of you!
Vector3 eyeCenter = Vector3.Lerp(m_LeftEye.position, m_RightEye.position, 0.5f); // Get the point right between the eyes
distanceFromPivot = eyeCenter + (m_LeftEye.forward * m_Zoom); // Use one eye inverse direction to move back by X amount
transform.position = RotatePointAroundPivot(distanceFromPivot, eyeCenter, new Vector3(m_$$anonymous$$ch, m_Yaw, 0)); // Apply my prepared $$anonymous$$ch and Yaw values
transform.LookAt(eyeCenter); // Finally now look back at the pivot
Actually i had to use eye.right, not sure why my model's eyes' forward is the right but anyways.
@meat5000, not surer how to reply to your post, no button? but i didn't create the model so yeah it's possible what you said :)Fri, 16 Oct 2015 16:29:27 GMTZenMicroComment by AmBeam on AmBeam's answer
http://answers.unity.com/comments/848960/view.html
@aldonaletto - thank you very much, Your script helped me a lot!Sun, 07 Dec 2014 18:28:57 GMTAmBeamComment by vexe on vexe's answer
http://answers.unity.com/comments/848872/view.html
@aldonaletto just wanted to drop by and say you are a genius good sir! Your answers always helped me solve some very sticky situations. Thank you!Sun, 07 Dec 2014 17:08:34 GMTvexeComment by WabiWasabi on WabiWasabi's answer
http://answers.unity.com/comments/635165/view.html
Great, concise explanation and example. (Would vote up if my rep permitted. :-)Sat, 08 Feb 2014 23:03:59 GMTWabiWasabiComment by reefwirrax on reefwirrax's answer
http://answers.unity.com/comments/606960/view.html
that is a muddled explanation. an object doesnt have a direction, unless you are explaining where it is pointing or moving to. an object only has an XYZ rotation, position, and movement. so if the object is moving, in a direction, it maintians its direction if the movement is a globalposition code. the transform.lookat stuff makes obj's turn towards a point in space.Wed, 01 Jan 2014 10:58:49 GMTreefwirraxComment by Yofurioso on Yofurioso's answer
http://answers.unity.com/comments/605687/view.html
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?Mon, 30 Dec 2013 15:23:28 GMTYofuriosoComment by reefwirrax on reefwirrax's answer
http://answers.unity.com/comments/605239/view.html
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. :DSun, 29 Dec 2013 19:10:30 GMTreefwirraxComment by aldonaletto on aldonaletto's answer
http://answers.unity.com/comments/602309/view.html
This code seems ok - what errors are you getting?Tue, 24 Dec 2013 02:05:45 GMTaldonalettoComment by reefwirrax on reefwirrax's answer
http://answers.unity.com/comments/602039/view.html
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
}Mon, 23 Dec 2013 16:43:06 GMTreefwirraxComment by Bobfan on Bobfan's answer
http://answers.unity.com/comments/532376/view.html
Thanks for the advice.Sat, 07 Sep 2013 15:26:52 GMTBobfanAnswer by aldonaletto
http://answers.unity.com/answers/532318/view.html
If you want to rotate a vector, [multiply it by a Quaternion][1]. 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
}
[1]: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion-operator_multiply.htmlSat, 07 Sep 2013 13:45:38 GMTaldonaletto