- Home /
Make a sum once per time, in the update function
I have this code, the commented part i want to do once per time, and because i need to put this code in the update function is constantly do the sum every frame, but i just need once per time when the player press the the arrowKeys. i Try to recreate this game: http://youtu.be/muJFwvf6gNE?t=1m44s especially the speedbar. Thanks and sorry if my english suck.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
private bool avanzar = false;
private float velocidadAvanza = 15f;
private float sumaPower = 0.1f;
void Start () {
}
void Update () {
Avanzar();
}
void Avanzar()
{
if(Input.GetKeyDown(KeyCode.RightArrow))
{
if(avanzar==false)
{
transform.Translate(Vector3.right * velocidadAvanza * Time.deltaTime);
avanzar = true;
PowerBar.restaPower += sumaPower; // <== Do this only Once
}
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
if(avanzar==true)
{
transform.Translate(Vector3.right * velocidadAvanza * Time.deltaTime);
avanzar = false;
PowerBar.restaPower += sumaPower; // <== Do this only Once
}
}
}
}
I'm not sure what you are seeing, but the lines you point to with "Do this only Once" are only getting executed once. Get$$anonymous$$eyDown() only returns true for a single frame, so these lines will only be executed on the frame the key is pressed. Put a Debug.Log() statement just before the lines you are worried about. If you are seeing more than one Debug.Log() then likely you have the script attached to more than one place.
Answer by FrankStarsKo · Sep 21, 2013 at 03:25 AM
utiliza esto en tu powerbar para que con otro boton solo sea una vez como dices.
if (Input.GetKeyDown("space"))
{
PowerBar.restaPower += sumaPower; // <== Do this only Once
}
aunque yo no veo que esten declarados esas variables, no vas a querer que te hagan todo el sistema que quieres verdad?
bueno, espero te sirva.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Breaking a loop, Javascript? 3 Answers
Trigger Not Activating 0 Answers
How to stop spawn after a limit (int) 1 Answer
Call UnityWebRequest every Second,Repeat Unity Web Request every second. 0 Answers