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


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

            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;
                adjustedPosition.z += (length * sign) / 2;
                scale.x *= length + width;
                scale.y = height;
                scale.z = width;

            adjustedPosition.y += height / 2;
            go.transform.localScale = scale;
            go.transform.localPosition = adjustedPosition;

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.

  • \$\begingroup\$ 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. \$\endgroup\$ – Candid Moon _Max_ Oct 14 '17 at 23:35
  • \$\begingroup\$ you have to work with deltaTime in order to animate something from Coroutines. Use it in combination with vector.lerp() \$\endgroup\$ – derHugo Oct 16 '17 at 17:00
  • \$\begingroup\$ yield return new WaitForSeconds(0.1f); -> That's 0.1 seconds not 0.1 miliseconds \$\endgroup\$ – 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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.