How to make a cube shrink over time
I am very new to the game dev industry. And i dont know sh*t about coding. I want to make a (20, 1, 20,) Cube shrink over time. I have googled after some tutorials. But i couldn't find ANYTHING.... So i was hoping you could help me out.
Answer by shadowpuppet · Dec 11, 2017 at 10:20 PM
This works on a simple cube I made. It decreases the size evenly ( x,y,z) .Change the growRate to a positive number to increase size. The lrger the digit ( whether positive or negative ) the faster it grows or shrinks
using UnityEngine;
using System.Collections;
public class shrink : MonoBehaviour {
public GameObject cube;
public float growRate = -3f;
void Start () {
}
// Update is called once per frame
void Update () {
cube.transform.localScale += new Vector3(0.1F, .1f, .1f) * growRate * Time.deltaTime;
}
}
Answer by Legend_Bacon · Dec 12, 2017 at 09:25 AM
Hello there,
This should help you out:
using System.Collections;
using UnityEngine;
public class Test : MonoBehaviour
{
private void Start()
{
//Call the function giving it a target scale (Vector3) and a duration (float).
ScaleToTarget(new Vector3(10.0f, 2.5f, 7.5f), 2.5f);
}
public void ScaleToTarget(Vector3 targetScale, float duration)
{
StartCoroutine(ScaleToTargetCoroutine(targetScale, duration));
}
private IEnumerator ScaleToTargetCoroutine(Vector3 targetScale, float duration)
{
Vector3 startScale = transform.localScale;
float timer = 0.0f;
while(timer < duration)
{
timer += Time.deltaTime;
float t = timer / duration;
//smoother step algorithm
t = t * t * t * (t * (6f * t - 15f) + 10f);
transform.localScale = Vector3.Lerp(startScale, targetScale, t);
yield return null;
}
yield return null;
}
}
Put that script on your cube, and it will shrink to target scale with a nice smoothing effect on start. It's up to you then to call ScaleToTarget()
from other scripts whenever you require.
More about smoothing formulas HERE
Hope that helps!
~LegendBacon
the actual name of the file must match the class. looks like you named the file of the script "size" but pasted in the code above which starts out " public class Test : $$anonymous$$onoBehaviour". either rename the script to Test or change the code to " public class size : $$anonymous$$onoBehaviour"
Hello there
As ShadowPuppet said, in this case you need your class name to match the file's (script's) name. Either rename the file to "Test" (not recommended), or rename the class from "Test" to something else. In your case, I believe it would be size? I would recommend rena$$anonymous$$g it to something more intuitive, like "Scaler" or "Resizer".
Best of luck to you!
~LegendBacon
Okay now it works. But it shrinks to fast, i would like it to be about 30 seconds to shrink. And then shrink in to (3, 1, 3,)
I am sorry if i seem to be ungrateful of your help and just seem like your were nothing, that is not what i mean. I my intension was to ask if you could help me something more, not to say that your job was not done well enough :).
EDIT: "It shrinks too fast."