Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

230 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Farming game Time management 0 Answers

How is it possible to verify values and is they are correct activate objects ? 0 Answers

My countdown timer doesn't work,I'm trying to create a countdown timer 0 Answers

Where and how should I stop the counter when the while loop is ended ? 0 Answers

I need a 2 hour realtime timer for my game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges