- Home /
Scaling objects over time?
I have a chest that I applied a script to, I'm trying to make objects start small while in the chest and as they're popping out of the chest get to regular scale. This is part of the script that I just added:
GameObject justDropped;
float newScale = Mathf.Lerp(1, 5, Time.time / 5);
if (randomValue <= LootTable[j].dropRarity)
{
Vector3 direction = new Vector3(Random.Range(1, 3), 3, Random.Range(-2, 2));
justDropped = Instantiate(LootTable[j].item, transform.position, Quaternion.identity);
justDropped.GetComponent<Rigidbody>().AddForce(direction * Random.Range(50, 100));
justDropped.transform.localScale += new Vector3(newScale, newScale, newScale);
return; //remove this for more items
I get gameobjects with scales that are 1 , 1 , 1 as giant items and they don't transform over time, and for some items who have scale that is more than 1, 1, 1 just come out normal size.
You are adding the Vector to the scale:
justDropped.transform.localScale += new Vector3(newScale, newScale, newScale);
Are you sure you don't just want to set it like this?
justDropped.transform.localScale = new Vector3(newScale, newScale, newScale);
justDropped.transform.localScale = Vector3.Lerp(justDropped.transform.localScale, new Vector3(5,5,5), Time.deltaTime * speed);
Place it in Update() method
Answer by NiaG-A · Aug 19, 2017 at 08:54 AM
I have a YouTube video about this https://www.youtube.com/watch?v=Wymchsif758 it only requires a few lines of code
Answer by Giovarco · Oct 25, 2017 at 04:08 PM
If you want to scale a gameobject over time, you need to use:
1) Co-routines, for the "over time" part
2) and change the Transform.localScale of the object
Check these links: https://docs.unity3d.com/Manual/Coroutines.html https://docs.unity3d.com/ScriptReference/Transform-localScale.html
Answer by vostok4 · Oct 25, 2017 at 08:07 PM
I recommend tweening libraries for this. I used iTween for a long time but recently discovered and fell in love with DOTween.
These tasks turn into one line of code that don't have to be hidden in an update:
transform.DOScale(5f, 1f);
Will scale your object to (5f, 5f, 5f)
over 1f
seconds.