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
1
Question by permass · Nov 15, 2016 at 07:39 PM · scripting problemvector3slidermathf

InverseLerp for vector3

Hi

I am currently working on a 3D slider that will be placed inside the game world itself. I am wondering if there were a method similar to

 public static float InverseLerp(float a, float b, float value);

but for vectors specifically for Vector3. I have sort of a work around. The sliders that i currently have placed are all working parallel the world unity axis, Vector.up, right and forward. The solution that i have right now for world axis sliders output the slider value like so:

         public float SliderOutput{
             get{
                 switch (RotationAxisChoice) {
                     case Axis.X:
                         return _sliderOutput = Mathf.InverseLerp(_lowerLimit.x, _opperLimit.x, _currentSlidePos.x);
                     case Axis.Y:
                         return _sliderOutput = Mathf.InverseLerp(_lowerLimit.y, _opperLimit.y, _currentSlidePos.y);
                     case Axis.Z:
                         return _sliderOutput = Mathf.InverseLerp(_lowerLimit.z, _opperLimit.z, _currentSlidePos.z);
                 }
                 return 0f;
             }
         }


this should work for the special case that the slider is perfectly aligned with the world axis, but i would like to do a generic solution that can have the slider orientated in any direction. And also get rid of the switch statementalt text

slider.png (277.8 kB)
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
7
Best Answer

Answer by Baertidaer · Nov 22, 2016 at 04:08 AM

I had the same problem and stumbled upon your question.

This works as a generic solution:

     public static float InverseLerp(Vector3 a, Vector3 b, Vector3 value)
     {
         Vector3 AB = b - a;
         Vector3 AV = value - a;
         return Vector3.Dot(AV, AB) / Vector3.Dot(AB, AB);
     }

You can of course Mathf.Clamp01() the output if you want to.

This works in other dimensions too, just change Vector3 to Vector2.

Comment
Add comment · Show 5 · 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 permass · Dec 01, 2016 at 10:41 AM 0
Share

that's a really nice solution thanks!

avatar image SalvoSoftware · Dec 05, 2016 at 10:15 AM 0
Share

Are you sure that Vector3.Dot(AB, AB) doesn't return zero? I really need to try this out!

avatar image permass SalvoSoftware · Dec 05, 2016 at 10:23 AM 1
Share

If it does return 0 its because the length of AB is 0, the dot product of a vector with it self is equal to the magnitude squared: dot(a,a) = a.x*a.x + a.y *a.y +a.z*a.z

avatar image SalvoSoftware permass · Dec 05, 2016 at 10:42 AM 1
Share

While I was waiting for your answer I've taked a look at the scripting API and I made a few calculations, and you're right. Good job.

So if A is equal to (0, 0, 0), B is equal to (2, 2, 0) and V is equal to (0, 2, 0), the function will return 0.5, so it does simply perfect.

I guess if V is "under" A the value will be less than 0, while if V is "above" B the value will be greater than 1.

avatar image Harinezumi · Sep 01, 2017 at 08:30 AM 0
Share

Thanks @Baertidaer, that's a great solution!

I'm wondering what happens if value is not in the same plane (line in 2D) a and b define, does it still return a value that makes sense?

EDIT1: never $$anonymous$$d, I just did some tests on paper and it makes sense. It basically takes the perpendicular projection of value onto the AB plane (line in 2D). You could also say it ignores the coordinate which does not fit into the AB system.

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

102 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

Related Questions

Vector3 does not contain a constructor that takes 4 arguments 0 Answers

Get Vector3 From Another Script 1 Answer

Linq not working 1 Answer

How to calculate forward/Up/Right without transform, have only vector3? 0 Answers

Swapping characters breaks controls 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