Menu
CoddyTech

Düş ve Yeniden Doğ

Coddy'nin Unity Journey'sinin Gerçek Oyunlar Geliştirmek bölümünün bir parçası. Ders 52 / 60.

challenge icon

Görev

Orta

Düşüş, oyuncuyu geri getirmeli.

  1. Player içinde [SerializeField] private float fallY = -5f; ve bir yeniden doğma noktası ekle; bu noktayı Start() içinde başlangıç konumuna ayarla.
  2. Yeniden doğma noktasını ayarlayan public void SetCheckpoint(Vector3 point) metodunu ekle.
  3. fallY kontrolünün altına şunları ekle: controller.enabled = false ayarla, yeniden doğma noktasına git, kontrolcüyü yeniden etkinleştir ve velocity değerini sıfırla.

“Oyna” düğmesine bas ve aşağı atla: platforma geri dönersin. Ardından “Kontrol et” düğmesine bas.

Kendin dene

using UnityEngine;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour
{
    [SerializeField] private float speed = 5f;
    [SerializeField] private float jumpHeight = 1.5f;
    [SerializeField] private float gravity = -9.81f;

    private CharacterController controller;
    private InputAction moveAction;
    private InputAction jumpAction;
    private Vector3 velocity;
    private MovingPlatform platform;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        moveAction = InputSystem.actions.FindAction("Move");
        jumpAction = InputSystem.actions.FindAction("Jump");
    }

    void Update()
    {
        if (controller.isGrounded && velocity.y < 0f)
        {
            velocity.y = -2f;
        }

        Vector2 input = moveAction.ReadValue<Vector2>();
        Vector3 move = new Vector3(input.x, 0f, input.y) * speed;

        if (jumpAction.WasPressedThisFrame() && controller.isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;
        Vector3 motion = (move + velocity) * Time.deltaTime;
        if (platform != null)
        {
            motion += platform.delta;
            platform = null;
        }
        controller.Move(motion);
    }

    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (hit.normal.y > 0.5f && hit.gameObject.TryGetComponent(out MovingPlatform moving))
        {
            platform = moving;
        }
    }
}

Gerçek Oyunlar Geliştirmek bölümündeki tüm dersler

Kendi başına pratik yap: Unity playground