Unity/3D 비행기 게임

[유니티] 3D 비행기 게임 만들기

Skull Crusher 2021. 5. 6. 17:07
728x90

1. Asset 받기

 

2. 해상도 조절

720*1280

 

3. 메인카메라 

Solid Color -> 31,44,56

 

4. 배경 효과

Create Empty -> SpaceStars 하위에 넣기

SpaceShip_Effects.zip
0.26MB

 

5. 스프라이트 추가 -> Prefab 으로 만들기 -> Collider 삭제 -> 엔진효과 추가

Scale 0.1

Rigidbody 추가

 

6. 우주선 이동

C# Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Player 우주선 이동 클래스
public class ShipMovement : MonoBehaviour
{
    public float moveSpeed; //이동 속도

    public Rigidbody rigidbody3d; // 물리 컴포넌트 참조

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //수직 수평 이동 키값
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        //비행기 방향
        Vector3 direction = new Vector3(h, 0f, v);

        rigidbody3d.velocity = direction * moveSpeed;
    }
}

 

7. 비행기 이동제한

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Player 우주선 이동 클래스
public class ShipMovement : MonoBehaviour
{
    public float moveSpeed; //이동 속도

    public Rigidbody rigidbody3d; // 물리 컴포넌트 참조

    //x: 왼쪽제한위치값, y:오른쪽제한위치값, z:위쪽제한위치값, w:아래쪽제한위치값
    public Vector4 boundBox; // 이동 영역

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //수직 수평 이동 키값
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        //비행기 방향
        Vector3 direction = new Vector3(h, 0f, v);

        // 물리엔진 
        rigidbody3d.velocity = direction * moveSpeed;

        // 이동 영역을 제한함
        transform.position = new Vector3(
            Mathf.Clamp(transform.position.x, boundBox.x, boundBox.y), //좌우 제한 영역
            0f,// 0
            Mathf.Clamp(transform.position.z, boundBox.z, boundBox.w)// 상하 제한 영역
        );
    }
}

 

8. z값으로 회전하면서 이동하기

C# Script -> 회전 속도 값 5

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Player 우주선 이동 클래스
public class ShipMovement : MonoBehaviour
{
    public float moveSpeed; //이동 속도

    public Rigidbody rigidbody3d; // 물리 컴포넌트 참조

    //x: 왼쪽제한위치값, y:오른쪽제한위치값, z:위쪽제한위치값, w:아래쪽제한위치값
    public Vector4 boundBox; // 이동 영역

    public float rotSpeed; //회전속도

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //수직 수평 이동 키값
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        //비행기 방향
        Vector3 direction = new Vector3(h, 0f, v);

        // 물리엔진 컴포넌트의 속도를 이용해 우주선을 이동 시킴
        rigidbody3d.velocity = direction * moveSpeed;

        // 이동 영역을 제한함
        transform.position = new Vector3(
            Mathf.Clamp(transform.position.x, boundBox.x, boundBox.y), //좌우 제한 영역
            0f,// 0
            Mathf.Clamp(transform.position.z, boundBox.z, boundBox.w)// 상하 제한 영역
        );

        //오일러 -> 쿼터니언각 사용
        transform.rotation = Quaternion.Euler(0f, 0f, rigidbody3d.velocity.x * -rotSpeed);

    }
}

9. 발포 레이저 만들기

Create Empty -> prefab 으로 만들기 -> 이름 바꾸기 Laser -> Rigidbody 추가 -> Capsule Collider 추가

10. 레이저 광선 추가

레이저 하위에 3D -> Quad -> 90도 회전

11. Materials 생성

Create-> Materials -> fx lazer orange dff 추가 -> Mobile-Particles-additive 하면 섬광 배경 없어짐

Materials.zip
0.01MB

10. 레이저 움직임 추가

C# Script -> DirctionMovement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DirctionMovement : MonoBehaviour
{
    public Vector3 direction; // 이동 방향

    public float moveSpeed; // 이동 속도

    public Rigidbody rigidbody3d; // 물리 엔진 컴포넌트 참보

    // Start is called before the first frame update
    void Start()
    {
        // 지정한 방향으로 이동속도를 설정함
        rigidbody3d.velocity = direction * moveSpeed;
    }
     
}

 

11. 레이저 발포

PlayerShip prefab -> Create Empty -> 이름 바꾸기 SpawnPos -> Position 0,0,5.5

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShipFire : MonoBehaviour
{
    //레이저 프리팹
    public GameObject laserPrefab;

    //발포 위치 참조
    public Transform spawnTransform;

    public float fireDelayTime; // 발포 지연 시간

    private float fireTime; // 발포 시간

   
    // Update is called once per frame
    void Update()
    {
        //왼쪽 컨트롤 키를 누르면 
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            Instantiate(laserPrefab, spawnTransform.position, Quaternion.identity);
        }
    }
}

 

비행기가 회전하면서 이동하고

ctrl을 누르면 레이저를 쏘는 걸 볼 수 있다. 슝~~~ 슝~~~~~~ 슝