- Home /
Issue lerping localscale
Go an issue here scaling localy.
At the start of the scene, they are all at 0% scale, and the first use of GrowChain works as expected, objects grow to fullsize, though localscale.z never quite gets to 1, usually getting to about 0.9999999996 or something. As soon as I shrink one, it only scales about 1/2 way down and appears to squash and stretch the model.
private IEnumerator GrowChain(GameObject go){
while(go.transform.localScale.z < 1){
float localZ = Mathf.Lerp(go.transform.localScale.z, 1, fadeTime * Time.deltaTime);
go.transform.localScale = new Vector3(go.transform.localScale.x, go.transform.localScale.y, localZ);
yield return null;
go.transform.FindChild("ShockChainParticles").gameObject.SetActive(true);
}
}
private IEnumerator ShrinkChain(GameObject go){
while(go.transform.localScale.z > 0){
float localZ = Mathf.Lerp(go.transform.localScale.z, 0, fadeTime * Time.deltaTime);
go.transform.localScale = new Vector3(go.transform.localScale.x, go.transform.localScale.y, localZ);
yield return null;
}
}
any idea what I am doing wrong?
You don't show fadeTime
? But, I think the problem is fadeTime*Time.deltaTime
. Delete that, forget you ever had it there, and read about how to use a 0-1 Lerp (I've seen several good explanations here in UA.)
Answer by komodor · May 08, 2015 at 02:00 PM
http://docs.unity3d.com/ScriptReference/Mathf.Lerp.html
the third parameter is float between 0f and 1f
so you should put there fadeTime/totalTime (where fadeTime is current time between 0f and totalTime and totalTime is end time of lerp)
Cool .. I think I got it working, does this make sense?
private IEnumerator GrowChain(GameObject go){
float currentLerpTime = 0;
while(go.transform.localScale.z < 1){
currentLerpTime += Time.time * speed;
float localZ = $$anonymous$$athf.Lerp(go.transform.localScale.z, 1, currentLerpTime);
go.transform.localScale = new Vector3(go.transform.localScale.x, go.transform.localScale.y, localZ);
yield return null;
}
}
private IEnumerator ShrinkChain(GameObject go, bool triggered){
float currentLerpTime = 0;
while(go.transform.localScale.z > 0){
currentLerpTime += Time.time * speed;
float localZ = $$anonymous$$athf.Lerp(go.transform.localScale.z, 0, currentLerpTime);
go.transform.localScale = new Vector3(go.transform.localScale.x, go.transform.localScale.y, localZ);
yield return null;
}
}
I am not sure, the currentLerpTime should be between 0 and 1 :D and Time.time*speed doesn't look like it fits to the condition
Your answer
Follow this Question
Related Questions
Lerp not working inside Coroutine. 1 Answer
How to Lerp localScale? 1 Answer
Scale (Transform.localscale) a gameobject based on ARFaceAnchor anchorData 0 Answers
Having trouble swapping GameObjects on button click 1 Answer
How to ''stack'' coroutines, and call each one till all are executed? 5 Answers