Menu
CoddyTech

שורות עמוסות יותר

שיעור 12 מתוך 20 בקורס משחק Unity: נתיבי שמיים של Coddy.

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

int busy = Mathf.Min(3, 1 + made / 12);
int count = Random.Range(1, busy + 1);

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

List<int> lanes = new List<int> { -1, 0, 1 };
int pick = Random.Range(0, lanes.Count);
int lane = lanes[pick];
lanes.RemoveAt(pick);
challenge icon

אתגר

בינוני
  1. ב־Fill, חשבו את busy ואת count כפי שעשיתם למעלה.
  2. הציבו count מכשולים, כל אחד בנתיב שנבחר מתוך lanes.
  3. ספרו את הקירות; כאשר kind == 2 וכבר יש שניים, בחרו במקום זאת Random.Range(0, 2).

נסו בעצמכם

using UnityEngine;

public class Runner : MonoBehaviour
{
    public float laneWidth = 2.5f;
    public float laneSpeed = 16f;
    public float jumpHeight = 1.6f;
    public float gravity = -40f;
    public float slideTime = 0.7f;

    public int lane;

    private CharacterController controller;
    private GameManager game;
    private float fallSpeed;
    private float slide;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        game = FindFirstObjectByType<GameManager>();
    }

    void Update()
    {
        if (!game.over)
        {
            if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) lane = Mathf.Max(-1, lane - 1);
            if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) lane = Mathf.Min(1, lane + 1);
            bool jump = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space);
            if (jump && controller.isGrounded)
            {
                fallSpeed = Mathf.Sqrt(jumpHeight * -2f * gravity);
                StopSlide();
            }
            if ((Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) && controller.isGrounded)
            {
                slide = slideTime;
                Resize(0.5f);
            }
        }

        if (slide > 0f)
        {
            slide -= Time.deltaTime;
            if (slide <= 0f) StopSlide();
        }

        if (controller.isGrounded && fallSpeed < 0f) fallSpeed = -2f;
        fallSpeed += gravity * Time.deltaTime;

        float targetX = lane * laneWidth;
        float x = Mathf.MoveTowards(transform.position.x, targetX, laneSpeed * Time.deltaTime);
        Vector3 move = new Vector3(x - transform.position.x, fallSpeed * Time.deltaTime, 0f);
        controller.Move(move);
    }

    void StopSlide()
    {
        if (transform.localScale.y < 1f) Resize(1f);
        slide = 0f;
    }

    void Resize(float height)
    {
        controller.enabled = false;
        transform.position += Vector3.up * (height - transform.localScale.y);
        transform.localScale = new Vector3(1f, height, 1f);
        controller.enabled = true;
    }

    void OnTriggerEnter(Collider other)
    {
        if (game.over) return;
        if (other.CompareTag("Obstacle"))
        {
            game.GameOver();
        }
    }
}
quiz iconבחנו את עצמכם

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

כל השיעורים ביחידה משחק Unity: נתיבי שמיים

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