Menu
CoddyTech

ניצחון או הפסד

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

challenge icon

אתגר

בינוני

השלב האחרון: דרך לנצח. השאר את כל מה שהוספת בשלבים הקודמים, והוסף ל־Ball:

  • public int target = 10; (הערך ב־Inspector הוא 8).
  • המשחק פעיל כל עוד timeLeft גדול מ־0 ו־score קטן מ־target. כתוב זאת כשיטה, bool IsPlaying(), והשתמש בה: השעון סופר לאחור, החצים דוחפים והמטבעות מוסיפים לניקוד רק כשהמשחק פעיל.
  • כשמטבע מעלה את score ל־target או יותר, כתוב ביומן You win! אחרי שורת הניקוד.
  • כשהזמן נגמר קודם, כתוב ביומן You lose! אחרי השורה Time's up!.

לחץ על Play והשג 8 נקודות לפני שהזמן נגמר, ואז לחץ על Check.

נסו בעצמכם

using UnityEngine;

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

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

    void Update()
    {
        if (timeLeft > 0f)
        {
            timeLeft -= Time.deltaTime;
            if (timeLeft <= 0f)
            {
                timeLeft = 0f;
                Debug.Log("Time's up! Score: " + score);
            }
        }
    }

    void FixedUpdate()
    {
        if (timeLeft > 0f)
        {
            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);
    }
}

כל השיעורים ביחידה פיזיקה ומשחקיות