Unity3D: rotate 3D models around the center with mouse

11 Nov


In Unity3D, you can use mouse to rotate 3D models with trivial code:image

However when you run the above code, you might find that rotation center (pivot, or anchor point) is usually located at one of the extreme vertices, not the geometric center of the object.

To solve the problem, attach the below script file MouseOrbit.cs to your 3D model imported (e.g. obj file), that is it!

The script is very simple, and contains two parts logically:

  • In the Start() function, it tries to find if the 3D model has a BoxCollider attached or not, and if not, it will programmatically create one and attach this component to it.


  • In the Update() function, the code finds the BoxCollider’s center, and rotate the object around the center of this bounding box.


Note: You should attach this to the geometric models imported. Suppose you created a place holder gameObject named “3DModels”, and the geometric model is called “ObjModelImported”, you should attach the script to the latter!


Downloads: MouseOrbit.cs | Unity3D demo project 


Posted by on November 11, 2013 in Programming, Unity 3d


Tags: , , , , , , , , , ,

8 responses to “Unity3D: rotate 3D models around the center with mouse

  1. Igor

    May 1, 2014 at 1:53 pm

    Wher is bound asigned???

  2. xinyustudio

    May 1, 2014 at 5:28 pm

    It is very clearly stated in Unity3D doc here: it represents an axis aligned bounding box.

  3. Ryan O'Connor

    June 5, 2014 at 6:32 am

    Hi, this code works but can be improved. You should not rotate around the world axis for y, it is more intuitive and much less error prone to rotate about local axis for y. To do so, modify the two “RotateAround” lines to read:

    transform.RotateAround(pivot, Vector3.up, dtx);
    transform.RotateAround(pivot, transform.right, dty);

  4. xinyustudio

    June 5, 2014 at 3:20 pm

    Cool! Nice to know this trick! Thanks.

  5. Mike

    November 14, 2014 at 10:03 pm

    Hello, I was glad to find your script. I applied it to my object .obj file which is a model of a brain. I was originally using KeyPress with vector3 up, down, left and right depending on the key actions and it would rotate fine along the ‘y’ axis but up or down it would not rotate just on the ‘x’ axis the ‘z’ axis would be involved. So the brain would not rotate in place on the ‘x’ axis it would go back or forward in space even though the ‘z’ axis was set to ‘0’. I thought your script would help this in viewing your letter Y example but when I applied your script to it and used the mouse the brain jumps all over the place and not rotate in place. My only thought of what it might be is the pivot for the brain is not exactly centered or at multiple verticies when it was created. Or the pivot on my scene is somewhere else even though I set it to ‘center” , ‘local’. Any thoughts?

    You can try to view it here:

  6. rocifier

    November 18, 2014 at 7:52 am

    Hi Mike, try checking your inspector window to see what level of the hierarchy is actually moving. There is also a plugin somewhere I think which renders pivot points in the unity editor window which may help (sorry its been lost on me). Apart from that I can’t see your web app, it says failed to download data for some reason.

  7. Mike

    November 25, 2014 at 12:06 am

    Thank you, I was able to figure it out and actually moved the center point of the object to it’s center through the script using this “ = new Vector3(0, 10, 0);” at line 32. Simple and effective.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: