Menu
CoddyTech

ניצחתם

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

challenge icon

אתגר

בינוני

תן לשחקן לנצח כשמגיעים ל־target נקודות.

  1. ב־Ball.cs, הוסף public int target = 10; ואת bool IsPlaying(), שמחזירה timeLeft > 0f && score < target.
  2. השתמש ב־IsPlaying() במקום ב־timeLeft > 0f בתוך Update() ו־FixedUpdate(), והתחל את AddScore עם if (!IsPlaying()) return;.
  3. אחרי שורת הניקוד, כאשר score >= target, הצג ביומן You win!.

לחץ על ״שחק״ ואסוף 8 נקודות. לאחר מכן לחץ על ״בדיקה״.

נסו בעצמכם

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);
    }
}

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

תרגלו בעצמכם: Unity אונליין