- Home /
Change position of camera on scene load?
Hi,
I'm trying to get a camera, which uses the Unity character controller, to move to a certain position when a scene is loaded.
For example; in scene1, the camera starts in the centre, however to enter scene 2 the camera muse go through a door on the left of the map.
When the user wants to go back to scene1, from scene2, they should appear on the left side of scene1.
With other scripts I've removed the object from the actual screen, and had it loaded, or instantiated, everytime it is needed. This way, the camera wouldn't need to be moved, just loaded in in the right position.
As a trigger is activated for the scene change I was going with something like this:
Application.LoadLevel ("scene1"); camera.position = (4,20,5);
I know this is wrong, and doesn't work, but I think I'm on the right path ( I hope ).
Could anyone lead me in more of the right direction?
Answer by AlucardJay · Apr 09, 2012 at 03:25 PM
While @gregzo method works, I am going to expand on it. If you have more than one door the player can enter the scene from, you may want to set the position and rotation of the camera for the next scene based on what door you are walking through.
I shall step out the methods.
Start with making a script for the camera. The name of this script is important to make the second script work. Call it -> CamSetLocation.js
// Main Camera Script
#pragma strict
public var CamNextLocation : Vector3;
public var CamNextRotation : Quaternion;
function Start()
{
DontDestroyOnLoad(this.gameObject);
}
public function OnLevelWasLoaded()
{
transform.position = CamNextLocation;
transform.rotation = CamNextRotation;
}
now for the example player script. this just goes in your normal script.
// Player Script
#pragma strict
var camScript : CamSetLocation; // CamSetLocation is the name of the script on the Main Camera
function Awake()
{
// load camScript with CamSetLocation.js (the script on the Main Camera)
camScript = GameObject.Find("Main Camera").GetComponent(CamSetLocation);
// move the camera
camScript.OnLevelWasLoaded();
}
function NextLevel()
{
// set the camera location before loading the new scene
camScript.CamNextLocation = Vector3(5, 1, -2);
camScript.CamNextRotation = Quaternion.Euler(35, 155, 0);
// Application.LoadLevel ("NextLevel");
}
Steps :
The player script finds the Main Camera and stores a reference to that script. Then you can change the position and rotation variables before you leave the scene (walk through a door).
So when you walk through a door, run the function NextLevel(), telling the camera it's next position and rotation. Then load the new scene.
In the new scene, the player script function Awake() calls the camera script function OnLevelWasLoaded() , to move the camera to the position and rotation set on leaving the last scene.
OR ... (and this is a separate answer, just an example on directly moving the camera without finding any scripts)
if the player knows where it is supposed to be in the new scene, you can directly tell the camera to move :
var TellCamNextLocation : Vector3 = Vector3(1, 2, 3);
function MoveCamera()
{
// directly move the Main Camera
Camera.mainCamera.transform.position = TellCamNextLocation;
}
A good answer @alucardj! Just some suggestions though mate. One is that this.gameObject can be simplified to gameObject (this really is rather pointless) and GameObject.Find("$$anonymous$$ain Camera") can be simplified to Camera.main (this defaults to the camera in the scene which is tagged with the "$$anonymous$$ain Camera" tag and is generally better than using strings). :P
Hi,
I tried using the code and got a error:
CamSetLocation.js(15,26): BCE0022: Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.Quaternion'.
I then looked into it and changed the line with an error to:
transform.rotation.eulerAngles.z
but this however, just changed the error to 'cannot convert UnityEngine.Vector3 to float'
In the first script change:
public var CamNextRotation : Vector3;
to
public var CamNextRotation : Quaternion;
and then in your second script change:
camScript.CamNextRotation = Vector3(35, 155, 0);
to
camScript.CamNextRotation = Quaternion.Euler(35, 155, 0);
You can't use the Vector3 class for rotation properties.
aah darn, @$$anonymous$$leptomaniac thanks mate, I did just copy-paste the rotation without thinking. Like I should be asleep now too. Actually , I shall edit the answer now. Thanks again.
Answer now correctly edited, thanks to @$$anonymous$$leptomaniac
Thanks so much for the help! After hours of adjustment I managed to get it all working.
As I have GUI scenes inbetween the normal scenes, I actually had to call the function to place the camera for the next scene after the Application.LoadLevel.
But yeah without your help I would be face down in a puddle of feces.
Thanks again!
Answer by gregzo · Apr 09, 2012 at 02:22 PM
You could do this for example, in your camera script:
DontDestroyOnLoad(this.gameObject);
function OnLevelWasLoaded(level : int)
{
if(level==1)
{
transform.position = level1CamPosition;
}
}
Your answer
Follow this Question
Related Questions
Camera movement 1 Answer
Place a object in front the camera in the coordinate 0 of Y axis 2 Answers
camera move x axis 2 Answers