Menu
CoddyTech

時間内にクリア

CoddyのUnityジャーニー「物理演算とゲームプレイ」セクションの一部。レッスン 32/71。

challenge icon

チャレンジ

中級

Coin Rushに時計を追加しましょう。

  1. Ball.csにpublic float timeLimit = 60f;とpublic float timeLeft;を追加し、Start()でtimeLeft = timeLimit;を設定します。
  2. 新しいUpdate()で、timeLeft > 0fの間、Time.deltaTimeを差し引きます。0になったら、0fを設定して、"Time's up! Score: " + scoreをログに出力します。
  3. FixedUpdate()では、timeLeft > 0fの間だけ力を加えます。

「プレイ」を押して、時計を確認しましょう。その後、「確認」を押します。

自分で試してみよう

using UnityEngine;

public class Ball : MonoBehaviour
{
    public float force = 10f;
    public int score;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        rb.AddForce(new Vector3(x, 0f, z) * force);
    }

    public void AddScore(int points)
    {
        score += points;
        Debug.Log("Score: " + score);
    }
}

物理演算とゲームプレイのすべてのレッスン

自分で練習してみよう: Unityプレイグラウンド