Menu
CoddyTech

道を作る

Coddyの「Unityゲーム:スカイレーン」コースのレッスン 5/20。

これまで、ランナーは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の破線4本で構成され、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 を追加します。インスペクターでプレハブを設定します。
  2. Start で chunkCount 個の区間を作り、各区間を z 座標 i * chunkLength に配置し、それぞれに床を設置します。
  3. 各区間で、d を 0 から 3 まで変化させ、z 座標 -7.5f + d * 5f、y 座標 0.01 に、車線のライン上それぞれ1つずつ、2本の破線を配置します。
  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プレイグラウンド