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 fredii3 · Jan 04, 2019 at 09:51 AM · slidervector3.distance

Vector3 distance to fill slider.

I want to fill a slider that indicates when a distance has been traveled.

I have:

 float dist = Vector3.Distance(other.position.z, transform.position.z);

dist goes between 6 and 280.

I am confused on how to do it, because the slider goes from 0 to 1. I tried Mathf.Clamp01 and it doesn't work.

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 fafase · Jan 04, 2019 at 10:19 AM

You can set min/max value in Inspector:

alt text

The other way is to normalize the distance. It goes from 6 to 280, so make it start from 0 to 274.

 float start = 6f;
 float total = 274f;
 float distance = Vector3.Distance(other.position.z, transform.position.z) - start;
 value = distance / total;


ui-sliderinspector.png (34.2 kB)
Comment
Add comment · Show 1 · 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 fredii3 · Jan 04, 2019 at 10:48 AM 0
Share

I'm measuring the distance between the player and the end of the level. The math you provided helped me. Here is my code for future people if the struggle with this:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
     public Transform player;
     public Transform levelCleared;
 
     public Slider slider;
 
     float dist;
 
     private void Start()
     {
     }
 
     private void Update()
     {
         dist = player.position.z - (levelCleared.position.z - player.position.z) * -0.1f;
         float value = dist / levelCleared.position.z;
         slider.value = value;
         Debug.Log(value);
     }
 }

avatar image
0

Answer by Hellium · Jan 04, 2019 at 10:16 AM

 float dist = Vector3.Distance(other.position.z, transform.position.z);
 float minDist = 6 ;
 float maxDist = 280;
 slider.normalizedValue = ( dist - minDist ) / ( 280 - minDist ) * (slider.maxValue - slider.minValue ) + slider.minValue;


Which can be simplified if you are SURE the value of the slider is between 0 and 1

 float dist = Vector3.Distance(other.position.z, transform.position.z);
 float minDist = 6 ;
 float maxDist = 280;
 slider.value = ( dist - minDist ) / ( 280 - minDist );
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

99 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

Related Questions

How to apply the material to my object only to a certain percent of the object? 0 Answers

How to put a image under a powerbar's handle thats moving? 0 Answers

How to save slider values? 3 Answers

In terms of performance, is changing the position of a whole canvas better than changing the position of a UI element? 1 Answer

Slider to modify a countdown time in another scene 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