简体   繁体   English

如何在运行时在Unity中同时移动和缩放3D对象?

[英]How to simultaneously move and scale 3D object at runtime in Unity?

I need to move and scale 3D object at runtime in Unity. 我需要在运行时在Unity中移动缩放 3D对象。

I can move it like this: 我可以这样移动它:

void Update(){

public Transform target_1;
float relocationSpeed = 10;
float step = relocationSpeed * Time.deltaTime;

MyObject.transform.position = Vector3.MoveTowards (MyObject.transform.position, target_1.position, step);
}

I can scale it up using coroutine like this: 我可以像这样使用协程扩大规模:

float scaleSize = 20;

IEnumerator ScaleUpMyObject(){

        for (var i = 0; i < scaleSize; i++) {

            MyObject.transform.localScale += new Vector3 (1, 1, 1);
            yield return new WaitForSeconds(0.1f);

        }
}

But what would be the best way to move and scale my object simultaneously ? 但是, 同时移动和缩放对象的最佳方法是什么?

Thank you! 谢谢!

Without changing too much of your original code, you could do something like this 无需更改太多原始代码,您可以执行以下操作

public Vector3 wantedSize = new Vector3(5, 5, 5);
void Update()
{
    float relocationSpeed = 10;
    float step = relocationSpeed * Time.deltaTime;

    MyObject.transform.position = Vector3.MoveTowards(MyObject.transform.position, target_1.position, step);
    MyObject.transform.localScale = Vector3.MoveTowards(MyObject.transform.localScale, wantedSize, step);
}

You can use the animator for that, here is an example. 您可以为此使用动画制作器,这是一个示例。

Unity 3D object animation example Unity 3D对象动画示例

Also you can use a library like iTween (free on the asset store) to animate the vectors of position and scale, has transition options too, for you to not only translate linearly (ease in and out in quad, bounce, exponentialy, etc). 您还可以使用iTween之类的库(资产商店免费提供)来设置位置和比例矢量的动画,还具有过渡选项,因为您不仅可以线性平移(以四边形,反弹,指数等方式进出) 。

Unity Asset Store - iTween Unity资产商店-iTween

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM