- Home /
Problem accessing the scene camera
Hello, I'm trying to access the editor's camera (also known as Scene Camera) through this simple code in an AWAKE() function:
Debug.Log("Camera.current = " + Camera.current);
... But it always returns "null", even though the scene camera panel is selected when I start the game through CTRL+P. Is that a known Unity bug or am doing something wrong?
Are you following unity's guideline to only use it for low rendering?
(http://docs.unity3d.com/Documentation/ScriptReference/Camera-current.html)
If not, I'll advise you to try and work with Camera.main
Well if I'm not mistaken, using Camera.main would get me the in-game player camera. Ins$$anonymous$$d, what I need is to get the scene camera's position and orientation as the game starts, so I can adjust my player camera accordingly.
What would be the correct method to do this, please?
Answer by GuyTidhar · Mar 13, 2013 at 12:45 PM
Your scene camera can be your game camera and you can mark it as the main camera by changing the 'tag' to 'MainCamera'.
Or a script attached to your player camera can have a public variable of type 'Camera' to which you drag the scene camera and from which you access it:
e.g.
var mainCam : Camera; // Drag the scene camera to this exposed variable once you attach this sctipt to the player camera
void Start()
{
transform.position = mainCam.position;
transform.rotation = mainCam.rotation;
}
Ah, forgive me but I don't even know how to see (or select) the Editor Scene camera in the hierarchy panel. The only camera in there is my in-game camera, which is indeed tagged as "$$anonymous$$ainCamera".
And I'm confused by your saying that "my scene camera can be my game camera". So far I have only one camera for my game (my "player camera"), which is visible and selectable in the hierarchy panel. But there is obviously also the default Unity in-editor scene camera (which again is invisible/un-selectable in the hierarchy panel). And THAT is the camera I'm interested in. Am I missing something here?
I see, my bad. I regarded the 'scene camera' you've mentioned as the default camera you get when creating a scene.
In this case, I am not aware of built in way to access this data.
But what are you trying to achieve by doing so?
I would like to use the editor scene camera to quickly go anywhere I please in my level, and then jump in-game; my player control script would then adjust the player's position to match the editor scene camera's position.
So in effect, the player would spawn wherever the editor scene camera is located, roughly matching its position and orientation.
The CryEngine works a bit like that. I mean you can have your standard player start spawn point of course, but you can also just jump in-game anywhere anytime. It's a great workflow. Very flexible. One that could easily be implemented in Unity if the editor camera could be queried for its world transform information.
Your answer
Follow this Question
Related Questions
Drawing Handles 1 Answer
Camera Zoom in Scene View 1 Answer
Scene View camera clipping 1 Answer