• 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 /
  • Help Room /
avatar image
Question by Kos-Boss · Jul 18, 2018 at 12:50 PM · arraymathfinterpolationmathf.lerpinterpolate

Mathf.Lerp for integers

Unexpectedly for myself, I'm hung up on this issue. Suppose there is an array with a length of N elements. And we need to take an element from there, but not by index, but by value t = [0..1] (from 0 to 1 both inclusive). That is, the problem is reduced to a linear interpolation of the float value into the index of the array.

int index = (int) Mathf.Lerp(0, N, t);

But when t = 1, then index = N which is out of array's range.

We can fix this as follows:

int index = Mathf.Clamp((int) Mathf.Lerp(0, N, t), 0, N - 1);

or int index = (int) Mathf.Lerp(0, N, t - Mathf.Epsilon);

but i dont like the way it looks.

So the question is how to do this gracefully?

Comment

People who like this

0 Show 4
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 Hellium · Jul 18, 2018 at 01:18 PM 0
Share

I would do as simple as this :

 int index = Mathf.RoundToInt( t * ( N - 1 ) ) ;
avatar image Kos-Boss Hellium · Jul 18, 2018 at 01:37 PM 0
Share

Thanks for the answer, but this is not correct. The range of index 9 is less than that of the rest. For example, if t = 0.93, then index = 8.

avatar image Hellium Kos-Boss · Jul 18, 2018 at 01:50 PM 0
Share

If you have 9 elements in your array, and t = 0.93, then the output will be 7 (7.44 rounded down). Supposing t = 0.95, the output is 8, and you get the last element of your array.

What's wrong with this?

Here is a graphic showing the index according to t :

https://www.desmos.com/calculator/n2bjtxdrop

Show more comments

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Hellium · Jul 18, 2018 at 02:31 PM

After our discussion in the comments, here is the formula that should work:

   // Scaling by ( 1 - Mathf.Epsilon ) prevents N to be chosen when t == 1
  int index = Mathf.FloorToInt( t * N * ( 1 - Mathf.Epsilon ) ) ;

https://www.desmos.com/calculator/5rcgfpnkox

Comment
TttEST_1

People who like this

1 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 Kos-Boss · Jul 18, 2018 at 02:40 PM 0
Share

And now here is exactly the problem that I described initially!

When t = 1, then index = N which is out of array's range. alt text

P. S. You have one redundant closing parentheses in your answer.

avatar image Hellium Kos-Boss · Jul 18, 2018 at 03:08 PM 0
Share

Oh yes, you are right! I guess, you have no choice but scaling by ( 1 - Mathf.Epsilon ) :

 int index = Mathf.FloorToInt( t * N * ( 1 - Mathf.Epsilon ) ) ;
avatar image Kos-Boss Hellium · Jul 18, 2018 at 03:14 PM 0
Share

I hoped there was a way to solve it more elegantly :)

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

Min and Max value? 1 Answer

Rigidbody2D interpolation doesn't work properly 0 Answers

Mathf.Lerp not working. 2 Answers

Mesh looks like overlapping. 0 Answers

Smooth interpolate between n and 0 float by a curve like a log function 0 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