/// <summary> /// From a Line, Get the quaternion representing the Vector p2 - p1 /// the up vector can be Vector3.up if you have no reference. /// </summary> /// <param name="p1">point 1</param> /// <param name="p2">point 2</param> /// <param name="upNormalized">default is Vector3.up</param> /// <returns>Quaternion representing the rotation of p2 - p1</returns> public static Quaternion QuaternionFromLine(Vector3 p1, Vector3 p2, Vector3 upNormalized) { Matrix4x4 rotationMatrix = ExtMatrix.LookAt(p1, p2, upNormalized); Quaternion rotation = rotationMatrix.ExtractRotation(); return(rotation); }
/// <summary> /// usage to rotate around a transform using this: /// Matrix4x4 currentTRSTransform = ExtMatrix.GetMatrixTRS(_currentOrbitter); /// Matrix4x4 newMatrixTrs = ExtMatrix.RotateAround(currentTRSTransform, _pivotTransform.position, _offsetRotate, Time.deltaTime * _speed); /// _currentOrbitter.FromMatrix(newMatrixTrs); /// </summary> /// <param name="customTransform"></param> /// <param name="center"></param> /// <param name="axis"></param> /// <param name="angle"></param> /// <returns></returns> public static Matrix4x4 RotateAround(Matrix4x4 customTransform, Vector3 center, Vector3 axis, float angle) { Vector3 pos = customTransform.ExtractPosition(); Quaternion rot = Quaternion.AngleAxis(angle, axis); // get the desired rotation Vector3 dir = pos - center; // find current direction relative to center dir = rot * dir; // rotate the direction Vector3 finalPosition = center + dir; // rotate object to keep looking at the center: Quaternion myRot = customTransform.ExtractRotation(); Quaternion finalRotation = myRot * Quaternion.Inverse(myRot) * rot * myRot; Vector3 localScale = customTransform.ExtractScale(); Matrix4x4 newTrsMatrix = ExtMatrix.GetMatrixTRS(finalPosition, finalRotation, localScale); return(newTrsMatrix); }