Код IT
← Каталог

Разработка на Unity — Игровой цикл

Фрагмент из «Разработка на Unity»: Игровой цикл.

csharp spinoffencyclopedia9-04-razrabotka-igr-3 embed URL статья в энциклопедии
C# main.cs
    using UnityEngine;

    public class PlayerController : MonoBehaviour
    {
        [SerializeField] private GameObject applePrefab;
        [SerializeField] private float speed = 5f;

        void Update()
        {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
            Vector3 move = new Vector3(h, 0f, v) * speed * Time.deltaTime;
            transform.Translate(move, Space.World);

            if (Input.GetKeyDown(KeyCode.Space) && applePrefab != null)
            {
                Instantiate(applePrefab, transform.position, Quaternion.identity);
            }
        }
    }
    using UnityEngine;

    public class PlayerController : MonoBehaviour
    {
        [SerializeField] private GameObject applePrefab;
        [SerializeField] private float speed = 5f;

        void Update()
        {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");
            Vector3 move = new Vector3(h, 0f, v) * speed * Time.deltaTime;
            transform.Translate(move, Space.World);

            if (Input.GetKeyDown(KeyCode.Space) && applePrefab != null)
            {
                Instantiate(applePrefab, transform.position, Quaternion.identity);
            }
        }
    }