• 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 cheliotK · Dec 16, 2013 at 03:34 PM · transformvector3lerpvector3.lerp

Vector3.Lerp completing before t = 1?

Hello all, I think I broke Vector3.Lerp (either that, or I'm doing something very wrong)

I want to set up a simple camera path to zoom in, by lerping its transform. I want to have a modifiable total duration, for testing purposes, so I set up this script to test it. The problem is that the lerp completes way before the t parameter reaches 1, and I have no idea why. This is the code I'm using, which by all accounts should be working:

 if(camZoom){
         timeInZoom = Time.time-zoomStartTime;
         timeClamped = timeInZoom/zoomDur;
         transform.position = Vector3.Lerp(startTransform.position,endTransform.position,timeClamped);
         transform.eulerAngles = Vector3.Lerp(startTransform.eulerAngles,endTransform.eulerAngles,timeClamped);
         if(yPrev==transform.position.y){
             Debug.Break();
         }
     }
     yPrev = transform.position.y;

When I use a desired total zoom duration (zoomDur variable) of 8 sec, the Debug.Break() kicks in at 2.1 seconds in the lerp (timeClamp shows a value of 0.26 in the Inspector at that point). As you can see, I'm using the timeClamped var to control the Lerp, and for the life of me I can't figure out why it completes when its t parameter is still 0.26! I have confirmed that the camera has actually reached its target Transform at that point, so the Vector3.Lerp works properly, but in a quarter of its desired time.

Any help please?

Comment
Add comment · Show 2
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 robertbu · Dec 16, 2013 at 03:40 PM 0
Share

I see nothing on the surface wrong with this code unless startTransform is your transform. If it is, you are mixing two different Lerp uses since 'startTransform.position' will be updated each frame. When varying 't' between 0 and 1, both the start and end positions need to remain constant.

avatar image cheliotK · Dec 16, 2013 at 03:46 PM 0
Share

Hi robertbu,

No, startTransform is recorded once when the lerp begins, and is the camera's Transform at the beginning of the lerp; after that point it is never reassigned a new value.

2 Replies

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

Answer by robertbu · Dec 16, 2013 at 04:05 PM

Variables can refer to their data either by reference or by value. Transforms are referred to 'by reference'. The above code moves the camera, and 'startTransform' gets assigned the transform of the camera. Since you have a reference, each time the camera position changes, 'startTransform.position' will change.

Vector3s are by value. If you look in the scripting reference you will see the word 'struct'. This indicates a variable that is handled 'by value'. The solution to your problem is to make your start and end in the Lerp() Vector3s. At the point you start the Lerp you would do:

 var startPos = transform.position;
 var endPos = endTransform.position;

Then your Lerp will be:

 transform.position = Vector3.Lerp(startPos,endPos,timeClamped);

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 cheliotK · Dec 16, 2013 at 04:18 PM 0
Share

thanks! also for anyone interested, see my answer for working script.

avatar image
0

Answer by cheliotK · Dec 16, 2013 at 04:09 PM

Hmm, problem is (weirdly) solved, after robertbu's comment. Here's the full working Update() function:

 function Update () {
     
     if(Input.GetKeyUp(KeyCode.Space) && !zoomStarted){
         startTransform = GameObject.Find("start").transform;
 //        startTransform = transform;            //this one was giving the problem
         zoomStartTime = Time.time;
         zoomStarted = true;
         camZoom = true;
     }
     
     if(camZoom){
         timeInZoom = Time.time-zoomStartTime;
         timeClamped = timeInZoom/zoomDur;
         transform.position = Vector3.Lerp(startTransform.position,endTransform.position,timeClamped);
         transform.eulerAngles = Vector3.Lerp(startTransform.eulerAngles,endTransform.eulerAngles,timeClamped);
         if(yPrev==transform.position.y){
             Debug.Break();
         }
     }
     yPrev = transform.position.y;
 }

I created an empty GameObject and positioned it at the start location, and used that object's Transform as the startTransform. This seemed to solve the problem.

However, I still can't figure out why the commented line was giving me a problem. As I understand it, if I was using the "startTransform = transform;" line instead of "startTransform = GameObject.Find("start").transform;", what should happen is that the startTransform var should only be assigned the value ONCE, since it's inside an if statement with a key press. For some reason though, it must have kept assigning it the camera's new transform values each frame, hence making the lerp faster.

If someone would care to elaborate, I would be interested in knowing why the previous approach didn't work. (Also, thanks to robertbu for the suggestion that led to the solution!)

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 robertbu · Dec 16, 2013 at 04:10 PM 0
Share

Don't need the empty game object. Just use Vector3s in the Lerp().

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 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

Related Questions

Lerp not working 1 Answer

Vector3.Lerp stick halfway 2 Answers

How to use Vector3.Lerp without slow-down 3 Answers

why one works, the other does not 2 Answers

How the heck does Mathf.Lerp work? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges