Rotate object around other object (or circle) using mouse

Hi everyone,

As the title suggests I’m trying to rotate an object around another object using the mouse. I’ve managed to rotate a cube around a capsule with this code :

    public float PlanetRotateSpeed = -0.005f;
    public float OrbitSpeed = 0.0005f;
    protected GameObject capsule;
    
    void Start () {
    	capsule = GameObject.Find("capsule"); 
    }
    	
    void Update () {
    	
        	transform.RotateAround (capsule.transform.position, Vector3.forward, OrbitSpeed* Time.deltaTime);
    }

So the cube rotates around the capsule as if it’s moving along a circle (or sphere), what I want to do (and can’t actually do) is keep the cube moving along the circle but not with “OrbitSpeed* Time.deltaTime” but depending on the mouse position.

Thanks in advance.

As mentioned in my comment, there are multiple different ways of achieving this behavior, but none are simple. It is easier as a 2D game looking down the Z axis.

using UnityEngine;
using System.Collections;

public class Around4 : MonoBehaviour {
	
	public Transform target;
	public float fRadius = 3.0f;
	private Transform pivot;

	void Start() {
		pivot = new GameObject().transform;
		transform.parent = pivot;
	}
	
	void Update () {
		Vector3 v3Pos = Camera.main.WorldToScreenPoint (target.position);
		v3Pos = Input.mousePosition - v3Pos;
		float angle = Mathf.Atan2 (v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;
		
		pivot.position = target.position;
		pivot.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
	}
}

Here is a second way that positions the object on the circle. The object is not rotated, so you would need to add a LookAt() if the object needs to be rotated:

using UnityEngine;
using System.Collections;

public class Around2 : MonoBehaviour {
	
	public Transform target; 
	public float fRadius = 3.0f;
	
	void Update () {
		Vector3 v3Pos = Camera.main.WorldToScreenPoint (target.position);
		v3Pos = Input.mousePosition - v3Pos;
		float angle = Mathf.Atan2 (v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;
		v3Pos = Quaternion.AngleAxis (angle, Vector3.forward) * (Vector3.right * fRadius);
		transform.position = target.position + v3Pos;
	}
}