我如何使用StartCoroutine来设置墙壁的每个缩放步骤的速度?

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; 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; } else { 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; } } } 

它现在所做的就是创建两面墙壁,等待0.1毫秒后再创建它们。

但是,我希望它显示墙的缩放比例,如何调整和移动相同的调整的Positioning.z的其他部分,而不仅仅是adjustedPosition.x

问题在于它只是等待,直到它放置在墙壁一次。 我希望看到墙面正在逐步建立,就像立方体放置立方体一样。

我现在还不能完全为你编写代码,但是我给你一个通用的例子,希望能给你一个想法:(对不起,variables名会在以后修复)

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