2017-05-23 4 views
2

私は蛇ゲームに取り組んでいます。私が入力を(上、下、左、右)速く打つと、私のヘビはそれ自身に入ります。私は私の動きを規制するために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; 
} 

答えて

0

...私は完全にあなたの質問を理解していないことを確認あなたが遅延する前に方向を変更しようと

もしそうなら、私がしようとします:

StartCoroutine(ChangeDirectionTo(Direction.North, 0.05f)); 
IEnumarator ChangeDirectionTo(Direction direction, float delay) { 
    yield return new WaitForSeconds(delay); 
    this.direction = direction; 
} 
+0

Hey @Emanouil入力に遅延を追加しようとしています(キーボードとボタンのUIの両方)。私のヘビを「動かす」ために、私のプログラムがユーザの選択した方向に立方体を作成する間隔に設定されたInvokeRepeating()を実行します。蛇が後方に移動できないように入力をコーディングしている間に(つまり、ヘビが北に行くなら、それは南に行くことができません)、問題は、ユーザーが現在ヘビを動かすことができるよりも速く方向を変えるならば、 (つまり、ヘビが北に行くと、ユーザーが東と南に向かって方向を変えて、フレームが終了するとヘビが遭遇するようになる) – xslipstream

+0

うん、あなたのアップデートを見て、コルーチンはあなたの問題の半分を解決した。 コルーチンはあなたのボタンにリンクするためのインスペクタですが、私は通常、補助的なpublic関数(IEnumatorではなくvoid)を作成してこの問題を解決し、それからクルーチンを呼び出します –

+0

私は周りを遊んでいて、同じ考えを思いつきました。各方向の個々のコルーチンを作成し、私の公開入力機能からそれらを呼び出す。私は今必要な遅延を作成しているようだ! – xslipstream

0

は、私は問題を参照してくださいと思います。 現在ののスネークの方向と、ユーザーのの方向を区別する必要があります。したがって

direction = nextDirection; 

プレーヤー:このような何かを試してみてください:プレイヤーが行きたいところ

if (direction != Direction.South && Input.GetKeyDown(KeyCode.UpArrow)) 
    nextDirection = Direction.North; 
if (direction != Direction.West && Input.GetKeyDown(KeyCode.RightArrow)) 
    nextDirection = Direction.East; 
if (direction != Direction.North && Input.GetKeyDown(KeyCode.DownArrow)) 
    nextDirection = Direction.South; 
if (direction != Direction.East && Input.GetKeyDown(KeyCode.LeftArrow)) 
    nextDirection = Direction.West; 
} 

を次に、あなたが実際にそれを産卵する前に、キューブを作成するコードで、あなたに伝えnextDirectionを使用キューブが生成される前に方向を変更する機会はありません。

これはうまくいくはずですが、ヘビのゲームが持っている素晴らしい "入力バッファリング"効果が得られないという欠点があります。そこでは、ヘビがU-順番。もしあなたがそれをしたいのであれば、ある種のスマートバッファリングシステムを実装する必要があります。これは、プレーヤーが非常に特殊な「無効な」入力(north/south方向が北、nextDirectionが東向き)将来の方向転換のためのスネーク。戦闘ゲームのための "特別な動き"入力をバッファリングするようなもの。

+0

提案ありがとう@Nat – xslipstream

+0

私の方向制御でコルーチンを使って解決策を見つけました。それは自分のキーボード入力で動作します。しかし、私はまだ私のボタンのUI入力で解決策を探しています。私はボタンから渡されたパラメータを使用するコルーチンを実行する方法を知らない – xslipstream

関連する問題