- Home /
Object Rotating around center from Mouse Movement
Im trying to have the mouse cursor control an object the player holds so that the object rotates around the player. To give a better picture of what I am looking for, the player is the earth and the object is the moon. I want to have the mouse to move around and determine which way the moon rotates. keep in mind this is a 3rd person 3d game. Any help would be greatly appreciated.
2D or 3D? Do you want the mouse to control the angle are is it only controlling the direction, or do you just want horizontal movement of the mouse to spin the moon around the planet? What code (if any) are you using for making the planet orbit now?
this is 3D, I want to mouse to move around the player and the moon follow the mouse. I provided a picture to more precisely explain what im wanting. I have some code im working with, if possible Id like to avoid the raycast check of where the mouse colliders with the ground because i have multiple objects in the scene as well, but if not its not really an issue. This is what i have:
Ray rayTo$$anonymous$$ouse =Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(rayTo$$anonymous$$ouse, out hit))
{
if(hit.collider.tag == "Ground"){
float ang = $$anonymous$$athf.Atan2(hit.point.x-lookAtObj.transform.position.x,hit.point.x-lookAtObj.transform.position.x);
lookAtObj.transform.RotateAround(this.transform.position,Vector3.up,ang);
this.transform.LookAt(lookAtObj.transform.position);
}
}
Hi $$anonymous$$hoatic, Im trying to do the same now, I see that you did not get a satisfying answer.
Have you comed up with a solution yet?
regards
@murkertrer - I did not answer this question the one time I read it because it did not contain enough information. Even now with the additional information I still have questions. Here is a bit of code to get you started. It positions the object in orbit, but it does not change the object orientation. It expects a camera rotation of (0,0,0). You need initialize the 'center' variable through drag and drop.
#pragma strict
var center : Transform;
private var dist : float;
function Start() {
dist = (transform.position - center.position).magnitude;
}
function Update () {
var pos = Input.mousePosition;
pos.z = transform.position.z - Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
transform.position = (pos - center.position).normalized * dist;
}
If this is not what you want (and @khoatic doesn't give you what you want), I recommend opening a new question. Be very specific in your new question.
Answer by AW0610AUT · Dec 15, 2013 at 12:45 PM
I´m not really sure what you want but if you need the moon itself to rotate you can just use the Spin script that is already in unity.
var rotSpeed: float = 60; // degrees per second
function Update(){
transform.Rotate(0, rotSpeed * Time.deltaTime, 0, Space.World);
}
If you want the moon to follow the mouse aswell you can make it a child of your camera.
Please look at the comment above. Im wanting the Spin to follow the mouse. Im not looking for a simple spin that rotates with time though.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124217im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How this move my gear to the right and to the top with one click without falling 0 Answers
Rotate a vector around a certain point. 3 Answers
Quaternion reset rotations didnt smooth 0 Answers
How Do I add rotations to one of my objects in my AR application? 1 Answer
How can I pause the first person character camera rotation in this case?? 1 Answer