Menu
CoddyTech

Yolu Hareket Ettir

Coddy'nin Unity Oyunu: Gökyüzü Şeritleri kursunda ders 6 / 20.

Bir koşu oyununda oyuncu yerinde kalır ve dünya ona doğru gelir. Kamerayı ve oyuncuyu sonsuza kadar ileri taşımak da işe yarardı, ancak sayılar durmadan büyürdü; yolu hareket ettirmek her şeyi sıfıra yakın tutar.

Hız tüm oyuna aittir, bu yüzden GameManager içinde bulunur. Road, Start içinde hızı bir kez bulur ve her karede hızını okur:

float step = game.speed * Time.deltaTime;
foreach (Transform chunk in chunks)
{
    chunk.position += Vector3.back * step;
}

Vector3.back, (0, 0, -1) değeridir: kameraya doğru. Parçaları daha sonra hareket ettirmek için Road'un onları List<Transform> içinde hatırlaması gerekir.

challenge icon

Görev

Kolay
  1. GameManager içine public float startSpeed = 10f; ve public float speed; ekle ve Start içinde speed = startSpeed olarak ayarla.
  2. Road içine private List<Transform> chunks = new List<Transform>(); (using System.Collections.Generic; ile birlikte) ve private GameManager game; ekle.
  3. Start içinde game = FindFirstObjectByType<GameManager>(); olarak ayarla ve oluşturulan her parçayı chunks listesine ekle.
  4. Update metodunu yukarıdaki gibi yaz.

Kendin dene

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 Oyunu: Gökyüzü Şeritleri bölümündeki tüm dersler

Kendi başına pratik yap: Unity playground