- Home /
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.
I can think of half a dozen different ways you can approach this problem, but which one will depend on a number of different factors. What is the relationship of the camera to the objects (i.e. will it be looking down the Z axis for example)? Will the object being rotated around be moving in the scene? Will the rotation always be parallel to the plane of the camera?
Hey, I realize I should have been more precise with my questions, so to answer yours : It's a 2D game, we use the X & Y axis, the object does move in the scene (it's attached to the player), and yes the rotation will always be parallel to the plane of the camera, I've tried with the $$anonymous$$ouseOrbit script but I'm not really liking the end result.
is there a way of adding a speed variable on the second method?
Answer by robertbu · May 03, 2013 at 01:29 PM
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;
}
}
I can't Upvote yet, but i successfuly used your snippet to accomplish the desired behavior of this question, thanks!
If possible, can you elaborate on the math of this solution?
If possible, can you elaborate on the math of this solution?
Are you struggling with something specific? In the first solution I do:
Line 16 - I convert the position of the object to a screen position. This places the mouse and the object in the same coordinate system.
Line 17 - Subtract the v3Pos from the mouse position gives a vector from the object to the mouse...the object becomes the center of the 'world'.
Line 18 - We get the angle of this vector where Vector3.Right would be 0.0 degrees.
Your answer
![](https://koobas.hobune.stream/wayback/20220613101307im_/https://answers.unity.com/themes/thub/images/avi.jpg)