# How can i use StartCoroutine to set the speed for each scaling step of the walls?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WallsTest : MonoBehaviour
{
// using a GameObject rather than a transform
public GameObject prefab;
public Vector3 wallsStartPosition;
public float width = 0;
public float height = 1;
public float length = 2;
public Camera wallsCamera;

void Start()
{
wallsCamera.transform.position = new Vector3(wallsStartPosition.x, wallsStartPosition.y + 100, wallsStartPosition.z - 235);

StartCoroutine(BuildWalls());
}

IEnumerator BuildWalls()
{
for (int i = -2; i < 2; i++)
{
GameObject go = Instantiate(prefab);
go.transform.parent = transform;
Vector3 scale = Vector3.one;

float sign = Mathf.Sign(i);
if ((i * sign) % 2 == 0)
{
adjustedPosition.x += (length * sign) / 2;
yield return new WaitForSeconds(0.1f);
scale.x = width;
scale.y = height;
scale.z *= length + width;
}
else
{
adjustedPosition.z += (length * sign) / 2;
scale.x *= length + width;
scale.y = height;
scale.z = width;
}

go.transform.localScale = scale;
}
}
}


What it does now it's creating two walls at once it's just waiting 0.1 millisecond before creating them.

But instead i want it to show the scaling of the walls how they scaling and moving same for the else part for the adjustedPosition.z and not only the adjustedPosition.x

The problem is it's just waiting until it's placing the walls at once. I want to see the walls being building step by step like it's placing cube after cube.

• Could you be more specific? Some images or gif would be extremely helpful. From what I understood you are trying to make animation from code during which the walls are built block by block, right? I can provide you a solution, but I have to be sure about what you are asking. – Candid Moon _Max_ Oct 14 '17 at 23:35
• you have to work with deltaTime in order to animate something from Coroutines. Use it in combination with vector.lerp() – derHugo Oct 16 '17 at 17:00
• yield return new WaitForSeconds(0.1f); -> That's 0.1 seconds not 0.1 miliseconds – Draco18s no longer trusts SE May 6 '18 at 6:06

I can't completely code it for you now but I give you an general example here which should be understandable hopefully to give you an idea how this works: (sorry for variable names will fix it later)

public IEnumerator Example()
{
//Define start and goal transforms
Vector3 currentPosition = Vehicle.transform.localPosition;
Quaternion currentRotation = Vehicle.transform.localRotation;
Vector3 currentScale = Vehicle.transform.localScale;

Vector3 goalPostionTarget = _goalTransformVehicle.Position;
Quaternion goalRotationTarget = _goalTransformVehicle.Rotation;
Vector3 goalScaleTarget = _goalTransformVehicle.Scale;

float timePassed = 0.0f;

//With the TargetAnimationDuration define length of animation in sec
float TargetAnimationDuration=1.5f;
float fracTime = timePassed / TargetAnimationDuaration;

while (fracTime < 1)
{
//Use Lerp to interpolate transforms over time
Vehicle.transform.localPosition = Vector3.Lerp(currentPosition, goalPostionTarget, fracTime);
Vehicle.transform.localRotation = Quaternion.Lerp(currentRotation, goalRotationTarget, fracTime);
Vehicle.transform.localScale = Vector3.Lerp(currentScale, goalScaleTarget, fracTime);

//Update fracTime
timePassed += Time.deltaTime;
fracTime = timePassed / TargetAnimationDuration;

//Return so frame is rendered before going on
yield return null;
}

//Finally set goal Transforms just to be sure
Vehicle.transform.localPosition = goalPostionTarget;
Vehicle.transform.localRotation = goalRotationTarget;
Vehicle.transform.localScale = goalScaleTarget;
}