Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Reset camera to original start point?

Discussion in 'Editor & General Support' started by garyhaus, Feb 3, 2007.

  1. garyhaus

    garyhaus

    Joined:
    Dec 16, 2006
    Posts:
    601
    Looking for a little help on getting my camera to 'snap' back to its original position when I press a key. Anybody done this. I was thinking somehow of using the Application.LoadLevel (0) to get it back... I have no clue yet how to do this.

    Thanks,

    Gary
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Write and attach a script to your camera.

    In the script's Awake or Start function, add code to set a varialbe to the cameras's position
    Code (csharp):
    1.  
    2. //JS
    3. var myCamPos : Vector3;
    4.  
    5. function Start()
    6. {
    7.   myCamPos = transfrom.position;
    8. }
    9.  
    10. //C#
    11. public Vector3 myCamPos = Vector3.zero;
    12.  
    13. void Start()
    14. {
    15.   myCamPos = transform.position;
    16. }
    17.  
    Then you code a block to capture your key press (as shown in the docs) then just set your camera's transform.position to myCamPos.

    (NOTE: the JS example may be wrong, I never learned how to properly use the : ) ;-)

    -Jeremy
     
    pamz3d likes this.
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,338
    Assuming you're using the FPS controller, you probably want to attach that to the controller rather than the camera, or else the camera gets reset but the controller doesn't, which would lead to all manner of confusion. ;)

    Code (csharp):
    1.  
    2. private var startPosition : Vector3;
    3.  
    4. function Start () {
    5.     startPosition = transform.position;
    6. }
    7.  
    8. function Update () {
    9.     if (Input.GetButtonDown("reset")) {
    10.         transform.position = startPosition;
    11.     }
    12. }
    13.  
    ...for the whole thing. You could integrate that into the FPSWalker script if you don't want it as a separate script. You'd probably want to reset the camera rotation as well, so add this to the Update function of the MouseLook script:

    Code (csharp):
    1.  
    2. if (Input.GetButtonDown("reset")) {
    3.     rotationX = 0F;
    4.     rotationY = 0F;
    5.     transform.localRotation = originalRotation;
    6. }
    7.  
    --Eric
     
  4. garyhaus

    garyhaus

    Joined:
    Dec 16, 2006
    Posts:
    601
    Thank you!!! Works great. I also added a reset for the zoom on my camera as I added the ability for the user to adjust the FOV.

    Code (csharp):
    1.  
    2.         // Set the camera's FOV attached to the same game object to 60
    3.         if (Input.GetButtonDown("reset")) {
    4.             camera.fieldOfView = 60;
    5.              }
    6.  
    7.  
    8.  
     
unityunity