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 /
avatar image
0
Question by MSachs · Apr 06, 2020 at 05:57 AM · axischangefloatvalue

Change value according to Y position of an object?

Hi,


I have an obejct which I can move up and down on the Y-axis and is limited to a min and max value. Let's say you can move it from a random value like 0.173 up to 0.298.


I then would like to have a different value which changes dynamically from e.g. -30 to +30 depending on the objects position on its Y-axis. Ideally this value would change in +-0.5 steps. Right now I can only think of manually "binding" the value on the y-axis to the other value like 0.173 = -30, 0.177 = -29.5, 0.181 = -29,...

But that definitely doesn't seem right at all...


Any help would be much appreciated!


Thanks in advance! :)

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

2 Replies

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

Answer by Namey5 · Apr 06, 2020 at 08:33 AM

You can get there with a bunch of functions. I'll explain what each step does;

 //These are the min/max for your object's y-axis (you would already have these)
 float yMin = 0.2f;
 float yMax = 0.4f;
 
 //These are the min/max for the function output (the range to convert to)
 float fMin = -30f;
 float fMax = 30f;
 
 //The size of each individual step
 float step = 0.5f;
 
 ...
 
 //y-axis position of the object
 float y = transform.position.y;
 //Re-map into a range of [0f,1f]. If you don't want the function to go outside the range, you can clamp this using Mathf.Clamp01()
 float t = (y - yMin) / (yMax - yMin);
 //Using t, re-map into the range of the function
 float range = Mathf.Lerp (fMin, fMax, t);
 
 //This is the final value. We scale by the step amount, round down to the nearest integer, then scale back into the original range. This allows you to step through a range at a specific interval
 float f = Mathf.Floor (range / step) * step;
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 MSachs · Apr 06, 2020 at 10:28 AM 0
Share

Thanks a lot for this in depth answer! I will test it right now and will let you know if it works!

avatar image MSachs · Apr 06, 2020 at 11:49 AM 0
Share

It works absolutely flawless! Thanks a lot!

avatar image
0

Answer by Crystall21 · Apr 06, 2020 at 03:15 PM

Take a look at this https://forum.unity.com/threads/re-map-a-number-from-one-range-to-another.119437/

Comment
Add comment · 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

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

126 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

Related Questions

(C#) Return Float Variable to previous value 1 Answer

Positive to negative not working? 1 Answer

Inverse the mouse position value 2 Answers

Slider doesn't work on Android device 1 Answer

Show Vector3 full float value in Debug 1 Answer


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