Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Supershandy · Oct 08, 2013 at 06:14 PM · gameobjectscaledistanceclamp

How to scale an object between two values over distance

Question is a little long winded but bear with me :)

What I would like to do is to be able to scale an object up or down depending on the distance in a uniform way (x,y,z), I've looked through the answers and although there plenty on the subject, there are none that I could find that seemed to answer my question.

In psuedocode

 var 1 : starting scale
 var 2 : ending scale
 Camera distance : float
 
 if camera distance going towards object
 
 scale increases, clamp at var 1
 
 if camera distance going away from target
 
 scale decreases, object renderer disabled if value is less than var 2

I don't want the object to be at full scale when the camera reaches 0,0,0 or wherever the object is, more like being full size before getting anywhere near it, so I'm guessing i'm also looking for a way for the scale to reach full size by a certain distance.

If that's a little all over the place, i'll provide a diagram.

I should also mention that I can work out how to work out the distance between two objects and how to scale in script, I just can't work out how to set the clamps for the scales and how to set when the distance should end the scaling up, the last part would come through trial and error using public variables

Thanks :)

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
8
Best Answer

Answer by Ejlersen · Oct 08, 2013 at 07:00 PM

If I understand you. You want some code to help you scale between min and max distance?

Example (Code not tested):

 var minimumDistance = 1.0;
 var maximumDistance = 10.0;
 
 var mínimumDistanceScale = 1.0;
 var maximumDistanceScale = 0.1;
 
 var distance = (transform.position - Camera.main.transform.position).magnitude;
 var norm = (distance - minimumDistance) / (maximumDistance - minimumDistance);
 norm = Mathf.Clamp01(norm);
 
 var minScale = Vector3.one * maximumDistanceScale;
 var maxScale = Vector3.one * minimumDistanceScale;
 
 transform.localScale = Vector3.Lerp(maxScale, minScale, norm);
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 Supershandy · Oct 08, 2013 at 07:10 PM 0
Share

$$anonymous$$distance and maxdistance I understand, the scale though confuses me a little as the $$anonymous$$imum is larger than the maximum, is it set that way so that the further out you are the smaller the object is and vica versa?

avatar image Supershandy · Oct 08, 2013 at 07:24 PM 0
Share

Oh wait, hang on...I get it now :p

avatar image Supershandy · Oct 08, 2013 at 07:40 PM 0
Share

Works fine, just set the code up like this

 var $$anonymous$$imumDistance : float;
 var maximumDistance : float;
  
 var $$anonymous$$imumDistanceScale : float;
 var maximumDistanceScale : float;
 
 function Update () 
 
 {
     var distance = (transform.position - Camera.main.transform.position).magnitude;
     var norm = (distance - $$anonymous$$imumDistance) / (maximumDistance - $$anonymous$$imumDistance);
     norm = $$anonymous$$athf.Clamp01(norm);
  
     var $$anonymous$$Scale = Vector3.one * maximumDistanceScale;
     var maxScale = Vector3.one * $$anonymous$$imumDistanceScale;
  
     transform.localScale = Vector3.Lerp(maxScale, $$anonymous$$Scale, norm);
 }
avatar image Ejlersen · Oct 09, 2013 at 06:47 PM 0
Share

Glad that it worked for you :)

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

16 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

Related Questions

Calculate Distance Between two Game objects 1 Answer

A node in a childnode? 1 Answer

GameObject's scale gets 0 after being parented in editor 1 Answer

Scale using accelerometer with limits 0 Answers

Changing the Center of a GameObject? 2 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