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 answers for the question "Rotate a vector around a certain point."Answer 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 GMTFlightOfOneAnswer 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 GMTGuenter123987Answer 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