- Home /
Unity Rotate Around and Orbit Question
Hi all,
I am making a scene with 3D cube on top e.g. (something like the scene view) top-right hand corner 3d cube, there are top,down,left,right cones buttons to allow user switch the view accordingly.
I am rotating a camera, that will rotates the cube similar to the scene view (cube you see in the top right hand corner) But my cube will be used during run-time.
However I am facing several problems with Rotating Around. I noticed when clicking on left and right buttons of the scene cube, it actually rotates the scene left and right around a point. When click on top/bottom view, it will go to "Top/Front" "Bottom/Back" view accordingly. And dragging right mouse button, the cube will also rotate to camera's rotation.
I did half of the following 1. Dragging right mouse button, cube will rotate according to camera rotation. 2. Rotate around left and right, however, it doesn't seems to work accordingly because I don't know where to stop the rotation e.g. should I stop at 90 degrees? Because the rotation can be applied anywhere e.g. the cube is with rotation of 90x,90y,20z, when i press left, it should Rotate Around and move towards the left view of the scene. 3. Top/Down rotation, I need some guidance on the implementation.
I am currently using Unity Rotate Around function, however it has some problems as I do not know where to stop the rotation.
I tried using Quarternion.Slerp(TOP_ROTATION/LEFT_ROTATION /FRONT_ROTATION) etc. But it is not so correct, because it does not Rotate Around a pivot point.
Anyone know how to properly code this camera rotation similar to what we have in the scene view? E.g. clicking left/right/top/bottom will go to the appropriate rotation (with rotate around pivot functionality).
My Code
private float m_degreesPerSec = 90f;
private Vector3 m_pivot = Vector3.zero;
private float m_totalRotationAngleSoFar = 0f;
private bool m_startRotation = false;
private void Update()
{
// problem about this is, it will always rotate 90 degree. e.g. if the cube is half tited, and I want it to rotate around a axis, I not sure how to get the desired rotated degrees
if(m_totalRotationAngleSoFar >= m_degreesPerSec )
{
m_startRotation = false;
}
if(m_startRotation )
{
if (m_currentView == CubeViewMode.TOP)
{
MyCamera.transform.RotateAround (m_pivot, Vector3.right, m_degreesPerSec * Time.deltaTime );
}
else if (m_currentView == CubeViewMode.LEFT)
{
MyCamera.transform.RotateAround (m_pivot, Vector3.up, m_degreesPerSec * Time.deltaTime );
}
else if (m_currentView == CubeViewMode.RIGHT)
{
MyCamera.transform.RotateAround (m_pivot, Vector3.down, m_degreesPerSec * Time.deltaTime );
}
else if (m_currentView == CubeViewMode.BOTTOM)
{
MyCamera.transform.RotateAround (m_pivot, Vector3.left, m_degreesPerSec * Time.deltaTime );
}
m_totalRotationAngleSoFar += Time.deltaTime * m_degreesPerSec ;
}
}
// When user clicks on top/right/left/bottom button besides the cube, this function will be call.
public void StartRotation()
{
m_startRotation = true;
m_totalRotationAngleSoFar = 0f;
}