私は蛇ゲームに取り組んでいます。私が入力を(上、下、左、右)速く打つと、私のヘビはそれ自身に入ります。私は私の動きを規制するためにUpdate()の下にタイマーを作成しようとしましたが、私は蛇がまだ時々動き回っているのを発見します。Unity - 私のボタンに遅延を追加する
私はInvokeRepeating()を使って0.05秒ごとにスネークを動かし、2種類の入力方法があります:1)キーボード入力(上、下、左、右矢印)と2)ゲームパッドUI 、下、左、右のボタン)。どのように私の方向性の変化を規制することができますか?
void Start() {
Screen.orientation = ScreenOrientation.Portrait;
isPaused = false;
InvokeRepeating("TimerInvoke", 0, deltaTimer);
}
void TimerInvoke() {
Movement();
StartCoroutine(checkVisible());
if (currentSize == maxSize)
TailFunction();
else
currentSize++;
}
void Update() {
CompDirectionControl();
PauseCMD();
}
void CompDirectionControl() {
if (direction != Direction.South && Input.GetKeyDown(KeyCode.UpArrow))
direction = Direction.North;
if (direction != Direction.West && Input.GetKeyDown(KeyCode.RightArrow))
direction = Direction.East;
if (direction != Direction.North && Input.GetKeyDown(KeyCode.DownArrow))
direction = Direction.South;
if (direction != Direction.East && Input.GetKeyDown(KeyCode.LeftArrow))
direction = Direction.West;
}
public void MPadDirectionControl(int numDirection)
{
if (direction != Direction.South && numDirection == 0)
direction = Direction.North;
if (direction != Direction.West && numDirection == 1)
direction = Direction.East;
if (direction != Direction.North && numDirection == 2)
direction = Direction.South;
if (direction != Direction.East && numDirection == 3)
direction = Direction.West;
}
私はこのようなものを作成しようとしたが、遅延が
if (Time.time >= timeStamp)
{
CompDirectionControl();
timeStamp = Time.time + moveInterval;
}
UPDATE 非常に矛盾している私は、コルーチンを使用して、私のキーボード入力に遅延を追加することができました。しかし、私はボタンのUI入力と同じようにする方法を考え出していません。 IEnumeratorのGamePadDirControl(int方向)を公開すると、ボタンのパラメータをIEnumatorに渡すにはどうすればよいですか?私は公衆に私のGamePadDirControlを設定していても、私は私のボタンUIのためにそれに
IEnumerator DirControl() {
if (direction != Direction.South && Input.GetKeyDown(KeyCode.UpArrow))
{
yield return new WaitForSeconds(deltaTimer);
direction = Direction.North;
}
else if (direction != Direction.West && Input.GetKeyDown(KeyCode.RightArrow))
{
yield return new WaitForSeconds(deltaTimer);
direction = Direction.East;
}
else if (direction != Direction.North && Input.GetKeyDown(KeyCode.DownArrow))
{
yield return new WaitForSeconds(deltaTimer);
direction = Direction.South;
}
else if (direction != Direction.East && Input.GetKeyDown(KeyCode.LeftArrow))
{
yield return new WaitForSeconds(deltaTimer);
direction = Direction.West;
}
}
UPDATE 2 を私のボタンをリンクすることはできませんので、私は私のインスペクタでそれを見ることはありません - それはできませんので、私のボタンがIEnumator関数とリンクすることが可能なので、私は各方向について個別のコルーチンを作成し、私の公式のvoid MPadDirectionControl(int numDirection)関数から呼び出すことにしました。
は、ボタンのUI入力にコードを更新
public void MPadDirectionControl(int numDirection)
{
if (direction != Direction.South && numDirection == 0)
{
StartCoroutine(IntervalN());
//direction = Direction.North;
}
else if (direction != Direction.West && numDirection == 1)
{
StartCoroutine(IntervalE());
//direction = Direction.East;
}
else if (direction != Direction.North && numDirection == 2)
{
StartCoroutine(IntervalS());
//direction = Direction.South;
}
else if (direction != Direction.East && numDirection == 3)
{
StartCoroutine(IntervalW());
//direction = Direction.West;
}
}
IEnumerator IntervalN() {
yield return new WaitForSeconds(deltaTimer);
direction = Direction.North;
}
IEnumerator IntervalE()
{
yield return new WaitForSeconds(deltaTimer);
direction = Direction.East;
}
IEnumerator IntervalS()
{
yield return new WaitForSeconds(deltaTimer);
direction = Direction.South;
}
IEnumerator IntervalW()
{
yield return new WaitForSeconds(deltaTimer);
direction = Direction.West;
}
Hey @Emanouil入力に遅延を追加しようとしています(キーボードとボタンのUIの両方)。私のヘビを「動かす」ために、私のプログラムがユーザの選択した方向に立方体を作成する間隔に設定されたInvokeRepeating()を実行します。蛇が後方に移動できないように入力をコーディングしている間に(つまり、ヘビが北に行くなら、それは南に行くことができません)、問題は、ユーザーが現在ヘビを動かすことができるよりも速く方向を変えるならば、 (つまり、ヘビが北に行くと、ユーザーが東と南に向かって方向を変えて、フレームが終了するとヘビが遭遇するようになる) – xslipstream
うん、あなたのアップデートを見て、コルーチンはあなたの問題の半分を解決した。 コルーチンはあなたのボタンにリンクするためのインスペクタですが、私は通常、補助的なpublic関数(IEnumatorではなくvoid)を作成してこの問題を解決し、それからクルーチンを呼び出します –
私は周りを遊んでいて、同じ考えを思いつきました。各方向の個々のコルーチンを作成し、私の公開入力機能からそれらを呼び出す。私は今必要な遅延を作成しているようだ! – xslipstream