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!