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 darthbator · Apr 22, 2014 at 05:03 AM · lerp

Lerp / SmoothDamp complete before t=1

I have recently noticed what I would consider to be some strange behavior in one of my games. I am noticing that when I move an object/change a value using Mathf.SmoothDamp or Vector2/3.Lerp the value appears to reach it's "to" value before the t variable ever hits 1!

Here's an example! I'm using this code to teleport objects around the screen. I'm normally moving the object around 1-2 units on one axes. I am seeing the object movement complete .61f that seems odd right?

 public IEnumerator teleport (Vector2 direction) {
         Debug.Log("start");
         direction = new Vector2(transform.position.x + direction.x, transform.position.y + direction.y);
         direction *= teleportationLength;
         Vector3 destinationV3 = new Vector3(direction.x, direction.y, transform.position.z);
         float teleportationProgress = 0f;
         sprite.enabled = false;
         StartCoroutine(CameraMan.Instance.zoomTo(Camera.main.orthographicSize + teleportationCameraOffset));
         while (teleportationProgress < 1) {
             if (transform.position == destinationV3)
                 Debug.Log("DONE: " + teleportationProgress);
             transform.position = Vector2.Lerp(transform.position, direction, teleportationProgress);
             teleportationProgress += Time.deltaTime * teleportationSpeed;
             yield return null;
         }
         sprite.enabled = true;
         StartCoroutine(CameraMan.Instance.zoomTo(Camera.main.orthographicSize - teleportationCameraOffset));
         Debug.Log("stop");
     }
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 flamy · Apr 22, 2014 at 06:04 AM

lerp should be from startPosition to endPosition, zero on time is at startPosition and 1 will be at endPosition.... having a variable in one of this will result in odd behaviour. In your case you are using transform.position which is variying every frame, meaning your start position changes every frame or after every time the position changes. and for telePortation you have complicated your logic more than necessary...

I suppose your direction is +x or -x so have Vector3.right or left for direction and multiply it with teleporation distance, and add it with current transform position. Now this is the destination and current position is your start position. and now it would me much more simple, check the code below....

 public IEnumerator teleport (Vector2 direction) {
       
        direction *= teleportationLength;
 
        Vector3 destinationV3 = transform.position+direction;
 
        Vector3 startPosition = transform.position;
 
        float teleportationProgress = 0f;
 
        sprite.enabled = false;
        StartCoroutine(CameraMan.Instance.zoomTo(Camera.main.orthographicSize + teleportationCameraOffset));
        while (teleportationProgress < 1) {
          if (transform.position == destinationV3)
           Debug.Log("DONE: " + teleportationProgress);
          transform.position = Vector2.Lerp(startPosition , destinationV3 , teleportationProgress);
          teleportationProgress += Time.deltaTime * teleportationSpeed;   // you can have it as   Time.deltaTime/TimeTakenForTeleportation.... this works fine too... but here idk wat yor teleportation speed value is... hope it works fine.
          yield return null;
        }
        transform.position = destinationV3 ;
        sprite.enabled = true;
        StartCoroutine(CameraMan.Instance.zoomTo(Camera.main.orthographicSize - teleportationCameraOffset));
        Debug.Log("stop");
     }

PS: this is untested code!!

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 darthbator · Apr 22, 2014 at 06:56 PM 0
Share

@!$%!@#! Thank you so much man! What a bone head mistake I was making!

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

22 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

Related Questions

Lerp - Time Remaining 1 Answer

If a rigidbody is attached, should you always use AddTorque/AddRelative force for rotation/movement? 1 Answer

rigidbody.velocity vs lerp efficiency 0 Answers

Lerping Camera for character movement. 1 Answer

Why is this coroutine only firing once? 3 Answers


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