Menu
CoddyTech

Springen, rutschen oder ausweichen

Lektion 10 von 20 im Kurs Unity-Spiel: Himmelsbahnen von Coddy.

Eine Art Hindernis bedeutet eine Antwort: springen. Drei Arten stellen drei Fragen:

eine rote Barriere auf der Straße, y 0.5: spring darüber; eine gelbe Stange, die auf y 1.95 hängt: rutsch darunter hindurch; eine violette Wand, y 1.5 und drei Einheiten hoch: wechsle die Spur.

Zwei kleine Methoden wandeln eine Typnummer in ein Prefab und eine Höhe um:

GameObject Obstacle(int kind)
{
    if (kind == 0) return barrierPrefab;
    if (kind == 1) return barPrefab;
    return wallPrefab;
}
challenge icon

Aufgabe

Einfach
  1. Füge barPrefab und wallPrefab zu Road hinzu.
  2. Schreibe Obstacle(kind) wie oben und Height(kind): 0.5 für kind 0, 1.95 für kind 1, 1.5 für die Wand.
  3. Wähle in Fill int kind = Random.Range(0, 3); aus und platziere Obstacle(kind) bei Height(kind).

Probier es selbst

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;
    }
}

Alle Lektionen in Unity-Spiel: Himmelsbahnen

Übe selbstständig: Unity-Playground