티스토리 뷰

아래는 유니티로 간단한 비행기 게임을 만들기 위한 예시 소스 코드입니다. 이 코드는 비행기를 조종하여 적을 피하고 점수를 획득하는 게임을 구현합니다.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5f;
    public float tiltSpeed = 5f;
    public float xRange = 5f;
    public float yRange = 3f;

    private Rigidbody2D rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        float moveX = Input.GetAxis("Horizontal");
        float moveY = Input.GetAxis("Vertical");

        Vector2 movement = new Vector2(moveX, moveY);
        rb.velocity = movement * speed;

        float tiltAngle = -moveX * tiltSpeed;
        Quaternion targetRotation = Quaternion.Euler(0f, 0f, tiltAngle);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * tiltSpeed);

        float clampedX = Mathf.Clamp(transform.position.x, -xRange, xRange);
        float clampedY = Mathf.Clamp(transform.position.y, -yRange, yRange);
        transform.position = new Vector3(clampedX, clampedY, transform.position.z);
    }
}

public class EnemyController : MonoBehaviour
{
    public float speed = 3f;
    public float rotationSpeed = 200f;

    private void Start()
    {
        // Randomly set initial rotation
        transform.rotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
    }

    private void Update()
    {
        transform.Translate(Vector3.down * speed * Time.deltaTime, Space.World);
        transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            Destroy(gameObject);
            // Perform game over logic
        }
    }
}

public class ScoreManager : MonoBehaviour
{
    public int score = 0;
    public TMPro.TextMeshProUGUI scoreText;

    private void Start()
    {
        UpdateScoreText();
    }

    public void AddScore(int value)
    {
        score += value;
        UpdateScoreText();
    }

    private void UpdateScoreText()
    {
        scoreText.text = "Score: " + score.ToString();
    }
}

public class GameManager : MonoBehaviour
{
    public GameObject enemyPrefab;
    public Transform spawnPoint;
    public float spawnInterval = 2f;

    private void Start()
    {
        InvokeRepeating("SpawnEnemy", spawnInterval, spawnInterval);
    }

    private void SpawnEnemy()
    {
        Instantiate(enemyPrefab, spawnPoint.position, Quaternion.identity);
    }
}



위의 코드는 비행기 게임을 구현하기 위해 사용되는 몇 가지 스크립트들을 포함하고 있습니다. 

- `PlayerController` 스크립트는 플레이어 비행기를 제어하며, 사용자 입력에 따라 비행기의 움직임과 기울기를 조정합니다.
- `EnemyController` 스크립트는 적 비행기를 제어하며, 일정한 속도로 아래로 이동하고 회전합니다. 플레

이어와 충돌할 경우 게임 오버 로직을 수행합니다.
- `ScoreManager` 스크립트는 점수를 관리하고, 적을 파괴할 때마다 점수를 증가시킵니다.
- `GameManager` 스크립트는 적 비행기를 일정 간격으로 생성합니다.

이 외에도 게임 화면을 구성하는 UI, 배경, 사운드 등을 추가로 구현해야 합니다. 이 예시 코드는 게임의 핵심 로직을 제공하며, 게임을 확장하고 보완하기 위해서는 추가적인 기능과 콘텐츠를 구현해야 합니다.

댓글
최근에 달린 댓글
글 보관함
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글