Menu
CoddyTech

כוכב אחר כוכב

חלק מהיחידה יסודות הסקריפטים ב-Unity במסלול ה-Unity של Coddy. שיעור 53 מתוך 85.

challenge icon

אתגר

בינוני

שלח כוכב חדש אחרי כל תפיסה ואחרי כל החטאה.

  1. כתוב void ResetStar() שמגדירה את star.position = new Vector3(Random.Range(-4f, 4f), 6f, 0f);, וקרא לה בתפיסה במקום השורה הישנה.
  2. אחרי ה־if של התפיסה, הוסף else if (star.position.y < 0f) שקורא ל־ResetStar();. בלוק else if מתבצע רק כשה־if שלפניו היה שקרי.

לחץ על "שחק": כל כוכב חדש נופל במיקום x חדש. ואז לחץ על "בדיקה".

נסו בעצמכם

using UnityEngine;

public class Catcher : MonoBehaviour
{
    [SerializeField] private float speed = 6f;
    [SerializeField] private Transform star;
    [SerializeField] private float fallSpeed = 3f;
    private int score;

    void Update()
    {
        float x = transform.position.x;
        if (Input.GetKey(KeyCode.RightArrow))
        {
            x += speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            x -= speed * Time.deltaTime;
        }
        x = Mathf.Clamp(x, -4f, 4f);
        transform.position = new Vector3(x, transform.position.y, transform.position.z);

        star.position += Vector3.down * fallSpeed * Time.deltaTime;

        if (Vector3.Distance(transform.position, star.position) < 1f)
        {
            score++;
            Debug.Log("Caught! Score: " + score);
            star.position = new Vector3(star.position.x, 6f, star.position.z);
        }
    }
}

כל השיעורים ביחידה יסודות הסקריפטים ב-Unity

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