Menu
CoddyTech

הפסדתם

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

השלב הזה משלים את מרוץ המטבעות: כדור שנדחף באמצעות כוחות, מטבעות שמשמשים כגורמים מפעילים, ניקוד שמתעדכן בסקריפט אחד ושעון שמסיים את המשחק.

challenge icon

אתגר

בינוני

הודיעו לשחקן כשהזמן אוזל קודם.

  1. ב־Update() של Ball.cs, מצאו את השורה שמדפיסה את Time's up!.
  2. מיד אחריה, הדפיסו את You lose!.

לחצו על "שחק" ותנו לשעון לספור עד הסוף: המסוף מציג Time's up! Score: 0, ואז You lose!. לאחר מכן לחצו על "בדיקה".

נסו בעצמכם

using UnityEngine;

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

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

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

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

    public void AddScore(int points)
    {
        if (!IsPlaying())
        {
            return;
        }
        score += points;
        Debug.Log("Score: " + score);
        if (score >= target)
        {
            Debug.Log("You win!");
        }
    }

    bool IsPlaying()
    {
        return timeLeft > 0f && score < target;
    }
}
quiz iconבחנו את עצמכם

השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.

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

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