0
\$\begingroup\$

Here is my all GameControl script:

static GameObject HexGrey;
static GameObject HexPurple;
static GameObject HexBlue;
static GameObject HexGreen;
static GameObject HexTurq;
static GameObject HexRed;
static GameObject HexYellow;

Vector3 GreyVec;
Vector3 PurpleVec;
Vector3 BlueVec;
Vector3 GreenVec;
Vector3 TurqVec;
Vector3 RedVec;
Vector3 YellowVec;
Vector3 GreyVecOrigin;
Vector3 PurpleVecOrigin;
Vector3 BlueVecOrigin;
Vector3 GreenVecOrigin;
Vector3 TurqVecOrigin;
Vector3 RedVecOrigin;
Vector3 YellowVecOrigin;

public Button button;                       // DeathCanvas
public Text text;

public Material wallMat;

float t;

public static int a = 0;
public static int b = 0;
float wallMoveT = 0.7f;

private IEnumerator coroutine;

void Start()
{
    button.gameObject.SetActive(false);         // DeathCanvas
    text.enabled = false;
    coroutine = enumerator(3);
    StartCoroutine(coroutine);
    ObjectAssignment();
    VecAssignment();
}

void Update()
{
    WallControl();
}

void ObjectAssignment()
{
    HexGrey = GameObject.FindGameObjectWithTag("HexGrey");
    HexPurple = GameObject.FindGameObjectWithTag("HexPurple");
    HexBlue = GameObject.FindGameObjectWithTag("HexBlue");
    HexGreen = GameObject.FindGameObjectWithTag("HexGreen");
    HexTurq = GameObject.FindGameObjectWithTag("HexLBlue");
    HexRed = GameObject.FindGameObjectWithTag("HexRed");
    HexYellow = GameObject.FindGameObjectWithTag("HexYellow");
}

void VecAssignment()
{
    t += Time.deltaTime / wallMoveT;

    GreyVecOrigin = new Vector3(HexGrey.transform.position.x, 0, HexGrey.transform.position.z);
    PurpleVecOrigin = new Vector3(HexPurple.transform.position.x, 0, HexPurple.transform.position.z);
    BlueVecOrigin = new Vector3(HexBlue.transform.position.x, 0, HexBlue.transform.position.z);
    GreenVecOrigin = new Vector3(HexGreen.transform.position.x, 0, HexGreen.transform.position.z);
    TurqVecOrigin = new Vector3(HexTurq.transform.position.x, 0, HexTurq.transform.position.z);
    RedVecOrigin = new Vector3(HexRed.transform.position.x, 0, HexRed.transform.position.z);
    YellowVecOrigin = new Vector3(HexYellow.transform.position.x, 0, HexYellow.transform.position.z);

    GreyVec = new Vector3(HexGrey.transform.position.x, -4, HexGrey.transform.position.z);
    PurpleVec = new Vector3(HexPurple.transform.position.x, -4, HexPurple.transform.position.z);
    BlueVec = new Vector3(HexBlue.transform.position.x, -4, HexBlue.transform.position.z);
    GreenVec = new Vector3(HexGreen.transform.position.x, -4, HexGreen.transform.position.z);
    TurqVec = new Vector3(HexTurq.transform.position.x, -4, HexTurq.transform.position.z);
    RedVec = new Vector3(HexRed.transform.position.x, -4, HexRed.transform.position.z);
    YellowVec = new Vector3(HexYellow.transform.position.x, -4, HexYellow.transform.position.z);
}

void WallControl()
{
    if (a == 1)
    {
        wallMat.color = Color.grey;
        if (b == 1)
        {
            HexPurple.transform.position = Vector3.Lerp((HexPurple.transform.position), (PurpleVec), t);
            HexBlue.transform.position = Vector3.Lerp((HexBlue.transform.position), (BlueVec), t);
            HexGreen.transform.position = Vector3.Lerp((HexGreen.transform.position), (GreenVec), t);
            HexTurq.transform.position = Vector3.Lerp((HexTurq.transform.position), (TurqVec), t);
            HexRed.transform.position = Vector3.Lerp((HexRed.transform.position), (RedVec), t);
            HexYellow.transform.position = Vector3.Lerp((HexYellow.transform.position), (YellowVec), t);
            Debug.Log("GREY");
        }
    }
    else if (a == 2)
    {
        wallMat.color = Color.magenta;
        if (b == 2)
        {
            HexGrey.transform.position = Vector3.Lerp((HexGrey.transform.position), (GreyVec), t);
            HexBlue.transform.position = Vector3.Lerp((HexBlue.transform.position), (BlueVec), t);
            HexGreen.transform.position = Vector3.Lerp((HexGreen.transform.position), (GreenVec), t);
            HexTurq.transform.position = Vector3.Lerp((HexTurq.transform.position), (TurqVec), t);
            HexRed.transform.position = Vector3.Lerp((HexRed.transform.position), (RedVec), t);
            HexYellow.transform.position = Vector3.Lerp((HexYellow.transform.position), (YellowVec), t);
            Debug.Log("MAGENTA");
        }
    }
    else if (a == 3)
    {
        wallMat.color = Color.blue;
        if (b == 3)
        {
            HexPurple.transform.position = Vector3.Lerp((HexPurple.transform.position), (PurpleVec), t);
            HexGrey.transform.position = Vector3.Lerp((HexGrey.transform.position), (GreyVec), t);
            HexGreen.transform.position = Vector3.Lerp((HexGreen.transform.position), (GreenVec), t);
            HexTurq.transform.position = Vector3.Lerp((HexTurq.transform.position), (TurqVec), t);
            HexRed.transform.position = Vector3.Lerp((HexRed.transform.position), (RedVec), t);
            HexYellow.transform.position = Vector3.Lerp((HexYellow.transform.position), (YellowVec), t);
            Debug.Log("BLUE");
        }
    }
    else if (a == 4)
    {
        wallMat.color = Color.green;
        if (b == 4)
        {
            HexPurple.transform.position = Vector3.Lerp((HexPurple.transform.position), (PurpleVec), t);
            HexBlue.transform.position = Vector3.Lerp((HexBlue.transform.position), (BlueVec), t);
            HexGrey.transform.position = Vector3.Lerp((HexGrey.transform.position), (GreyVec), t);
            HexTurq.transform.position = Vector3.Lerp((HexTurq.transform.position), (TurqVec), t);
            HexRed.transform.position = Vector3.Lerp((HexRed.transform.position), (RedVec), t);
            HexYellow.transform.position = Vector3.Lerp((HexYellow.transform.position), (YellowVec), t);
            Debug.Log("GREEN");
        }
    }
    else if (a == 5)
    {
        wallMat.color = Color.cyan;
        if (b == 5)
        {
            HexPurple.transform.position = Vector3.Lerp((HexPurple.transform.position), (PurpleVec), t);
            HexBlue.transform.position = Vector3.Lerp((HexBlue.transform.position), (BlueVec), t);
            HexGreen.transform.position = Vector3.Lerp((HexGreen.transform.position), (GreenVec), t);
            HexGrey.transform.position = Vector3.Lerp((HexGrey.transform.position), (GreyVec), t);
            HexRed.transform.position = Vector3.Lerp((HexRed.transform.position), (RedVec), t);
            HexYellow.transform.position = Vector3.Lerp((HexYellow.transform.position), (YellowVec), t);
            Debug.Log("CYAN");
        }
    }
    else if (a == 6)
    {
        wallMat.color = Color.red;
        if (b == 6)
        {
            HexPurple.transform.position = Vector3.Lerp((HexPurple.transform.position), (PurpleVec), t);
            HexBlue.transform.position = Vector3.Lerp((HexBlue.transform.position), (BlueVec), t);
            HexGreen.transform.position = Vector3.Lerp((HexGreen.transform.position), (GreenVec), t);
            HexTurq.transform.position = Vector3.Lerp((HexTurq.transform.position), (TurqVec), t);
            HexGrey.transform.position = Vector3.Lerp((HexGrey.transform.position), (GreyVec), t);
            HexYellow.transform.position = Vector3.Lerp((HexYellow.transform.position), (YellowVec), t);
            Debug.Log("RED");
        }
    }
    else if (a == 7)
    {
        wallMat.color = Color.yellow;
        if (b == 7)
        {
            HexPurple.transform.position = Vector3.Lerp((HexPurple.transform.position), (PurpleVec), t);
            HexBlue.transform.position = Vector3.Lerp((HexBlue.transform.position), (BlueVec), t);
            HexGreen.transform.position = Vector3.Lerp((HexGreen.transform.position), (GreenVec), t);
            HexTurq.transform.position = Vector3.Lerp((HexTurq.transform.position), (TurqVec), t);
            HexRed.transform.position = Vector3.Lerp((HexRed.transform.position), (RedVec), t);
            HexGrey.transform.position = Vector3.Lerp((HexGrey.transform.position), (GreyVec), t);
            Debug.Log("YELLOW");
        }
    }
    else if (a == 8)
    {
        wallMat.color = Color.black;
        HexGrey.transform.position = Vector3.Lerp((HexGrey.transform.position), (GreyVecOrigin), t);
        HexPurple.transform.position = Vector3.Lerp((HexPurple.transform.position), (PurpleVecOrigin), t);
        HexBlue.transform.position = Vector3.Lerp((HexBlue.transform.position), (BlueVecOrigin), t);
        HexGreen.transform.position = Vector3.Lerp((HexGreen.transform.position), (GreenVecOrigin), t);
        HexTurq.transform.position = Vector3.Lerp((HexTurq.transform.position), (TurqVecOrigin), t);
        HexRed.transform.position = Vector3.Lerp((HexRed.transform.position), (RedVecOrigin), t);
        HexYellow.transform.position = Vector3.Lerp((HexYellow.transform.position), (YellowVecOrigin), t);
        Debug.Log("BLACK");
    }
    else
    {
        wallMat.color = Color.black;
    }
}

private IEnumerator enumerator(int waitTime)
{
    while (true)
    {
        yield return new WaitForSeconds(waitTime);
        a = Random.Range(1, 8);
        yield return new WaitForSeconds(1.5f);
        b = a;
        yield return new WaitForSeconds(waitTime);
        a = 8;
        b = 0;
    }
}

public void GoToMainMenu()
{
    SceneManager.LoadScene("MainMenu");
}

I know using integers named "a" and "b" looks like complicated, but infact it's not. First a pick random int inRange 1-8 (and color of the walls changes), then waits for 1.5f seconds and i say "b = a" (hexs' transform.position changes).

I've checked the a and b integers but the values are right.

What I want: first change the color of the walls randomly. Then wait 1.5f seconds and transform the objects according to walls' color. And loop this cycle.

Problem is: Color of the walls changes but objects positions never change.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

It looks like you forgot to update your t variable.

From what I understand of your code, you'll want to reset it in your IEnumerator enumerator(int waitTime) function:

private IEnumerator enumerator(int waitTime)
{
    while (true)
    {
        yield return new WaitForSeconds(waitTime);
        a = Random.Range(1, 8);
        yield return new WaitForSeconds(1.5f);
        b = a;
        t = 0;  // <--- reset t timer here
        t += Time.deltaTime / wallMoveT;  // <--- update it here
        yield return new WaitForSeconds(waitTime);
        ...

There are still two issues I see with this code, and I'll tackle them separately.


Once your walls will be moving, you'll most likely see that they're not moving as you expect, linearly: they'll be moving with a "curved" speed. This is because you use the current location as one of the parameters for your lerp.

You are very close to get this working: you have kept your "original" and you "target" positions separately, you just need to use these for the lerp instead of using the current object position:

if (b == 1)
{
    HexPurple.transform.position = Vector3.Lerp(HexPurpleOrigin, PurpleVec, t);
    HexBlue.transform.position = Vector3.Lerp(HexBlueOrigin, BlueVec, t);
    HexGreen.transform.position = Vector3.Lerp(HexGreenOrigin, GreenVec, t);

All of this code repetition seems a bit error prone. I'm not too familiar with c# but I would try to build arrays of things based on enums and act upon them.

This is all untested code, but that would give you an idea:

// First, you need your enum:
enum HexColors {
  Grey,
  Purple,
  Blue,
  Green,
  Turq,
  Red,
  Yellow,
  Count
};

static GameObject[] gameObjects = new GameObject[(int)HexColors.Count];
Vector3[] vec = new Vector3[(int)HexColors.Count];
Vector3[] vecOrigin = new Vector3[(int)HexColors.Count];

public Button button;                       // DeathCanvas
public Text text;

public Material wallMat;

float t;

public static int a = 0;
public static int b = 0;
float wallMoveT = 0.7f;

private IEnumerator coroutine;

void Start()
{
    button.gameObject.SetActive(false);         // DeathCanvas
    text.enabled = false;
    coroutine = enumerator(3);
    StartCoroutine(coroutine);
    ObjectAssignment();
    VecAssignment();
}

void Update()
{
    WallControl();
}

void ObjectAssignment()
{
    gameObjects[(int)HexColors.Grey  ] = GameObject.FindGameObjectWithTag("HexGrey");
    gameObjects[(int)HexColors.Purple] = GameObject.FindGameObjectWithTag("HexPurple");
    gameObjects[(int)HexColors.Blue  ] = GameObject.FindGameObjectWithTag("HexBlue");
    gameObjects[(int)HexColors.Green ] = GameObject.FindGameObjectWithTag("HexGreen");
    gameObjects[(int)HexColors.Turq  ] = GameObject.FindGameObjectWithTag("HexLBlue");
    gameObjects[(int)HexColors.Red   ] = GameObject.FindGameObjectWithTag("HexRed");
    gameObjects[(int)HexColors.Yellow] = GameObject.FindGameObjectWithTag("HexYellow");
}

void VecAssignment()
{
    for (int i = 0; i < (int)HexColors.Count; i++)
    {
        vecOrigin[i] = new Vector3( gameObjects[i].transform.position.x,  0, gameObjects[i].transform.position.z);
        vec      [i] = new Vector3( gameObjects[i].transform.position.x, -4, gameObjects[i].transform.position.z);
    }
}

void WallControl()
{
    if (a == b)
    {
        for (int i = 0; i < (int)HexColors.Count; i++)
        {
            if (i+1 != a)
                gameObjects[i].transform.position = Vector3.Lerp((gameObjects[i].transform.position), (vec[i]), t);
        }
    }

    if (a == 1)
    {
        wallMat.color = Color.grey;
        if (b == 1)
            Debug.Log("GREY");
    }
    else if (a == 2)
    {
        wallMat.color = Color.magenta;
        if (b == 2)
            Debug.Log("MAGENTA");
    }
    else if (a == 3)
    {
        wallMat.color = Color.blue;
        if (b == 3)
            Debug.Log("BLUE");
    }
    else if (a == 4)
    {
        wallMat.color = Color.green;
        if (b == 4)
            Debug.Log("GREEN");
    }
    else if (a == 5)
    {
        wallMat.color = Color.cyan;
        if (b == 5)
            Debug.Log("CYAN");
    }
    else if (a == 6)
    {
        wallMat.color = Color.red;
        if (b == 6)
            Debug.Log("RED");
    }
    else if (a == 7)
    {
        wallMat.color = Color.yellow;
        if (b == 7)
            Debug.Log("YELLOW");
    }
    else if (a == 8)
    {
        wallMat.color = Color.black;
        for (int i = 0; i < (int)HexColors.Count; i++)
            gameObjects[i].transform.position = Vector3.Lerp((gameObjects[i].transform.position), (vecOrigin[i]), t);
        Debug.Log("BLACK");
    }
    else
    {
        wallMat.color = Color.black;
    }
}

private IEnumerator enumerator(int waitTime)
{
    while (true)
    {
        yield return new WaitForSeconds(waitTime);
        a = Random.Range(1, 8);
        yield return new WaitForSeconds(1.5f);
        b = a;
        yield return new WaitForSeconds(waitTime);
        a = 8;
        b = 0;
    }
}

public void GoToMainMenu()
{
    SceneManager.LoadScene("MainMenu");
}

Someone better with c# than I am could comment, but that's a basis. This would save you a lot of repeating code and make the code a bit easier to read and to understand.

\$\endgroup\$
3
  • \$\begingroup\$ You found the problem but not the solution, when a==b it makes t = 0 then makes t += Time.deltaTime / wallMoveT continuously 'cause it's in the Update method. Which moves my objects faster when a = 1-8 range and teleports them suddenly when a = 8 \$\endgroup\$ Apr 27, 2019 at 9:08
  • \$\begingroup\$ I fixed it, and edited your answer, all you have to do update t variable in the enumerator, not the wallControl void. \$\endgroup\$ Apr 27, 2019 at 9:15
  • \$\begingroup\$ Oh, ok, sorry! Well, glad I could help! \$\endgroup\$
    – Vaillancourt
    Apr 27, 2019 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .