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 Mansquatch · Nov 16, 2012 at 04:58 AM · cameraguilerpbuttons

How to lerp between cameras on a UI button click?

I am trying to set up a UI button that, when the user clicks it, it calls to a camera manager that will either lerp from one camera object to another, fade-in/fade-out to another camera object, or just snap to another camera object. The problem I am having is that the co-routine I am calling when the button is clicked does not seem to wait() and allow the lerp to finish. The camera moves a bit when I click but it seems to get interrupted, which is what I thought the co-routine yield would prevent. This is the OnGUI() function code that is in my HUD_mgr script:

     void OnGUI(){
         if(!next_Btn){
             Debug.LogError("Please assign a texture on the inspector");
             return;
         }
         if(GUI.Button(new Rect(10,10,50,50), next_Btn))
         {
             Debug.Log("Clicked the button. Yippee...");
             camera_Mgr.SendMessage("switchCamera");
         }
     }

and here is the switchCamera() function code that is within my Camera_mgr script:

IEnumerator switchCamera(){ var duration = Time.deltaTime*3;

         Vector3 pos = main_Camera.transform.position;
         Quaternion rot = main_Camera.transform.rotation;
         main_Camera.transform.position = Vector3.Lerp(pos, second_Camera.transform.position, duration);
         main_Camera.transform.rotation = Quaternion.Lerp(rot, second_Camera.transform.rotation, duration);
         yield return new WaitForSeconds(duration);
     }

I am guessing that the fact that OnGUI() is called several times a frame is what is stomping my camera lerp just after it starts. However, if the yield does not work then what should I do? FYI, the snap-to works just fine. It's the lerp and fade-in/fade-out that do not seem to complete properly.

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
1
Best Answer

Answer by Tim-Michels · Nov 16, 2012 at 08:02 AM

I took a look at your code, and what you're doing wrong is, you don't set the position and rotation every frame. It only gets set once, whereafter your WaitForSecondsis called. After that WaitForSeconds, your Coroutine will stop and nothing will happen further with your camera.

You should also know that the third parameter of the Lerp function should run from 0 to 1 if you're animating something like this, but to illustrate this to you, I've re-written your coroutine. You should check if this works, but also really try to understand what's happening.

I use this technique all the time when animating stuff, and it's a really powerfull way of doing a short animation sequence.

 IEnumerator switchCamera()
     {
         var animSpeed = 0.5f;
 
         Vector3 pos = main_Camera.transform.position;
         Quaternion rot = main_Camera.transform.rotation;
 
         float progress = 0.0f;  //This value is used for LERP
 
         while (progress < 1.0f)
         {
             main_Camera.transform.position = Vector3.Lerp(pos, second_Camera.transform.position, progress);
             main_Camera.transform.rotation = Quaternion.Lerp(rot, second_Camera.transform.rotation, progress);
             yield return new WaitForEndOfFrame();
             progress += Time.deltaTime * animSpeed;
         }
 
         //Set final transform
         main_Camera.transform.position = second_Camera.transform.position;
         main_Camera.transform.rotation = second_Camera.transform.rotation;
     }

Please give a thumbs up/mark as answered if this helped you out. Good luck ;)

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
avatar image
0

Answer by Seth-Bergman · Nov 16, 2012 at 07:57 AM

The problem here is that using WaitForSeconds does not cause the method to loop.. It just pauses within the method call:

 IEnumerator Example(){
 //do something
 yield return new WaitForSeconds(1);
 //do a second thing after one second
 }//  The end

In your example, the yield is pointless, since nothing happens after it..

Second, you are simply moving your main camera to the position of your other camera, not switching cameras as stated.. Let's start with the simplest possible method, to as you put it "just snap" to the other camera:

  void OnGUI(){
        if(!next_Btn){
          Debug.LogError("Please assign a texture on the inspector");
          return; 
        }
        if(GUI.Button(new Rect(10,10,50,50), next_Btn))
        {
          Debug.Log("Clicked the button. Yippee...");
          camera_Mgr.cameraOne = !camera_Mgr.cameraOne;
        }
     }

And the camera script:

 public boolean cameraOne = true;
 public Camera main_Camera;
 public Camera second_Camera;

 void Update(){ 
       if(cameraOne){
          main_Camera.enabled = true;
          second_Camera.enabled = false;
          }
       else{
          main_Camera.enabled = false;
          second_Camera.enabled = true;
          }
    }

By toggling a variable (cameraOne) , we are making the current camera state easily available.. then we just check cameraOne and turn on the appropriate camera (based on whether it is true or false). This will also make the lerping thing easy, since we can just use Update()..

camera script version 2:

 public boolean cameraOne = true;
 public Transform positionA;   // you could just use an empty GameObject for these
 public Transform positionB;   // a camera will work too, but it's less efficient
 private Transform target;
 
 public Camera main_Camera;

 void Update(){ 
     float duration = Time.deltaTime*3;             
     Vector3 pos = main_Camera.transform.position;
     Quaternion rot = main_Camera.transform.rotation;

       if(cameraOne)
        target = positionA;
       else
        target = positionB;

        main_Camera.transform.position = Vector3.Lerp(pos, target.position, duration);
        main_Camera.transform.rotation = Quaternion.Lerp(rot, target.rotation, duration);
    }

for fading between cameras, maybe have a look at this:

http://wiki.unity3d.com/index.php?title=CrossFadePro

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

11 People are following this question.

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

Related Questions

Customizing GUI buttons 1 Answer

ignore left mouse button click in game when clicking on gui buttons 1 Answer

How to link gui buttons to seperate scripts? 1 Answer

3D menu camera rotation issue. 0 Answers

Smooth Camera Angle Scroll 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