Menu
CoddyTech

בנו את הדרך

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

עד עכשיו הרץ עמד על בלוק פשוט בשם Ground. מכאן הסקריפט Road בונה את הכביש, והבלוק נעלם.

הכביש מורכב מחלקים באורך 20 יחידות, המונחים בזה אחר זה. חלק הוא GameObject ריק בשם Chunk, וכל מה שנמצא בו הוא צאצא שלו: הרצפה, הקווים המקווקווים בין הנתיבים ושני הקצוות. הזזת החלק תזיז את כולו.

Transform chunk = new GameObject("Chunk").transform;
chunk.position = new Vector3(0f, 0f, i * chunkLength);
Instantiate(floorPrefab, chunk.position + new Vector3(0f, -0.25f, 0f), Quaternion.identity, chunk);

הארגומנט האחרון של Instantiate הוא ההורה. עובי הרצפה הוא חצי יחידה, ולכן האמצע שלה נמצא רבע יחידה מתחת לפני הכביש, בגובה y 0.

כל קו נתיב מורכב מארבעה מקטעים באורך 3, אחד בכל 5 יחידות, במיקומי x -laneWidth / 2f ו־laneWidth / 2f. הקצוות נמצאים במיקומי x -4.1 ו־4.1, ובגובה y 0.05.

challenge icon

אתגר

קל
  1. ב-Road הוסיפו את שדות ה-prefab floorPrefab, stripePrefab ו-edgePrefab (כולם מסוג public GameObject), ואת chunkCount = 7, chunkLength = 20f, laneWidth = 2.5f. החלון Inspector ממלא את ה-prefab.
  2. ב-Start, צרו chunkCount חלקים, כאשר חלק i נמצא ב-z i * chunkLength, ולכל חלק יש רצפה.
  3. בכל חלק, עבור d מ-0 עד 3, מקמו שני קווים מקווקווים ב-z -7.5f + d * 5f, ב-y 0.01, אחד על כל קו נתיב.
  4. מקמו את שתי השפות.

נסו בעצמכם

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 float fallSpeed;
    private float slide;

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

    void Update()
    {
        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;
    }
}

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

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