Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jaaaaamie · Apr 09, 2012 at 02:13 PM · cameratransformpositioncontrolleraxis

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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;
 }

Comment
Add comment · Show 19 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kleptomaniac · Apr 09, 2012 at 04:17 PM 0
Share

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

avatar image jaaaaamie · Apr 10, 2012 at 03:45 PM 0
Share

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'

avatar image Kleptomaniac · Apr 10, 2012 at 03:54 PM 1
Share

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.

avatar image AlucardJay · Apr 10, 2012 at 03:56 PM 0
Share

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

avatar image jaaaaamie · Apr 13, 2012 at 10:39 AM 1
Share

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!

Show more comments
avatar image
0

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;
    }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Following a gameObject, unexpected problems? 1 Answer

Trouble converting transform.position to C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges