简体   繁体   中英

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

I need to move and scale 3D object at runtime in Unity.

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

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).

Unity Asset Store - iTween

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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