Hi, i need help.
I have this script to flap when is Time > nextFlap. But i want to flap only 1x when is Time > nextFlap, NO flapping always after Time > nextFlap.
How Can I do this?
Thank you very much.
Here is code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScriptPlayerInMenu : MonoBehaviour {
public float flapAmount = 10;
public float speed = 300;
public float flapRate = 0.5F;
private float nextFlap = 0.0F;
bool android;
bool started;
void Start () {
}
void Update () {
Vector3 vel = GetComponent().velocity;
print(vel);
if (Time.time > nextFlap)
{
vel = Flap(vel);
}
vel.x = speed * Time.deltaTime;
GetComponent().velocity = vel;
}
Vector3 Flap(Vector3 v)
{
v.y = flapAmount;
return v;
}
}
Just create a bool and when you are checking for whether enough time has passed, also check whther that bool is false.
Then when it triggers and enough time has passed, set ti to true.
I would create an example but I cant atm, perhaps someone else can.
So… We keep track of variables when we make scripts.
// somewhere in the top of the class
float TimeBetweenFlaps = 1;
float nextFlap;
// in Start probably
nextFlap = Time.time + TimeBetweenFlaps;
// later on when you want to flap
if(Time.time >= nextFlap) {
// flap code
nextFlap = Time.time + TimeBetweenFlaps;
}
There are great introduction tutorials for Unity available online.
You would really benefit from looking those over, especially introductions to scripting, for example.
I was hesitant to respond with a solution, since this is pretty basic, and you showed no work as to how you were trying…
![]()