Menu
CoddyTech

도로 만들기

Coddy의 Unity 게임: 스카이 레인 코스 레슨. 20개 중 5번째.

지금까지 러너는 Ground라는 이름의 평평한 블록 위에 서 있었습니다. 이제부터는 Road 스크립트가 도로를 만들고, 블록은 사라집니다.

도로는 길이가 20단위인 조각들로 이루어져 있으며, 조각들이 차례로 이어집니다. 조각은 Chunk라는 이름의 빈 GameObject이며, 그 조각에 있는 모든 것은 자식입니다. 바닥, 차선 사이의 점선, 양쪽 가장자리가 여기에 포함됩니다. 조각을 움직이면 그 안의 모든 것이 함께 움직입니다.

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의 마지막 인수는 부모입니다. 바닥의 두께는 0.5단위이므로, 바닥의 가운데는 도로 표면인 y 0보다 0.25 아래에 놓입니다.

각 차선 표시는 길이가 3인 점선 네 개로 이루어져 있으며, 5단위 간격으로 하나씩 배치됩니다. x 좌표는 -laneWidth / 2f와 laneWidth / 2f입니다. 가장자리는 x -4.1과 4.1, y 0.05에 놓입니다.

challenge icon

챌린지

쉬움
  1. Road에 프리팹 필드 floorPrefab, stripePrefab, edgePrefab(모두 public GameObject)와 chunkCount = 7, chunkLength = 20f, laneWidth = 2.5f를 추가하세요. Inspector에서 프리팹을 지정합니다.
  2. Start에서 chunkCount개의 조각을 만들고, 각 조각은 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 플레이그라운드