Question by
Aderbal-chan · Dec 10, 2018 at 05:22 PM ·
scripting problemtimetimer countdowntimer-script
Allow player to Press Input.GetKeyDown once per update ?
Okay, I've got this code that's working perfectly
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ContadorPontuação : MonoBehaviour {
public float Pontuacao = 0;
public Text ppontuacao;
public int Vidas = 4;
public GameObject pisca1;
public GameObject pisca2;
public GameObject pisca3;
public GameObject pisca4;
public GameObject pisca5;
public GameObject pisca6;
// Use this for initialization
void Start () {
pisca1 = GameObject.Find("pisc 1");
pisca2 = GameObject.Find("pisc 2");
pisca3 = GameObject.Find("pisc 3");
pisca4 = GameObject.Find("pisc 4");
pisca5 = GameObject.Find("pisc 5");
pisca6 = GameObject.Find("pisc 6");
}
// Update is called once per frame
void Update () {
if (Vidas > 0) {
if (Input.GetKeyDown(KeyCode.Space)) {
if (GameObject.Find("pisc 1").GetComponent<Change_Color2>().CorInicial.Equals(Color.red) ||
GameObject.Find("pisc 2").GetComponent<Change_Color3>().CorInicial.Equals(Color.red) ||
GameObject.Find("pisc 3").GetComponent<Change_Color2>().CorInicial.Equals(Color.red) ||
GameObject.Find("pisc 4").GetComponent<Change_Color2>().CorInicial.Equals(Color.red) ||
GameObject.Find("pisc 5").GetComponent<Change_Color3>().CorInicial.Equals(Color.red) ||
GameObject.Find("pisc 6").GetComponent<Change_Color2>().CorInicial.Equals(Color.red)) {
Vidas--;
if (Vidas == 3){
GameObject.Find("Heart n1").gameObject.active = false;
}
else if (Vidas == 2){
GameObject.Find("Heart n2").gameObject.active = false;
}
else if (Vidas == 1){
GameObject.Find("Heart n3").gameObject.active = false;
}
else if (Vidas == 0){
GameObject.Find("Heart n4").gameObject.active = false;
}
}
else if (GameObject.Find("pisc 1").GetComponent<Change_Color2>().CorInicial.Equals(Color.yellow) ||
GameObject.Find("pisc 2").GetComponent<Change_Color3>().CorInicial.Equals(Color.yellow) ||
GameObject.Find("pisc 3").GetComponent<Change_Color2>().CorInicial.Equals(Color.yellow) ||
GameObject.Find("pisc 4").GetComponent<Change_Color2>().CorInicial.Equals(Color.yellow) ||
GameObject.Find("pisc 5").GetComponent<Change_Color3>().CorInicial.Equals(Color.yellow) ||
GameObject.Find("pisc 6").GetComponent<Change_Color2>().CorInicial.Equals(Color.yellow)) {
Pontuacao = Pontuacao + 20 + ((Vidas*10)/2);
}
else {
Pontuacao = Pontuacao - 20;
}
ppontuacao.text = Pontuacao.ToString();
}
}
}
}
but I want the code to recognize the space pressing only once per another code update (in the second code, the "update" wait mainly 4 seconds to go, and at the end of the game, it's 2 seconds or something around it), in resume, the Space must be counted only once following the variable "SegundosPorCiclo" from this Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Change_Color2 : MonoBehaviour {
public float Velocidade = 0.0f;
public Color CorInicial;
public Color CorFinal;
public bool Repete = false;
public int IsRunning = 1;
public float SegundosPorCiclo = 4;
public float ChecagemSegCicl;
public int ContadorCiclo;
public float TempoTotal = 0;
float startTime;
public ContadorPontuação cont;
public int cor = 0;
// Use this for initialization
void Start () {
startTime = Time.time;
}
// Update is called once per frame
void Update () {
if (IsRunning == 1){
StartCoroutine (Wait());
}
}
public IEnumerator Wait(){
IsRunning = 0;
yield return new WaitForSeconds (SegundosPorCiclo);
if (TempoTotal < 300 && (GameObject.Find("Text (1)").GetComponent<ContadorPontuação>().Vidas > 0)) {
CorInicial = CorFinal;
ContadorCiclo = ContadorCiclo + 1;
cor = UnityEngine.Random.Range(0, 5); //escolhendo a futura cor
if (cor == 1)
CorFinal = Color.red;
if (cor == 2)
CorFinal = Color.yellow;
if (cor == 3)
CorFinal = Color.blue;
if (cor == 4)
CorFinal = Color.green;
if (!Repete) // mudança de cor
{
float t = (Time.time - startTime) * Velocidade;
GetComponent<Renderer>().material.color = Color.LerpUnclamped(CorInicial, CorFinal, t);
}
else
{
float t = (Mathf.Sin(Time.time - startTime) * Velocidade);
GetComponent<Renderer>().material.color = Color.LerpUnclamped(CorInicial, CorFinal, t);
}
Debug.Log (ContadorCiclo);
IsRunning = 1;
TempoTotal = TempoTotal + SegundosPorCiclo; //acrescentando ao tempo final
if (TempoTotal == 60) //aumento de dificuldade a cada 60 seg
SegundosPorCiclo = SegundosPorCiclo - 0.5f;
if (TempoTotal > 119 && TempoTotal < 121)
SegundosPorCiclo = SegundosPorCiclo - 0.5f;
if (TempoTotal > 179 && TempoTotal < 181)
SegundosPorCiclo = SegundosPorCiclo - 0.5f;
if (TempoTotal > 239 && TempoTotal < 241)
SegundosPorCiclo = SegundosPorCiclo - 0.5f;
}
else { //mudando tudo pra verde quando acabar o programa
float t = (Time.time - startTime) * (Velocidade + 1);
GetComponent<Renderer>().material.color = Color.LerpUnclamped(CorInicial, Color.green, t);
}
ChecagemSegCicl = SegundosPorCiclo;
}
}
Comment