• 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 · Feb 21, 2017 at 09:25 PM · cameralerptime.deltatime

camera Lerp Silly Question

having a C# moment...

My Lerp is Not Lerping over time despite Time.Deltatime

It just SNAPS

My Speed Variable cuts off the Lerp unless it's like 100 (?) in my inspector

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Lerp : MonoBehaviour {
 
     public  Vector3 startPos;
     public Vector3 endPos;
     public float speed = .8f;
     public float counter = 0;
     public float target = 10;
 
 
 
     // Update is called once per frame
     void Update () {
     }
 
 
     void OnMouseUp()
     {
         //record where the Camera is
         startPos = Camera.main.transform.position;
         //make it go forward 1.8 units
         endPos = Camera.main.transform.position + Camera.main.transform.forward * 1.8f;
         // Move over time
         Camera.main.transform.position = Vector3.Lerp(startPos, endPos, (speed*Time.deltaTime));
         // set an IEnumerator Coroutine to wait
         StartCoroutine(MyWait());
         //reset counter when com back from coroutine, so we can use again
         counter = 0;
         target = 10;
 
 }
 
 
 
 
     IEnumerator MyWait()
     {
         // wait for seconds
         while (counter < target)
         {
             counter += Time.deltaTime; yield return null;
         }
         //put the Camera back to where it was
         Camera.main.transform.position = Vector3.Lerp(-endPos, startPos, (speed * Time.deltaTime));
     }
 
 
 
 }
 

THANKS For Looking

~be

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

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by Bentoon · Feb 22, 2017 at 11:31 PM

@tanoshimi Thank you Tanoshimi,

You are right but I am still having problems;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Lerp : MonoBehaviour {
 
     public  Vector3 startPos;
     public Vector3 endPos;
     public float speed = .8f;
     public float counter = 0;
     public float target = 10;
     public float timeTakenDuringLerp = 1f;
     public float distanceToMove = 1.8f;
     private float _timeStartedLerping;
     //Whether we are currently interpolating or not
     private bool _isLerping;
     private bool _isLerpingBack;
 
 
     // Update is called once per frame
     void Update () {
     }
 
 
     void OnMouseUp()
     {
         //record where the Camera is
         startPos = Camera.main.transform.position;
         //make it go forward 1.8 units
         endPos = Camera.main.transform.position + Camera.main.transform.forward * 1.8f;
         // Move over time
         _isLerping = true;
         // Camera.main.transform.position = Vector3.Lerp(startPos, endPos, (speed*Time.deltaTime));
         // set an IEnumerator Coroutine to wait
         StartCoroutine(MyWait());
         //reset counter when com back from coroutine, so we can use again
 
         counter = 0;
         target = 10;
 
 }
 
 
 
 
     IEnumerator MyWait()
     {
         // wait for seconds
         while (counter < target)
         {
             counter += Time.deltaTime; yield return null;
         }
         //put the Camera back to where it was
         // Camera.main.transform.position = Vector3.Lerp(-endPos, startPos, (speed * Time.deltaTime));
         _isLerping = false;
         _isLerpingBack = true;
     }
 
 
 
 
 
      void FixedUpdate()
     {
         if (_isLerping)
         {
             //We want percentage = 0.0 when Time.time = _timeStartedLerping
             //and percentage = 1.0 when Time.time = _timeStartedLerping + timeTakenDuringLerp
             //In other words, we want to know what percentage of "timeTakenDuringLerp" the value
             //"Time.time - _timeStartedLerping" is.
             float timeSinceStarted = Time.time - _timeStartedLerping;
             float percentageComplete = timeSinceStarted / timeTakenDuringLerp;
 
             //Perform the actual lerping.  Notice that the first two parameters will always be the same
             //throughout a single lerp-processs (ie. they won't change until we hit the space-bar again
             //to start another lerp)
             Camera.main.transform.position = Vector3.Lerp(startPos, endPos, percentageComplete);
 
             //When we've completed the lerp, we set _isLerping to false
             if (percentageComplete >= 1.0f)
             {
                 _isLerping = false;
             }
         }
         else if (_isLerpingBack)
         {
             float timeSinceStarted = Time.time - _timeStartedLerping;
             float percentageComplete = timeSinceStarted / timeTakenDuringLerp;
 
             Camera.main.transform.position = Vector3.Lerp(endPos, startPos, percentageComplete);
 
             //When we've completed the lerp, we set _isLerping to false
             if (percentageComplete >= 1.0f)
             {
                 _isLerpingBack = false;
             }
         }
     }
 
 
 }
 

Any ideas?

Thanks

~be

Comment
Add comment · Show 3 · 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 MarshallN · Feb 23, 2017 at 12:36 AM 0
Share

You're never setting _timeStartedLerping, so percentageComplete is just set to Time.time, which is considerably greater than 1 after the first second of runtime. Set _timeStartedLerping to Time.time when _isLerping or _isLerpingBack is set to make your script work.

avatar image Bentoon MarshallN · Feb 23, 2017 at 05:01 AM 0
Share

hello @$$anonymous$$arshallN

You dont mean something like :

  if (_isLerpingBack)
         {
             _timeStartedLerping = Time.time;
 
             float timeSinceStarted = Time.time - _timeStartedLerping;
             float percentageComplete = timeSinceStarted / timeTakenDuringLerp;
 
             Camera.main.transform.position = Vector3.Lerp(endPos, startPos, percentageComplete);
 
             //When we've completed the lerp, we set _isLerping to false
             if (percentageComplete >= 1.0f)
             {
                 _isLerpingBack = false;
             }
         }


Thanks!

~be

avatar image MarshallN Bentoon · Feb 23, 2017 at 03:01 PM 0
Share

No, no, actually nothing like that, haha - that would make timeSinceStarted equal to 0 at all times, since _timeStartedLerping would be set to Time.time every frame. What you want to do is set _timeStartedLerping whenever you toggle_isLerping or _isLerpingBack.

So, in your On$$anonymous$$ouseUp() function, set _timeStartedLerping = Time.time.

Then, your coroutine should look like

 IEnumerator $$anonymous$$yWait()
      {
          // wait for seconds
          while (counter < target)
          {
              counter += Time.deltaTime; yield return null;
          }
          //put the Camera back to where it was
          // Camera.main.transform.position = Vector3.Lerp(-endPos, startPos, (speed * Time.deltaTime));
          _isLerping = false;
          _isLerpingBack = true;
         _timeStartedLerping = Time.time; // THIS IS THE LINE I ADDED
      }


avatar image
0

Answer by hellojbb1234 · Feb 22, 2017 at 08:31 AM

Try increasing your speed variable I don't know exactly but I think if the Lerp is to low it will do what your describing, aka just snap into a position immediately.

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 tanoshimi · Feb 21, 2017 at 11:07 PM

You're using Lerp incorrectly because you're never incrementing the third parameter. This is a common problem and has bedn explained well before, e.g. at http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly

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.

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

93 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 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 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 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 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

Smooth camera movement 0 Answers

Smooth camera shift, Lerp? SmoothShift? 2 Answers

Third person Camera With Lerp trouble 0 Answers

How to move camera from point A to B 1 Answer

Problem with First-Person-Control and Quaternion.Lerp 0 Answers

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