- Home /
Having trouble making my camera orbit an object
I've got simple planetary bodies orbiting a sun, that works fine. However, I have a fixed camera for each planet (every planet is a prefab combination of planet and child camera). I'm trying to get the camera to rotate around the planet, always facing it, by use of arrow keys.
if(Input.GetKey(KeyCode.LeftArrow)){
transform.RotateAroundLocal(Vector3(0, -CameraRotate, 0), CameraRotate * Time.deltaTime);
transform.localPosition(0, 0, 0);
}
The first line works just fine, and rotates the camera to the left. I have an identical code group that does the opposite.
My problem is the second line. I initially assumed that it would reference the camera itself, just like the line above. Unfortunately, I get the following error:
BCE0077: It is not possible to invoke an expression of type 'UnityEngine.Vector3'.
Unless I'm wrong, this means I'm not referencing the camera but instead attempting to tell the entire game to rotate. Is that right? I've tried every variation I can think of, from Object.tranform to gameObject.transform; they work, to a point, but I get a null error telling me an object is being referenced that isn't there (probably misremembering that).
I'm using java actually. I forgot to mention that.
I tried adding 'new' as you suggested, but it didn't change a thing. Everything seems to function exactly as without the 'new' there; the camera still just spins where it is, rather than rotating around its parent.
Answer by AlvinHerawan · Mar 19, 2013 at 02:50 AM
So far I've been exploring about camera scripting on the Asset Store. One solution I found was from the Camera Orbit Tool where you parent your camera to an empty game object and rotate that game object instead of the camera. This will give a distance between the camera and the object that the camera is orbiting.
So in your case, you might want to create an empty game object on each planet and parent your camera to that empty game object. You can then attach a script to rotate the parent, causing the camera to orbit each planet.
I hope I get your question right and get your problem solved.
Oh, that's an interesting idea! I'll have to give it a try.
In a little different condition, you might want to check on this thread: http://answers.unity3d.com/questions/418421/set-camera-orbit-rotation-on-mouse-direction-mouve.html
robertbu's accepted answer is really good.
Answer by robertbu · Feb 27, 2013 at 09:46 PM
Your problem is on the third code line. I think you want:
if(Input.GetKey(KeyCode.LeftArrow)){
transform.RotateAroundLocal(Vector3(0, -CameraRotate, 0), CameraRotate * Time.deltaTime);
transform.localPosition = Vector3(0, 0, 0);
}
Well that clears my error, and I see now what I did wrong with the line, so thanks for that.
However, this isn't doing what I'd hoped it would do. Rather than orbit my camera around its planet, always facing that planet, the camera simply spins around in place.
Is there something I'm missing about RotateAroundLocal?
Your answer
Follow this Question
Related Questions
Camera Orbit Rotation Problem 0 Answers
Slowly orbit camera on keypress 1 Answer
Help with camera movement using mobile input 0 Answers
Orbit around an object 0 Answers
How to rotate the camera smoothly for just 1 second? 2 Answers