1

Basicamente com este código estou a tentar fazer com que a cor e a cor de emissão do objeto mudem sistematicamente num tempo pré-determinado, mas sempre que o totalTimerchega por volta do valor de 300 as cores basicamente param de mudar. Estou a usar a versão 2019.2.5f1 do Unity para este projeto. Assim sendo alguém sabe o que se passa?

P.S: Uma coisa que denotei foi que sempre que mudo o valor do frameTimer para valores maiores o valor no qual o totalTimer muda também, mas como estou a usar o frameTimer como contador de frames, não seria benéfico ter um valor acima de 60.

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

public class ColorChanger : MonoBehaviour
{
    #region State Variables
    //state variables
    private int frameTimer;
    private int totalTimer = 0;
    private int newSpeed;
    private int colorChanger;
    #endregion

    #region Properties
    public int NewSpeed
    {
        get => newSpeed;
    }
    #endregion

    Renderer rend;

    public Color[] colors;
    private Color newColor, oldColor;


    // Start is called before the first frame update
    void Start()
    {
        rend = GetComponent<Renderer>();
        newSpeed = 40;
        #region Color instantion
        colors = new Color[9];
        colors[0] = (new Color(0.5098f, 0.3254f, 0.1882f));    //brown
        colors[1] = (new Color(0.8941f, 0.9137f, 0.0392f));    //yellow 
        colors[2] = (new Color(0.1607f, 0.8705f, 0.9568f));    //light blue
        colors[3] = (new Color(1f, 0.5215f, 0.0745f));         //orange
        colors[4] = (new Color(0.7607f, 0.2274f, 0.8588f));    //lightish purple
        colors[5] = (new Color(0.3254f, 0.8588f, 0.2274f));    //green
        colors[6] = (new Color(0.9568f, 0.2470f, 0.1607f));    //red
        colors[7] = (new Color(0.7294f, 0.1490f, 0.1450f));    //crimson
        colors[8] = (new Color(0.0705f, 0.3960f, 0.9019f));    //blue
        #endregion
    }

    // Update is called once per frame
    void Update()
    {
        frameTimer++;
        colorChanger++;

        if (frameTimer == 30)
        {
            SpeedChanger(totalTimer++); frameTimer = 0;
           Debug.Log($"The Total Timer is {totalTimer}");
        }

        #region Color Update
        if(colorChanger == newSpeed)
        {
            newColor = colors[Random.Range(0, colors.Length - 1)];

            if(newColor == oldColor)
            {
                while(newColor == oldColor)
                    newColor = colors[Random.Range(0, colors.Length - 1)];
            }
            oldColor = newColor;
            rend.material.SetColor("_Color", newColor);
            rend.material.SetColor("_EmissionColor", newColor);
            rend.material.EnableKeyword("_EMISSION");
            colorChanger = 0;
            //Debug.Log( $"\n {newColor} this is the frame color");
        }
        #endregion
    }

    private int SpeedChanger(int totalTimer)
    {
        switch(totalTimer)
        {
            case 85:
                newSpeed = 30;
                break;
            case 165:
                newSpeed = 25;
                break;
            case 200:
                newSpeed = 20;
                break;
            case 300:
                newSpeed = 15;
                break;
        }
        return newSpeed; 
    }
}

1 Resposta 1

1

Acontece porque no momento que você muda o newSpeed chamando o metodo SpeedChanger(int totalTimer) o seu colorChanger é maior que quinze e ele numca mais vai ser zero por que pra isso o collorChange tem que ser igual ao newSpeed.

Mas sinceramente essa não é a melhor forma de fazer isso. O melhor seria fazer um contador usando o time.deltatime, até por que assim como esta a velocidade que vai trocar a cor do teu objeto vai depender do clock da maquina que estiver rodando.

Ficaria mais ou menos assim

#region State Variables
    //state variables
    private float howManyChanges;
    private float speedToChange;
    private float timer = 0;
    private int lastRandomColorIndex;
    public Color[] colors;
    #endregion

    Renderer rend;



    // Start is called before the first frame update
    void Start()
    {
        rend = GetComponent<Renderer>();
        speedToChange = 5;
        #region Color instantion
        colors = new Color[9];
        colors[0] = (new Color(0.5098f, 0.3254f, 0.1882f));    //brown
        colors[1] = (new Color(0.8941f, 0.9137f, 0.0392f));    //yellow 
        colors[2] = (new Color(0.1607f, 0.8705f, 0.9568f));    //light blue
        colors[3] = (new Color(1f, 0.5215f, 0.0745f));         //orange
        colors[4] = (new Color(0.7607f, 0.2274f, 0.8588f));    //lightish purple
        colors[5] = (new Color(0.3254f, 0.8588f, 0.2274f));    //green
        colors[6] = (new Color(0.9568f, 0.2470f, 0.1607f));    //red
        colors[7] = (new Color(0.7294f, 0.1490f, 0.1450f));    //crimson
        colors[8] = (new Color(0.0705f, 0.3960f, 0.9019f));    //blue
        #endregion
        NewRandomColor();
    }

    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;

        if (timer >= speedToChange)
        {
            howManyChanges++;
            SpeedChanger();
            NewRandomColor();
            timer = 0;
        }
    }

    private void NewRandomColor()
    {
        int randomColorIndex = Random.Range(0, colors.Length - 1);
        if(lastRandomColorIndex == randomColorIndex)
        {
            if (randomColorIndex == colors.Length -1)
            {
                randomColorIndex--;
            }
            else
            {
                randomColorIndex++;
            }
        }

        lastRandomColorIndex = randomColorIndex;
        Color newColor = colors[Random.Range(0, colors.Length - 1)];
        rend.material.SetColor("_Color", newColor);
        rend.material.SetColor("_EmissionColor", newColor);
        rend.material.EnableKeyword("_EMISSION");
    }

    private void SpeedChanger()
    {
        switch (howManyChanges)
        {
            case 5: //Quantas vezes já trocou
                speedToChange = 4; //Segundos para proxima troca
                break;
            case 10:
                speedToChange = 3; //Segundos para proxima troca
                break;
            case 15:
                speedToChange = 2; //Segundos para proxima troca
                break;
            case 20:
                speedToChange = 1; //Segundos para proxima troca
                break;
        }
    }

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .