Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
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
Add comment · 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 = $$anonymous$$athf.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

· Add your reply
  • Sort: 
avatar image
1
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
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 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 - $$anonymous$$athf.Epsilon ) :

 int index = $$anonymous$$athf.FloorToInt( t * N * ( 1 - $$anonymous$$athf.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

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

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

Trying to use coroutines in order to use Mathf.Lerp, Lerp instantly jumps values 0 Answers

Mathf.Lerp not working. 2 Answers

Get a value that interpolates between two floats based on where character is facing 1 Answer

What exactly does Mathf.InverseLerp() do? 4 Answers

Rigidbody2D interpolation doesn't work properly 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