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
1
Question by Team_26 · Dec 23, 2013 at 11:27 AM · transformpositionvector3.lerp

Vector3.Lerp doesn't work!

So i have a code:

 if (Input.GetKey (KeyCode.UpArrow))
 {
     float X = player.transform.position.x;
     float Y = player.transform.position.y;
     
     Vector3 Tunel_1 = new Vector3(X, Y, 0);
     Vector3 Tunel_2 = new Vector3(X, Y, 5);

     var position = Vector3.Lerp(Tunel_1, Tunel_2, Time.deltaTime * smooth);
     player.transform.position = position;

And I don't know why does my lerp not work. Have you got any ideas?

Comment
Add comment · Show 7
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 Team_26 · Dec 23, 2013 at 11:25 AM 0
Share

Sorry for bad ocde blockquote.

avatar image KellyThomas · Dec 23, 2013 at 12:23 PM 0
Share

Lerp works best with a t value that varies within the range 0 to 1. Time.deltaTime is a poor fit as it only varies when the framerate fluctuates.

avatar image Team_26 · Dec 23, 2013 at 12:30 PM 0
Share

So how can I write this code using this value?

public float T = 0.5;

player.transform.position = Vector3.Lerp(Tunel_1, Tunel_2, T * smooth);

?

avatar image KellyThomas · Dec 23, 2013 at 12:39 PM 0
Share

Try this, it should demonstrate the concept:

 float t = $$anonymous$$athf.Repeat(Time.time / 10.0f, 1.0f);
 Debug.Log(t);
 player.transform.position = Vector3.Lerp(Tunel_1, Tunel_2, t); 
avatar image Team_26 · Dec 23, 2013 at 12:52 PM 0
Share

There are no compilation fails too, but lerp still doesn't work :/

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Dec 23, 2013 at 11:34 AM

Lerp gives you an interpolated value between A and B.

In your case it was between (X, Y, 0) and (X, Y, 5).

I suspect you expected the object to go from (X, Y, 0) and (X, Y, 5) over time but what happens is that you are sampling one position between these two points and accept that as your new position. I suspect you expected that you could just feed back this value for the next iteration, but since you are discarding Z, so you are always moving between 0 to 5 in Z but you lose track of your progress. Each call to Lerp will cause the animation to start over.

Try this instead:

 using UnityEngine;
 
 public class LerpMove : MonoBehaviour 
 {
     public GameObject player;
     public float smooth = 2;
 
     void Update () 
     {
         if (Input.GetKey (KeyCode.UpArrow)) 
             MovePlayerTowardZ5 ();
     }
 
     void MovePlayerTowardZ5 ()
     {
         var from = player.transform.position;
         var to = from; 
         to.z = 5;
 
         var newPosition = Vector3.Lerp (from, to, Time.deltaTime * smooth);
         player.transform.position = newPosition;
     }
 }
Comment
Add comment · Show 2 · 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 Statement · Dec 23, 2013 at 05:00 PM 0
Share

The expected behaviour of the snippet and best as I could interpret your intention is that when you hold down the up arrow key, player will move toward Z = 5, but since you are using Lerp, you will never actually reach Z = 5.

$$anonymous$$aybe usage of Lerp in this manner is widespread since it seems to give "smooth" movement which often is desirable, but I find it in most cases to be incorrect to use Lerp. Think of using lerp like this:

You have 10 miles to go to your destination. So you walk 50% of them and have 5 miles left to go for the next frame. The next frame you walk 50% of the remaining 5 miles and have 2.5 miles left to go for the next frame. Then 1.25. Then 0.625, 0.3125, 0.15625 and so on. If you keep dividing the distance you have left to go each time, you will never reach there (same goes for using Time.deltaTime). You will eventually get very very close, but you will never get to exactly Z = 5. It will just take longer and longer to cover less distance.

Consider using Vector3.SmoothDamp or Vector3.$$anonymous$$oveTowards ins$$anonymous$$d, which will get rid of that effect, if you agree that it is undesirable to use Lerp.

avatar image Team_26 · Dec 23, 2013 at 05:17 PM 0
Share

Thank you very much!!

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

hi everyone, i have a problem in my pause script, when i press the pause button my zombie is still moving . can anyone help me :( 2 Answers

transform.position.y and x or z + 1? 0 Answers

Problem with transform 2 Answers

Help around an approach to a "Virtual Motion Table" in Unity 0 Answers

Instantiated prefab position 5 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