随着时间的推移缩放GameObject

时间:2017-10-05 13:40:32

标签: c# unity3d

我制作了一个统一的测试游戏,因此当我点击一个按钮时,它会产生一个从工厂类创建的圆柱体。我正在尝试这样做,当我创建圆柱体时,它的高度在接下来的20秒内缩小。我发现的一些方法很难转化为我正在做的事情。如果你能引导我走向正确的方向,我将非常感激。

这是我的圆柱类代码

User.find({name: 'Vignesh'}).populate("posts").exec(function(err, post){
    if(!err){
        console.log("..........populated posts of User........");
        console.log(post);
    } else {
        console.log(err);
    }

});

3 个答案:

答案 0 :(得分:3)

可以使用协程函数中的Time.deltaTimeVector3.Lerp来完成此操作。与Rotate GameObject over timeMove GameObject over time问题类似。修改它有点做这件事。

bool isScaling = false;

IEnumerator scaleOverTime(Transform objectToScale, Vector3 toScale, float duration)
{
    //Make sure there is only one instance of this function running
    if (isScaling)
    {
        yield break; ///exit if this is still running
    }
    isScaling = true;

    float counter = 0;

    //Get the current scale of the object to be moved
    Vector3 startScaleSize = objectToScale.localScale;

    while (counter < duration)
    {
        counter += Time.deltaTime;
        objectToScale.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
        yield return null;
    }

    isScaling = false;
}

<强> USAGE

将在20秒内缩放GameObject:

StartCoroutine(scaleOverTime(cylinder.transform, new Vector3(0, 0, 90), 20f));

答案 1 :(得分:0)

结帐Lerp。如何使用它的一般例子是这样的:

float t = 0;
Update()
{
    t += Time.deltaTime;
    cylinder.localScale = new Vector3(1, Mathf.Lerp(2f, 1f, t/3f), 1); // shrink from 2 to 1 over 3 seconds;
}

答案 2 :(得分:0)

您将创建一个新的monobehaviour脚本并将其添加到您的基元。然后你将使用monobehaviour(或使用协程)的“更新”方法随时间变化对象。

Monobehaviour必须如下所示:

public class ShrinkBehaviour : MonoBehaviour
{
    bool isNeedToShrink;
    Config currentConfig;

    float startTime;
    float totalDistance;

    public void StartShrink(Config config)
    {
        startTime = Time.time;
        currentConfig = config;
        totalDistance = Vector3.Distance(currentConfig.startSize, currentConfig.destinationSize);
        isNeedToShrink = true;
        transform.localScale = config.startSize;
    }

    private void Update()
    {
        if (isNeedToShrink)
        {
            var nextSize = GetNextSize(currentConfig);

            if (Vector3.Distance(nextSize, currentConfig.destinationSize) <= 0.05f)
            {
                isNeedToShrink = false;
                return;
            }

            transform.localScale = nextSize;
        }
    }

    Vector3 GetNextSize(Config config)
    {
        float timeCovered = (Time.time - startTime) / config.duration;
        var result = Vector3.Lerp(config.startSize, config.destinationSize, timeCovered);
        return result;
    }

    public struct Config
    {
        public float duration;
        public Vector3 startSize;
        public Vector3 destinationSize;
    }
}

要使用此功能,您必须执行下一步:

public Cylinder()
{
    GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
    var shrink = cylinder.AddComponent<ShrinkBehaviour>();
    shrink.StartShrink(new ShrinkBehaviour.Config() { startSize = Vector3.one * 10, destinationSize = Vector3.one * 1, duration = 20f });
    cylinder.transform.position = new Vector3(3, 0, 0);

    cylinder.GetComponent<MeshRenderer>().material.color = Random.ColorHSV();
    Destroy(cylinder, 30.0f);
}

你必须记住,monobehaviour-script必须在单独的文件中,并且必须具有类似于monobehaviour-class名称的名称。例如,ShrinkBehaviour.cs;