Rotating Object Around Camera Perspective

Hello Unity Forum,

I’m fairly new to Unity and to scripting in Unity and I am having some difficulty with a rotation script I have on an object. My scene has a cube along with a camera facing the cube slightly off center to the right of it. The script I have attached to the cube is set up to rotate the cube along it’s X and Y axis based on the mouse’s X and Y positions once the mouse is clicked down.

Here is the JavaScript I am using to control the object:

function Update () {
if(Input.GetButton(“Fire1”)){
__ transform.Rotate(Vector2(-(Input.GetAxis(“Mouse Y”))6,-(Input.GetAxis(“Mouse X”))6));__
** }

}

This script is working great, but I realize it does not accomplish what my true intentions are. What I am intending to do is rotate the object in X and Y based on the X and Y of what the camera is seeing without translating its position. If you click and move down the cube rotates down in respect to what down is to the camera. If you release the mouse and then click and move to the right the cube rotates to the right to what right is in respect to the camera. The idea being that the cube stays in the same spot but can be moved three dimensionally with a series of mouse clicks and movements to get it to rotate around the scene. If anyone can give me some tips, advice, examples of any code that would help achieve this I would be eternally grateful! If this is confusing at all I can help provide additional information.

Thank you

  • Alan

I’m not quite sure, but it sounds like what you might be looking for is what’s commonly called ‘arcball’ rotation. If so, searching these forums, Unity Answers, and/or the internet in general for that term should lead you to some references on the topic. (I don’t have any links handy to post here, but maybe someone else will.)

Hey Jesse, I did some searching around about trackball /arcball rotation scripts and found some good examples. This is exactly what I was looking for, thank you so much for your help, I was racking my brain all day on this.

  • Alan