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
2
Question by Uriel_96 · Apr 08, 2011 at 02:43 AM · javascriptmovementlerpfluid

Make Lerp or other more fluid or continuous

OK, so I am making an algorithm that goes to one point to another point and I am already done but, when the enemy is walking to the final point it moves to slow, it means that as some knows some algorithm use the grid so is separated in squares(or triangles) so this makes that it doesn't goes fluid or continuous. So my question here is: how can I make a movement more fluid or continuous having the same speed. This is part of what I am doing so you can see it how I do it:

enemy.position = Vector3.Lerp(enemy.position,nodes[newdistance],Time.deltaTime * speed);
Comment
Add comment · Show 1
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 paulaceccon · Oct 04, 2012 at 07:37 PM 0
Share

Hi, Uriel. Can I ask you a question?? I am new in Unity and I guess I'm trying to do something like I did. I post this question now http://answers.unity3d.com/questions/12859/move-an-object-through-a-set-of-positions.html, and I really think it's similar that what you've done. I would like to now if you can help me. Thanks!

3 Replies

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

Answer by Bunny83 · Apr 08, 2011 at 03:22 AM

Your problem is that everyone is using lerp in that strange way. Lerp is meant to lineary go from start to end, but for that behaviour you need a fix start and end point and the last parameter specify how far the interpolation is at the moment. This parameter t have to be between 0 and 1 (0==start 1==end). Everyone uses as start the sctual position and a more or less constant value for t. If t is 0.1 that means that the calculated position is exact 10% toward the target position from the actual position. When the start position comes closer to the end the distance will get smaller. We move always 10% each frame but 100% is the current distance. So with this method you will never reach the target, you come very close and the speed is almost zero but theoretically you can't reach the end (due to float inaccuracy you may reach it)

In short: what you want is a constant movement, so a distance independent movement speed. Unity have this very nice function called MoveTowards. This function is supposed to take the current position and moves it towards the target position.

enemy.position = Vector3.MoveTowards(enemy.position,nodes[newdistance],Time.deltaTime * speed);
Comment
Add comment · Show 4 · 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 Uriel_96 · Apr 08, 2011 at 10:24 PM 0
Share

thanks man, useful, fast and easy, just what I was looking for :)

avatar image Nerdmigo · Oct 04, 2012 at 02:16 PM 0
Share

thank you Bunny83, i just got started with unity about a month ago, and this fixed the not so smooth movement of my objects too !

avatar image Kiloblargh · Nov 22, 2013 at 09:08 PM 0
Share

It's really weird that almost everyone who has not read the docs on Lerp has come up with the same wrong idea of what it's for and what arguments to give it.

avatar image Bunny83 · Nov 23, 2013 at 04:20 AM 0
Share

@$$anonymous$$iloblargh: Well, as far as i remember the docs had for a long time one of the "wrong" or "bad" examples. Actually most examples in the docs are bad.

It's not really wrong. There's no problem using lerp that way, but you should understand what it does. If you do you should be able to figure out what results you would get and if that's really what you want ;)

avatar image
2

Answer by Eric5h5 · Apr 08, 2011 at 03:20 AM

Basically already answered here.

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 cregox · Nov 22, 2013 at 09:02 PM 0
Share

updated link: http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html

avatar image
0

Answer by Dragonlance · Oct 04, 2012 at 08:14 PM

For a nice Lerp you need something like:

 transform.localPosition = 
     Vector3.Lerp(
          startPos,
          targetPos,
          (Time.time - startTime) / duration);

Do not put in the current position each frame or it will not interpolate linear! Save the start position and calculate with that until the end.

And you will have to cancel the Lerp as soon as ((Time.time - startTime) / duration) is >= 1.0

As Bunny said (Time.deltaTime * speed) is the right thing for MoveTowards not for Lerp.

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 paulaceccon · Oct 04, 2012 at 08:23 PM 0
Share

Thanks, Dragonlance. The duration would be something like this: float duration = Time.time - lerpStartTime; ?? Sorry for the noob question.

avatar image Dragonlance · Oct 04, 2012 at 08:46 PM 0
Share

Hi Paul,

no it is somethin you define.

Lets say I want to move the object from (1,5,7) to (2,16,20) in 5 seconds, then it would be:

Vector3 startPos = new Vector3(1.0f,5.0f,7.0f); Vector3 targetPos= new Vector3(2.0f,16.0f,20.0f); float duration = 5.0f;

Well you can also calculate the duration from the speed: float speed = 1.8f; float duration = Vector3.distance(startPos, targetPos) / speed;

Of course this has to be packed in context. $$anonymous$$aybe you want to start the animation from the local position and you just know the target position: Vector3 startPos = transform.localPosition; Vector3 targetPos = new Vector3(2.0f,16.0f,20.0f); float duration = Vector3.distance(startPos, targetPos) / speed;

The most simple way to actually use it is something like:

 public float speed = 2.0f;
 private Vector3 startPos;
 private Vector3 targetPos;
 private float duration;
 bool animationRunning = false;

 void Update()
 {
     if(animationRunning) {
         float animationProgress = (Time.time - startTime) / duration;
         transform.localPosition = 
             Vector3.Lerp(
                 startPos,
                 targetPos,
                 animationProgress );
         }
         if(animationProgress >= 1.0f) {
              transform.localPosition = targetPos;
              animationRunning = false;
         }
      }
 }

 public void StartAnimation(Vector3 target)
 {
     Vector3 startPos = transform.localPosition;
     Vector3 targetPos = target;
     float duration = Vector3.distance(startPos, targetPos) / speed;
     animationRunning = false
 }


Personally I prefer a Coroutine in most cases ins$$anonymous$$d of update.

(This example is c# and has to be in a $$anonymous$$onobehaviour Class, I just hate the code identation here and did not want to format it all sry.)

Good Luck!!

avatar image paulaceccon · Oct 04, 2012 at 08:50 PM 0
Share

Ohh, I see! I was trying something completely different! Thanks a looooot! Really! :DD

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

transform.position vector3.lerp appears to stay still for a while before the 5DP matches the new location 1 Answer

How to use Lerp? 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