- Home /
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 :)
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);
$$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?
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);
}
Your answer
Follow this Question
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