- Home /
How to lerp an object's scale?
I have never used lerp before. How would I simply lerp an object's scale? I have tried this:
function Explosion(){
Explosion.transform.localScale = Vector3.Lerp(finalScale, finalScale, finalScale);//final scale is just a float
}
but it didn't work. Any help would be appreciated.
You are lerping between the same value so it wont change?
Wow, yep that's it. Thanks! As I said I've never used lerp before.
Answer by robertbu · Mar 16, 2014 at 10:40 PM
First Lerp() takes two Vector3 parameters and a fraction parameter. You are passing three floats. Assuming you are doing this to size over time, 'Explosion' would need to get called every frame. I doubt you have it structured that way. You can structure Explosion as a coroutine to make this work. Here is a bit of demo code. Create a new scene, Create a cube, add this code to the cube. While running the up and down arrow keys will make the cube bigger or smaller.
#pragma strict
public var scaleBy : float = 1.5;
public var speed : float = 1.0;
private var scale : Vector3 = Vector3(1,1,1);
private var scaling = false;
private var trans : Transform;
function Start() {
trans = transform;
}
function Update() {
if (!scaling) {
if (Input.GetKeyDown(KeyCode.UpArrow)) {
Explosion(scaleBy, speed);
}
else if (Input.GetKeyDown(KeyCode.DownArrow)) {
Explosion(1.0 / scaleBy, speed);
}
}
}
function Explosion(factor : float, speed : float) {
scaling = true;
scale *= factor;
while (trans.localScale != scale) {
trans.localScale = Vector3.MoveTowards(trans.localScale, scale, Time.deltaTime * speed);
yield;
}
scaling = false;
}
The 'scaling' boolean variable is used to lock out input while the cube is scaling. You may not need it for an explosion use.
Answer by Rushabhshroff98 · Mar 29, 2016 at 03:18 PM
Simply use Explosion.transform.localScale = Mathf.Lerp(finalScale,finalScale,finalScale);
@BZS
Uhmm I am sorry it should be - Explosion.transform.localScale = vector3.one * $$anonymous$$athf.Lerp(finalScale,finalScale,finalScale);