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 Bentoon · May 14, 2015 at 01:58 AM · camera-movementlerpitweenpath

Lerping between Camera Paths

Helloooo

I have a camera with different iTween Paths on it

I am simply jumping back and forth by activating and deactivating them...

It's crude, I know, but it's actually the best solution so far...

But I want the Camara to LERP between the two paths instead of just Jumping immediately.

Here is the C# code so far:

Thanks for looking

 using UnityEngine;
 using System.Collections;
 
 public class switchITween : MonoBehaviour {
 
     
     Vector3 firstCam;
     Vector3 secondCam;
 
     void OnMouseUp() {
         // these are a bunch of Path scripts on the main camera that I am activating and deactivating 
         FlythroughCameraController1 a;
         FlythroughCameraControllerA b;
         a=Camera.mainCamera.GetComponent<FlythroughCameraController1>();
         b=Camera.mainCamera.GetComponent<FlythroughCameraControllerA>();
 
 //        float i = 0.0f;
 //        float rate = 1.0/Time;
 
 
 // These Activate and deactivate the path scripts, but I want that camera not just to JUMP to the newly active payh but Lerp there
         if (a.enabled == true) {
         Debug.Log ("B");                
         a.enabled = false;    
         b.enabled = true;
 //            while (i>1.0) { 
 //                i += Time.deltaTime * rate;
 //                Camera.main.transform.position = Vector3.Lerp(firstCam, secondCam, i);
 //
 //            }
         }
 //
 //
 //
         else if (b.enabled == true) {
         Debug.Log ("A");
             b.enabled = false;
             a.enabled = true;
 //            while (i>1.0){ 
 //                i+= Time.deltaTime * rate;
 //                Camera.main.transform.position = Vector3.Lerp(secondCam, firstCam, i);
 //
 //                }
             }
 //
     }
 }
 
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

1 Reply

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

Answer by Aeleck · May 15, 2015 at 07:04 AM

Vector3.Lerp function's third parameter is a number between 0 and 1. It looks like you dont execute unless i is already above 1 therefore the Lerp function doesn't have a chance to interpolate between your two points. Also, OnMouseUp is called once and Lerping is usually best done over several frames otherwise your camera will just go part ways to the second point and stop. So what I would suggest is using

 while(i < 1.0f) {
     i+= Time.deltaTime * rate;
     Camera.main.transform.position = Vector3.Lerp(secondCam, firstCam, i);
 }

Hope this helps! Good luck :)

Comment
Add comment · Show 18 · 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 Bentoon · May 15, 2015 at 08:02 PM 0
Share

Thanks Aeleck,

Now I get an error on this line:

     float rate = 1.0/Time;
avatar image Aeleck · May 15, 2015 at 08:26 PM 0
Share

Time is a class and cannot be mathematical used. You could actually just set rate to however many seconds you want the lerp to last since Time.deltaTime will make that happen and adding it to i with 1 / rate will keep the lerp constant (not exponential). If you want the lerp to last two seconds then try setting rate to 2 and then have the line

 i+= Time.deltaTime * rate;

become

 i += Time.deltaTime * 1/rate;

The reason we need to take 1/rate is because of the lerp needing to be between 0 and 1 and percentages and all that fun stuff. Also, if you put this in the update method you can get rid of the While loop, you dont want it all happening at once. Just check if the i is less than 1.

avatar image Bentoon · May 15, 2015 at 09:01 PM 0
Share

Aeleck

Thanks for your help.

This plays, but doesn't Lerp,

So I put the path switching after the Lerp with a wait for seconds in there

Still has trouble,

.. I am more concerned with is the fact that the mouse Up / Down are only working 15% of the time on $$anonymous$$obile. Crazy

 using UnityEngine;
 using System.Collections;
 
 public class switchITween4 : $$anonymous$$onoBehaviour {
 
     
     //variable to hold the point where two paths intersect to Lerp to
     public    Vector3 newCamPos;
 
     float i = 0.0f;
     public float rate = 2f;
 
     void On$$anonymous$$ouseDown() {
         // these are a bunch of Path scripts on the main camera that I am activating and deactivating 
         Debug.Log ("$$anonymous$$OUSE");                
 
         Vector3 CamPos = Camera.main.transform.position;
 
 
         FlythroughCameraController1 a;
         FlythroughCameraControllerA b;
         a=Camera.mainCamera.GetComponent<FlythroughCameraController1>();
         b=Camera.mainCamera.GetComponent<FlythroughCameraControllerA>();
 
 
 // These Activate and deactivate the path scripts, but I want that camera not just to JU$$anonymous$$P to the newly active payh but Lerp there
         if (a.enabled == true) {
         Debug.Log ("B");                
 
             while(i < 1.0f) {
                 i+= Time.deltaTime * 1/rate;
                 Camera.main.transform.position = Vector3.Lerp(CamPos, newCamPos, i);
             }
 
             StartCoroutine (myWait ());
             a.enabled = false;    
             b.enabled = true;
 
         }
 
 
         else if (b.enabled == true) {
         Debug.Log ("A");
 
             while(i < 1.0f) {
                 i+= Time.deltaTime * 1/rate;
                 Camera.main.transform.position = Vector3.Lerp(CamPos, newCamPos, i);
             }
 
             StartCoroutine (myWait ());
             b.enabled = false;
             a.enabled = true;
             }
     }
 
 
     IEnumerator myWait()
     {
         Debug.Log ("myWait");
         yield return new WaitForSeconds (2.0f);
     }
 
 
 ]
 
 //    void OnApplicationQuit ()
 //    {
 //    FlythroughCameraController1 a;
 //    FlythroughCameraControllerA b;
 //    a=Camera.mainCamera.GetComponent<FlythroughCameraController1>();
 //    b=Camera.mainCamera.GetComponent<FlythroughCameraControllerA>();
 //        a.enabled = true;
 //    }
 
 
avatar image Aeleck · May 15, 2015 at 09:09 PM 0
Share

You've got to move the lerping into the Update method and get rid of the While statements. Try this:

 using UnityEngine;
 using System.Collections;
      
 public class switchITween4 : $$anonymous$$onoBehaviour {
          
      //variable to hold the point where two paths intersect to Lerp to
     public    Vector3 newCamPos;
      
     float i = 0.0f;
     public float rate = 2f;
     
     private bool lerping = false;
     
     void Update() {
         if(lerping) {
              // These Activate and deactivate the path scripts, but I want that camera not just to JU$$anonymous$$P to the newly active payh but Lerp there
              if (a.enabled == true) {
                  Debug.Log ("B");                
                  if(i < 1.0f) {
                      i+= Time.deltaTime * 1/rate;
                      Camera.main.transform.position = Vector3.Lerp(CamPos, newCamPos, i);
                  } else if(i >= 1.0f) {
                      lerping = false;
                      i = 0.0f;
                      a.enabled = false;
                      b.enabled = true;
                  }     
      
                  StartCoroutine (myWait ());
              } else if (b.enabled == true) {
                  Debug.Log ("A");
                  if(i < 1.0f) {
                      i+= Time.deltaTime * 1/rate;
                      Camera.main.transform.position = Vector3.Lerp(CamPos, newCamPos, i);
                  } else if(i >= 1.0f) {
                      lerping = false;
                      i = 0.0f;
                      b.enabled = false;
                      a.enabled = true;
                  }     
                  StartCoroutine (myWait ());
                  }
              }
          }
      }
 
      void On$$anonymous$$ouseDown() {
           // these are a bunch of Path scripts on the main camera that I am activating and deactivating 
           Debug.Log ("$$anonymous$$OUSE");                
      
           Vector3 CamPos = Camera.main.transform.position;
      
      
           FlythroughCameraController1 a;
           FlythroughCameraControllerA b;
           a=Camera.mainCamera.GetComponent<FlythroughCameraController1>();
           b=Camera.mainCamera.GetComponent<FlythroughCameraControllerA>();
      
      
           lerping = true;
     }
      
      
     IEnumerator myWait() {
          Debug.Log ("myWait");
          yield return new WaitForSeconds (2.0f);
     }
      
      
      //    void OnApplicationQuit ()
      //    {
      //    FlythroughCameraController1 a;
      //    FlythroughCameraControllerA b;
      //    a=Camera.mainCamera.GetComponent<FlythroughCameraController1>();
      //    b=Camera.mainCamera.GetComponent<FlythroughCameraControllerA>();
      //        a.enabled = true;
      //    }
 }

Sorry about all the updates. Had to fix a bunch of my own erros. xD

avatar image Bentoon · May 16, 2015 at 01:25 AM 1
Share

Thank you Aeleck! I stepped away and will look at this when I get back

I really appreciate your thoroughness & help!

best

~be

Show more comments

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

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

Related Questions

put Camera on path with touch &/or arrow keys 1 Answer

ITween orienttopath x-y-z? 1 Answer

iTween makes a weird start on the path 1 Answer

how to cancel iTween Catmull-Rom path 1 Answer

tween Path animation & touch control 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