• 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 shefPhysics · Apr 16, 2019 at 02:10 AM · fpstimelerpienumeratorwhile-loop

Lerp returning max value halfway

Hello, well my problem seems to be quite common and yet I haven't been able to fix it... degrees and robotjoints are float values, in an array, but are simple floats.

Lerp is misbehaving, I think... the code is the next:

 private IEnumerator MoveJoints(float[] degrees, float speed) {
     float t = 0.0f;
     while (t < 1) { 
         t += Time.deltaTime * speed;
         for (int i = 0; i < robotJoints.Length; i++) {
             robotJoints[i] = Mathf.Lerp(robotJoints[i], degrees[i], t);
         }
         Debug.Log("In " + t.ToString() + " the value is " + robotJoints[0].ToString() + " aiming for " + degrees[0].ToString());
         yield return null;
     }
     done = true;
 }

I can see with the debug that the value of robotjoints[0] becomes the same as degrees[0] when t is 0.5 or higher... shouldn't it be at 1?

What am I not seeing?

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
2
Best Answer

Answer by highpockets · Apr 16, 2019 at 10:48 AM

This is likely because you are passing the lerp function the first param as the updated float value on every iteration. You need a constant float value as the start float to do a linear interpolation. Since the interpolation is not linear with your code, you are getting the results as is. Try passing the start float that stays constant until your reach 1.0f and this will likely not happen.

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 Bunny83 · Apr 16, 2019 at 11:05 AM 0
Share

Converted to an answer. Note that one may want to use $$anonymous$$athf.$$anonymous$$oveTowardsAngle or $$anonymous$$athf.$$anonymous$$oveTowards in such cases. Though if those are joint angles of a robot and they should arrive all at the target position at the same time Lerp would make more sense.

avatar image shefPhysics · Apr 17, 2019 at 01:21 AM 0
Share

I cant believe I didn't see this! Indeed, this was the problem, thank you for your help!

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

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

fps calculation with while loop in coroutine 2 Answers

Updating at frameRate some fixedUpdated gameObject 1 Answer

slow a lerp ? array/static problem 1 Answer

Jump with mathf.lerp problem 2 Answers

Color lerp once? 2 Answers

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