Rotate around object at a given angle

Hello, new to Unity here. Looked around everywhere but could’t find and answer. Thanks in advance for the help. I’m trying to rotate a camera around and object as I drag the mouse. The more I drag the mouse the faster it rotates. I don’t want it to rotate smoothly, I want it to rotate on increments of 45 degrees. I don’t know how to find the camera position values at a given angle. (I cannot parent the camera to the object and rotate the object – would require days of rewriting camera scripts). I DO know how to round/snap individual attributes after I find the correct position at a given angle. Here is the desired behavior except that it doesn’t snap: (written in boo)

import UnityEngine

class cameraRotation (MonoBehaviour): 
	
	public object_to_rotate_around as Transform
	private main_cam as Camera
	private initial_mouse_click as Vector3
	
	def Start ():
		main_cam = Camera.main
		//initial_mouse_click = Input.mousePosition
	
	def Update ():
		if Input.GetMouseButtonDown(0):
			initial_mouse_click = Input.mousePosition
		elif Input.GetMouseButton(0):
			mouse_delta = Input.mousePosition - initial_mouse_click
			main_cam.transform.RotateAround(object_to_rotate_around.position, Vector3.up, mouse_delta.x * 0.05)

Hi. I’m not a Boo programmer, so please excuse the syntax errors :wink: Basically you want to look for the modulus operator and apply. Change:

    elif Input.GetMouseButton(0):
         mouse_delta = Input.mousePosition - initial_mouse_click
         main_cam.transform.RotateAround(object_to_rotate_around.position, Vector3.up, mouse_delta.x * 0.05)

to

    elif Input.GetMouseButton(0):
         mouse_delta = Input.mousePosition - initial_mouse_click
        mouse_delta.x *= 0.05f;
        stored += mouse_delta.x;
        rem = stored % 22.5f;
        actualChange = stored-rem;
        stored = rem;
         main_cam.transform.RotateAround(object_to_rotate_around.position, Vector3.up, actualChange )

//You need to also declare float stored = 0 and float actualChange = 0; at the op of your program

For reference, here’s my test code in c# which I used to check it would work. Obviously my cube and cylinder will not work unless they’re in your project :wink:

Vector3 initial_mouse_click=Vector3.zero;
float stored = 0;

void Update () {  

    if (Input.GetMouseButtonDown(0))
    {
        initial_mouse_click = Input.mousePosition;
    }
    else if (Input.GetMouseButton(0))
    {
        Vector3 mouse_delta = Input.mousePosition - initial_mouse_click;
        GameObject go = GameObject.Find("Cylinder");
        GameObject cb = GameObject.Find("Cube");
        mouse_delta.x *= 0.05f;
        stored += mouse_delta.x;
        float rem = stored % 22.5f;
        float actualChange = stored-rem;
        stored = rem;
        go.transform.RotateAround(cb.transform.position, Vector3.up, actualChange);//mouse_delta.x);
    }
}

I apologise if I’ve used too many variables. It’s 1am, I’m tired and just don’t care anymore :wink:

Edit: Rearranged code to retain data lifetime

This works great! thank you!

This code should be outside of the update function:

Vector3 initial_mouse_click=Vector3.zero;
float stored = 0;
float actualChange = 0;