- Home /
Need help orbiting an object around another object based on rotation
I have a camera that I want to position upon a player-centric orbit; the position is based upon the camera's current x rotation. The initial x rotation is 0 and the z-offset is -10 (which is also the radius of the orbit sphere). Any help with the trigonometry of this would be appreciated.
Answer by Seth-Bergman · Jul 19, 2012 at 05:59 AM
hmm, kind of a tough one.. You want the camera to rotate around the player, always at the same distance, and always facing the player, correct?
If so, using transform.LookAt(playerObject.transform) takes care of the direction.. and transform.RotateAround(playerObject.transform,Vector3.up,speed) should handle the rotation.. I figure if you're always looking at the object, adjusting the distance to 10 should be pretty simple:
if(Vector3.Distance(transform.position,playerObject.transform.position) > 10)
//move forward
else if(Vector3.Distance(transform.position,playerObject.transform.position) < 10)
//move back
at least that's how I would probably try to go about it, avoiding trig altogether.. There are scripts out there too for this, including the mouseOrbit script in Standard Assets I believe.. search the forums for full examples.. If I mistook your objective, please clarify!
EDIT: My logic is that RotateAround works on a 360 degree rotation, same as rotating the camera.. if I know I want to rotate the camera 55 degrees, the ending position in my rotation around the object to stay facing it is the same reached by rotating around it by 55 degrees.. It wouldn't really matter if I rotate the camera and capture the corresponding position, or vice versa.. (but I may still be missing something in your needs...)
Unfortunately, this isn't really what I was looking for. What I want is to be able to supply an angle (e.g. -45/315) and have a calculated position returned. RotateAround doesn't work because that method only increments or decrements the current rotation, rather than setting it.
I was looking for something more like this:
Vector3 targetPos = new Vector3( (cameraTarget.transform.position.x + OffsetX), (cameraTarget.transform.position.y + OffsetY) - ($$anonymous$$athf.Sin(transform.rotation.x) OffsetZ), cameraTarget.transform.position.z - ($$anonymous$$athf.Cos(transform.rotation.x) OffsetZ));
However, this doesn't exactly yield the results I'm looking for, although similar.
if you know how far though, you could set the increment exactly, by setting speed to the number of degrees, no?
i.e.
RotateAround(targetPosition,Vector3.up,angleInDegrees);
call once, and it would snap to the new position, I believe..
Anyway, sorry if I'm no help..
Your answer
![](https://koobas.hobune.stream/wayback/20220613074402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I combine Orbiting with mouse drag and with a button click? 0 Answers
Rotate camera to object on sphere 1 Answer
Touch mouseorbit 5 Answers
Mouse Orbit snapping issues 0 Answers
Help understanding transform 1 Answer