- Home /
How can i scale down a sprite based on distance from the target?
I've been trying to figure out how i can scale a sprite gameobject as it reaches the target, the target does not move (dead center) based on the distance from the target i need the sprite to scale down in the x axis as it reaches the target.
Need to scale it down based on distance because if i do apply speed on the sprite over time, i need the scaling to remain at the same rate.
Any help would be greatly appreciated, thanks!
Answer by Theinsanekiller · Apr 19, 2016 at 07:10 AM
The easiest way to do it is you will have to get the distance between the two game objects and you can achieve it using Vector2.Distance () function and then you will have to set the x value of transform equals to the distance.
I understand how to achieve the distance between the two objects but i'm having a difficulties reducing the scale over time as it approaches the target.
Thanks
As far as i can Understand your object is constantly moving towards a target scale should be the distance. So what you will have to do is you will have to constantly calculate distance and supply the value the value to the sprite's scale in update.
Answer by Mazer83 · Sep 11, 2019 at 01:24 PM
Decide what your max size should be.
Decide what your min size should be.
Decide the distance at which the scaling should start.
The sprite will be at max size (100%) when the sprite is over a certain distance from the target.
The sprite will be at min size (let's say, 20%) when the sprite is under a certain distance. Then you just interpolate linearly between the two.
scaleFactor = maxScale - minScale // (1 - 0.2)
if(dist < minDist) //minDist is the min distance needed to not be scaling
{
scale = (dist/minDist) * scaleFactor;
}
So here if you're 50% of the distance, you scale down by 40%, which is halfway between the max and min scale.