Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 g0th1k4 · Jul 13, 2011 at 07:17 PM · camerapositionreturn

Return camera to original position

Hi there

I'm currently creating a game and I'm having some trouble with camera movements. The camera starts with being able to see the whole stage during the menu navigation and once the user hits play game, the camera zooms closer to the object on the stage so you can only see the table (I'm making a cup and ball game) as opposed to the whole room.

 SetCameraPosition(Vector3(GameObject.Find("CamPosGame").transform.position.x, GameObject.Find("CamPosGame").transform.position.y, GameObject.Find("CamPosGame").transform.position.z), Vector3(0, GameObject.Find("CamTargetGame").transform.position.y, 0),0); 


 function SetCameraPosition( positionTarget : Vector3, facingTarget : Vector3, buffer : float )
 {    
     //Move the camera towards the Altar, with easing
     
     //
     //var distanceTravelled : float = Vector3.Distance( camStartPos, Camera.main.transform.position ); //How far is left?
     //var totalDistance : float = Vector3.Distance( camStartPos, positionTarget ); //Total distance?
     //var moveVar : float = distanceTravelled / totalDistance;
     
     var distanceTravelled : Vector3;
     var totalDistance : Vector3;
     var moveVar : Vector3;
     
     distanceTravelled.y = Vector3.Distance( Vector3( 0, camStartPos.y, 0 ), Vector3( 0, Camera.main.transform.position.y, 0 ) );
     totalDistance.y = Vector3.Distance( Vector3( 0, camStartPos.y, 0 ), Vector3( 0, positionTarget.y + 0.1, 0 ) );
     moveVar.y = distanceTravelled.y / totalDistance.y;
     
     distanceTravelled.z = Vector3.Distance( Vector3( 0, 0, camStartPos.z), Vector3( 0, 0, Camera.main.transform.position.z) );
     totalDistance.z = Vector3.Distance( Vector3( 0, 0, camStartPos.z), Vector3( 0, 0, positionTarget.z + 0.1) );
     moveVar.z = distanceTravelled.z / totalDistance.z;
     
     //Debug.Log( "Distance Travelled : " + distanceTravelled.z );
     //Debug.Log( "Total Distance : " + totalDistance.z );
     //Debug.Log( "Cam Start Pos : " + camStartPos.z );
     //Debug.Log( "Position Target : " + positionTarget.z );
     //Debug.Log( "Move Var : " + moveVar.z );
     
     //Setup Z movement
     if( moveVar.z == 0 )
         moveVar.z = 0.1;
     
     moveVar.z = moveVar.z * 2;
     
     if( moveVar.z > 1 )
     {
         moveVar.z = moveVar.z % 1;
         moveVar.z = 1 - moveVar.z;
     }
     
     if( moveVar.z == 0 )
     {
         moveVar.z = 0.1;
     }
         
     moveVar.z = Mathf.Clamp( moveVar.z, 0, 0.5 );
     
     //Debug.Log( moveVar.z );
     
     if( camStartPos.z > positionTarget.z ) //Check for neg or pos direction
     {
         moveVar.z *= -1;
     }
     
     Camera.main.transform.position.z += moveVar.z * Time.deltaTime * camSpeed;
     
     
     if( Camera.main.transform.position.y > positionTarget.y)
     {
         //Setup Y movement
         if( moveVar.y == 0 )
         moveVar.y = 0.1;
         
         moveVar.y = (moveVar.y) * 2;
         
         if( moveVar.y >= 1 )
         {
             moveVar.y = moveVar.y % 1;
             moveVar.y = 1 - moveVar.y;
         }
         
     //    if( moveVar.y == 0 )
     //{
     //        moveVar.y = 0.1;
     //    }
         
         moveVar.y = Mathf.Clamp( moveVar.y, 0, 0.5 );
         
         Camera.main.transform.position.y -= moveVar.y * Time.deltaTime * camSpeed;
     }
     
     //Debug.Log( moveVar );
     
     
     //Move the camera, smoothing on each end -- NOTE: This equation took a lot of revision, so don't mess with it unless you have a backup, and really know what you're doing.
     //Camera.main.transform.position.z +=  Mathf.Abs( (moveVar.z + 0.1) * Mathfx.Hermite( camStartPos.z, positionTarget.z, moveVar.z ) ) * Time.deltaTime * camSpeed;
     //Debug.Log(Mathf.Abs( (moveVar.z + 0.1) * Mathfx.Hermite( camStartPos.z, positionTarget.z, moveVar.z ) ) * Time.deltaTime * camSpeed);
     
     //if( Camera.main.transform.position.y >= positionTarget.y + buffer + 1 ) //Constrain so we don't fall too low
     //{
     //    Camera.main.transform.position.y -=  Mathf.Abs( (moveVar.y + 0.1) * Mathfx.Hermite( camStartPos.y, positionTarget.y, moveVar.y ) ) * Time.deltaTime * camSpeed;
     //}
     
     //if( Camera.main.transform.position.y >= positionTarget.y ) //Constrain so we don't fall too low
     //{
     //    Camera.main.transform.position.y -= 0.035;
     //}
     
     //Rotation
     var targetRotation = Quaternion.LookRotation( facingTarget + Vector3( 0, 0, 0) - Camera.main.transform.position, Vector3.up ); //Find the rotational vector that will bring us to face the Target
     Camera.main.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime); //Rotate towards said target
     
     //Debug.Log( distanceTravelled.z + " :: " + totalDistance.z + " :: " + moveVar.z);
     //Check to see if we're in position yet
     
     if( Camera.main.transform.position.z >= (positionTarget.z) )
     {
         bSetGameCam = false; //once camera reaches positionTarget, stop game cam and reveal shells
         bButtonsActive = true; //activate buttons
     }
     
     if( Camera.main.transform.position.z <= (positionTarget.z)  )
     {
         if( bSetupEndChoice )
         {
             bSetupEndChoice = false;
             
         }
 
     }
     //if return button is selected, return to main menu position
     if (Camera.main.transform.position.z <= -13 && bReturn)
     {
         
         bReturn = false;
         bButtonsActive = false;
 
         
     }
     
 }

That's the code for the camera to zoom into the table. My question is I want the camera to zoom back out to the menu navigation position when the user hits the "return to main menu button"

How would I go about doing that?

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
0

Answer by almo · Jul 13, 2011 at 07:26 PM

I'm not positive, but it looks like you are using an overkill solution for your zoom. Have you looked at Vector3.Lerp, or Vector3.MoveTowards?

Comment
Add comment · Show 1 · 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 DaveA · Jul 13, 2011 at 07:40 PM 0
Share

Amen. Should be a pretty simple script

avatar image
0

Answer by DaveA · Jul 13, 2011 at 07:40 PM

You can grab the original fov value, stick it in a variable, and reset it to that value when you hit that button. Or feed it back through this function.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Camera movement 1 Answer

Set game object position relative to the Camera 1 Answer

how to have an object positioned relative to the screen(like a button) 1 Answer

Place a object in front the camera in the coordinate 0 of Y axis 2 Answers

How to move camera up and down 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