2017-05-25 10 views
0

私はPing PongゲームをUnityの学習活動として取り組もうとしています。私はそれが永遠のために上下に跳ね上がっている場合、ピンポンボールのためのリセットシステムを作ることに取り組んでいます。一定期間後に変数を0に戻す方法

私はボールがゲームの上壁に当たった場合HitsTop変数は1

によって増加します、例えば1で接触変数をインクリメントする底壁コライダートリガーと上壁コライダートリガーを設定

同様に、底面の壁。私の問題は、ボールが底壁と上壁に10回当たったときにボールの位置をリセットすることです。

HitsTopHitsBottomの変数を一定の時間、たとえば5秒後にリセットするコードを追加したいとします。

これはC#で可能ですか?

私のコードは次のような次のとおりです。

using UnityEngine; 
using System.Collections; 

public class GameManager : MonoBehaviour { 

    public static int PlayerScore1 = 0; 
    public static int PlayerScore2 = 0; 
    public static int HitsTop = 0; 
    public static int HitsBottom = 0; 

    public GUISkin layout; 

    Transform theBall; 

    // Use this for initialization 
    void Start() { 
     theBall = GameObject.FindGameObjectWithTag("Ball").transform; 
    } 

    public static void Score (string wallID) { 
     if (wallID == "rightWall") 
     { 
      PlayerScore1++; 
     } else if (wallID == "leftWall") { 
      PlayerScore2++; 
     } 

     if (wallID == "topWallTrigger") 
     { 
      HitsTop++; 
     } else if (wallID == "bottomWallTrigger") { 
      HitsBottom++; 
     } 
    } 

    void OnGUI() { 
     GUI.skin = layout; 
     GUI.Label(new Rect(Screen.width/2 - 150 - 12, 20, 100, 100), "" + PlayerScore1 + HitsTop); 
     GUI.Label(new Rect(Screen.width/2 + 150 + 12, 20, 100, 100), "" + PlayerScore2 + HitsBottom); 

     if (GUI.Button(new Rect(Screen.width/2 - 60, 35, 120, 53), "RESTART")) 
     { 
      PlayerScore1 = 0; 
      PlayerScore2 = 0; 
      HitsTop = 0; 
      HitsBottom = 0; 
      theBall.gameObject.SendMessage("RestartGame", 0.5f, SendMessageOptions.RequireReceiver); 
     } 

     if (PlayerScore1 == 10) 
     { 
      GUI.Label(new Rect(Screen.width/2 - 150, 200, 2000, 1000), "PLAYER ONE WINS"); 
      theBall.gameObject.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver); 
     } else if (PlayerScore2 == 10) 
     { 
      GUI.Label(new Rect(Screen.width/2 - 150, 200, 2000, 1000), "PLAYER TWO WINS"); 
      theBall.gameObject.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver); 
     } 
     if (HitsTop == 10) { 
      theBall.gameObject.SendMessage("RestartGame", 1.0f, SendMessageOptions.RequireReceiver); 
      HitsTop = 0; 
     } else if (HitsBottom == 10) { 
      theBall.gameObject.SendMessage("RestartGame", 1.0f, SendMessageOptions.RequireReceiver); 
      HitsBottom = 0; 
     } 
    } 
} 
+0

にタイマーをリセットはい、それは、C#で可能です - いくつかのコードを投稿してください。 –

答えて

1

は、変数をデクリメントするTime.deltaTimeを使用してください。あなたがHitsTopHitsBottomをインクリメントするとき

float timer = 5;  

void Update() 
{ 
    //This will decrement the timer's value by the time. Once this hits zero, the timer is reset to its original value. 
    timer -= Time.deltaTime; 
    if(timer <= 0) 
    { 
    //Call reset game function 
    timer = 5; 
    } 
} 

また、5

+0

このコードは私にとって美しく機能しました。どうもありがとう! :) –

+1

サービスの喜び –

関連する問題