-1
\$\begingroup\$
    void LeftPartFalls()
{
    if (LeftPartMat)
    {
        for (int i = 0; i < Mathf.Min(5, FirstLine.Length); i++)        // COLOR CHANGE
        {
            Debug.Log(i);
            FirstLine[i].gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
            SecondLine[i].gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
            ThirdLine[i].gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
            FourthLine[i].gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
            FiftLine[i].gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
            SixthLine[i].gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
            SeventhLine[i].gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
            EighthLine[i].gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
            NinthLine[i].gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
            TenthLine[i].gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
        }
    }
    if (!LeftPartMat)
    {
        for (int i = 0; i < Mathf.Min(5, FirstLine.Length); i++)        // COLOR CHANGE
        {
            FirstLine[i].gameObject.GetComponent<MeshRenderer>().material.color = new Color(162f / 255f, 172f / 255f, 186f / 255f, 1f);
            SecondLine[i].gameObject.GetComponent<MeshRenderer>().material.color = new Color(162f / 255f, 172f / 255f, 186f / 255f, 1f);
            ThirdLine[i].gameObject.GetComponent<MeshRenderer>().material.color = new Color(162f / 255f, 172f / 255f, 186f / 255f, 1f);
            FourthLine[i].gameObject.GetComponent<MeshRenderer>().material.color = new Color(162f / 255f, 172f / 255f, 186f / 255f, 1f);
            FiftLine[i].gameObject.GetComponent<MeshRenderer>().material.color = new Color(162f / 255f, 172f / 255f, 186f / 255f, 1f);
            SixthLine[i].gameObject.GetComponent<MeshRenderer>().material.color = new Color(162f / 255f, 172f / 255f, 186f / 255f, 1f);
            SeventhLine[i].gameObject.GetComponent<MeshRenderer>().material.color = new Color(162f / 255f, 172f / 255f, 186f / 255f, 1f);
            EighthLine[i].gameObject.GetComponent<MeshRenderer>().material.color = new Color(162f / 255f, 172f / 255f, 186f / 255f, 1f);
            NinthLine[i].gameObject.GetComponent<MeshRenderer>().material.color = new Color(162f / 255f, 172f / 255f, 186f / 255f, 1f);
            TenthLine[i].gameObject.GetComponent<MeshRenderer>().material.color = new Color(162f / 255f, 172f / 255f, 186f / 255f, 1f);
        }
    }

    if (LeftPartFall)       // FALL DOWN
    {
        for (int i = 0; i < Mathf.Min(5, FirstLine.Length); i++)
        {
            FirstLine[i].gameObject.transform.position = Vector3.Lerp(FirstLine[i].transform.position, new Vector3(FirstLine[i].transform.position.x, -4f, FirstLine[i].transform.position.z), t);
            SecondLine[i].gameObject.transform.position = Vector3.Lerp(SecondLine[i].transform.position, new Vector3(SecondLine[i].transform.position.x, -4f, SecondLine[i].transform.position.z), t);
            ThirdLine[i].gameObject.transform.position = Vector3.Lerp(ThirdLine[i].transform.position, new Vector3(ThirdLine[i].transform.position.x, -4f, ThirdLine[i].transform.position.z), t);
            FourthLine[i].gameObject.transform.position = Vector3.Lerp(FourthLine[i].transform.position, new Vector3(FourthLine[i].transform.position.x, -4f, FourthLine[i].transform.position.z), t);
            FiftLine[i].gameObject.transform.position = Vector3.Lerp(FiftLine[i].transform.position, new Vector3(FiftLine[i].transform.position.x, -4f, FiftLine[i].transform.position.z), t);
            SixthLine[i].gameObject.transform.position = Vector3.Lerp(SixthLine[i].transform.position, new Vector3(SixthLine[i].transform.position.x, -4f, SixthLine[i].transform.position.z), t);
            SeventhLine[i].gameObject.transform.position = Vector3.Lerp(SeventhLine[i].transform.position, new Vector3(SeventhLine[i].transform.position.x, -4f, SeventhLine[i].transform.position.z), t);
            EighthLine[i].gameObject.transform.position = Vector3.Lerp(EighthLine[i].transform.position, new Vector3(EighthLine[i].transform.position.x, -4f, EighthLine[i].transform.position.z), t);
            NinthLine[i].gameObject.transform.position = Vector3.Lerp(NinthLine[i].transform.position, new Vector3(NinthLine[i].transform.position.x, -4f, NinthLine[i].transform.position.z), t);
            TenthLine[i].gameObject.transform.position = Vector3.Lerp(TenthLine[i].transform.position, new Vector3(TenthLine[i].transform.position.x, -4f, TenthLine[i].transform.position.z), t);
        }
    }
    if (!LeftPartFall)      // RISE UP
    {
        for (int i = 0; i < Mathf.Min(5, FirstLine.Length); i++)
        {
            FirstLine[i].gameObject.transform.position = Vector3.Lerp(FirstLine[i].transform.position, new Vector3(FirstLine[i].transform.position.x, 0f, FirstLine[i].transform.position.z), t);
            SecondLine[i].gameObject.transform.position = Vector3.Lerp(SecondLine[i].transform.position, new Vector3(SecondLine[i].transform.position.x, 0f, SecondLine[i].transform.position.z), t);
            ThirdLine[i].gameObject.transform.position = Vector3.Lerp(ThirdLine[i].transform.position, new Vector3(ThirdLine[i].transform.position.x, 0f, ThirdLine[i].transform.position.z), t);
            FourthLine[i].gameObject.transform.position = Vector3.Lerp(FourthLine[i].transform.position, new Vector3(FourthLine[i].transform.position.x, 0f, FourthLine[i].transform.position.z), t);
            FiftLine[i].gameObject.transform.position = Vector3.Lerp(FiftLine[i].transform.position, new Vector3(FiftLine[i].transform.position.x, 0f, FiftLine[i].transform.position.z), t);
            SixthLine[i].gameObject.transform.position = Vector3.Lerp(SixthLine[i].transform.position, new Vector3(SixthLine[i].transform.position.x, 0f, SixthLine[i].transform.position.z), t);
            SeventhLine[i].gameObject.transform.position = Vector3.Lerp(SeventhLine[i].transform.position, new Vector3(SeventhLine[i].transform.position.x, 0f, SeventhLine[i].transform.position.z), t);
            EighthLine[i].gameObject.transform.position = Vector3.Lerp(EighthLine[i].transform.position, new Vector3(EighthLine[i].transform.position.x, 0f, EighthLine[i].transform.position.z), t);
            NinthLine[i].gameObject.transform.position = Vector3.Lerp(NinthLine[i].transform.position, new Vector3(NinthLine[i].transform.position.x, 0f, NinthLine[i].transform.position.z), t);
            TenthLine[i].gameObject.transform.position = Vector3.Lerp(TenthLine[i].transform.position, new Vector3(TenthLine[i].transform.position.x, 0f, TenthLine[i].transform.position.z), t);
        }
    }
}
private IEnumerator enumerator(float waitTime)
{
    while (true)
    {
        LeftPartMat = true;

        yield return new WaitForSeconds(waitTime);

        LeftPartFall = true;

        t = 0f;
        t += Time.deltaTime / 0.7f;

        yield return new WaitForSeconds(4);
        LeftPartFall = false;
        LeftPartMat = false;           
        yield return new WaitForSeconds(waitTime);
    }
}

WaitTime is 4f

  1. Color doesn't changes
  2. The result i want is; Result i want The result i get; Result i get

By the way this script was working last day, i tested it several times and save&closed it, other day i changed nothing and just open the client and test it again and noticed it doesn't work.

\$\endgroup\$
1
\$\begingroup\$

I've solved my problem, if you're having the some issue:

public Vector2Int size = new Vector2Int(10, 10);
public float spacing = 1f;
public MeshRenderer tilePrefab;
MeshRenderer[,] tiles;

public Material fallingMaterial;
public Material risingMaterial;
public float fallSeconds = 3f;
public float fallHeight = -4f;
public float riseSeconds = 4f;

void Awake()
{
    CreateFloor();
    StartCoroutine(AnimationLoop(3f));
    transform.position = new Vector3(0, 0, -20);
}

void CreateFloor()
{
    // Define our tiles array in the appropriate size.
    tiles = new MeshRenderer[size.x, size.y];

    // Spawn the floor centered around this object's position.
    Vector3 origin = transform.position + new Vector3(size.x, 0, size.y) * -0.5f * spacing;

    // For each row, and each column, instantiate a tile.
    for (int x = 0; x < size.x; x++)
    {
        for (int y = 0; y < size.y; y++)
        {
            tiles[x, y] = Instantiate<MeshRenderer>(tilePrefab, new Vector3(x, 0, y) * spacing + origin, Quaternion.identity);
        }
    }
    for (int x = 0; x < size.x; x++)
    {
        for (int y = 0; y < size.y; y++)
        {
            tiles[x, y].gameObject.layer = LayerMask.NameToLayer("GroundLayer");
        }
    }
}

void SelectLeftHalf(List<MeshRenderer> pattern)
{
    Debug.Log("SelectLeftHalf");
    pattern.Clear();
    for (int x = 0; x < size.x / 2; x++)
    {
        for (int y = 0; y < size.y; y++)
        {
            pattern.Add(tiles[x, y]);
        }
    }
}

void ChangeAllMaterials(List<MeshRenderer> pattern, Material material)
{
    foreach (var renderer in pattern)
        renderer.sharedMaterial = material;
}

IEnumerator SlideAllBlocks(List<MeshRenderer> pattern, float startHeight, float endHeight, float duration)
{
    float progress = 0f;
    while (progress < 1f)
    {
        progress = Mathf.Clamp01(progress + Time.deltaTime / duration);

        // Compute a height to move to, with an ease-out curve.
        float height = Mathf.Lerp(startHeight, endHeight, 1 - (1 - progress) * (1 - progress));

        // Set all blocks in the pattern to this height.
        foreach (var renderer in pattern)
        {
            var position = renderer.transform.position;
            position.y = height;
            renderer.transform.position = position;
        }

        // Wait one frame, then resume.
        yield return null;
    }
}

IEnumerator AnimationLoop(float moveSeconds)
{
    // Prep our variable for tracking the pattern of tiles we're acting on.
    // Since this is a local variable, we can control exactly who gets to act on it,
    // so anything that changes the pattern should be easy to track down.
    var pattern = new List<MeshRenderer>();

    while (true)
    {
        // Each cycle, select a randomly-chosen pattern of blocks.
        int selection = Random.Range(1, 2);

        switch (selection)
        {
            case 1: SelectLeftHalf(pattern); break;
        }

        // Change the material of all blocks in this pattern.
        ChangeAllMaterials(pattern, fallingMaterial);

        // Wait before we start to fall.
        yield return new WaitForSeconds(moveSeconds);

        // Chain control to our sliding method until the fall is complete.
        yield return SlideAllBlocks(pattern, 0f, fallHeight, fallSeconds);

        // Done falling. Reset the materials and rise back up.
        ChangeAllMaterials(pattern, risingMaterial);

        // Chain control to our sliding method until the fall is complete.
        yield return SlideAllBlocks(pattern, fallHeight, 0f, riseSeconds);

        // Everything has risen and reset. Now adjust our moveSeconds for next cycle:
        if (moveSeconds >= 1.3f)
        {
            moveSeconds -= 0.7f;
        }
        else if (moveSeconds <= 1.3f)
        {
            moveSeconds = Mathf.Max(moveSeconds - 0.6f, 0.7f);
        }

        // Wait before starting the next cycle & choosing a new pattern.
        yield return new WaitForSeconds(moveSeconds);
    }
}

It can be look complicated at the first look but trust me it's simple.

If you are having a problem with understanding, check this link, at that post DmGregory has a great answer about it.

\$\endgroup\$

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.