Menu
CoddyTech

נצחו את השעון

חלק מהיחידה פיזיקה ומשחקיות במסלול ה-Unity של Coddy. שיעור 32 מתוך 71.

challenge icon

אתגר

בינוני

הוסף שעון ל־Coin Rush.

  1. ב־Ball.cs, הוסף public float timeLimit = 60f; ו־public float timeLeft;; הגדר timeLeft = timeLimit; בתוך Start().
  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 אונליין