Menu
CoddyTech

תפוס אותו

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

challenge icon

אתגר

בינוני

תפוס את הכוכב וספור את התפיסות.

  1. הוסף שדה private int score;.
  2. אחרי שהכוכב זז, כאשר Vector3.Distance(transform.position, star.position) < 1f, הוסף 1 ל־score והצג ביומן את "Caught! Score: " + score.
  3. בתנאי ה־if הזה, הזז את הכוכב בחזרה ל־y = 6f, תוך שמירה על ערכי x ו־z שלו.

לחץ על "שחק" והחלק מתחת לכוכב: בחלון Console מופיע Caught! Score: 1. לאחר מכן לחץ על "בדיקה".

נסו בעצמכם

using UnityEngine;

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

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

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

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