반응형

아래 있는 Effect 유니티 파일을 다운로드합니다 

Effect.unitypackage
0.04MB

 

 

 

임포트 합니다 

 

 

 PlayerRig 오브젝트 자식으로 있는  PlayerShip을 선택하고   Prefabs 폴더 안에 있는 Laser 프리팹을 드래그하여 자식으로 놓고 하나 더 복사하여 이름을 Laser_Right , Laser_Left로 합니다 

그리고 각각의 위치를 비행기 날개에서 발사 되는 위치에 적당히 올려놓습니다 

 

 

 

PlayerControls 스크립트 수정합니다 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering.HighDefinition;

public class PlayerControl : MonoBehaviour
{   
    public float controlSpeed = 10f;
    public float xRange = 12f;
    public float yRange = 10f;
    [SerializeField] GameObject[] lasers;

    public float positionPithcFactor = -2f;
    public float controlPithcfactor = -10f;
    public float positionYawFactor = 2f;

    public float controlRollFactor = -20f;
    [SerializeField] InputAction fire;

    float xThrow;
    float yThrow;
    void Update()
    {
        ProcessTranslation();
        ProcessRotation();
        ProcessFiring();
    }

    void OnEnable()
    {
        fire.Enable();
    }

    private void OnDisable()
    {
        fire.Disable();
    }

    //상하좌우로 움직일때 로컬 로테이션을 준다 
    void ProcessRotation()
    {
        //y축 로컬 움직임 팩터
        float pitchDueToPosition = transform.localPosition.y * positionPithcFactor;
        float pitchdueToControlThrow = yThrow * controlPithcfactor;


        float pitch = pitchDueToPosition + pitchdueToControlThrow;
        //x축 로컬 움직임 팩터
        float yaw = transform.localPosition.x * positionYawFactor;
        float roll = xThrow * controlRollFactor;

        //로컬축으로 로테이션한다
        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
    }
    private void ProcessTranslation()
    {
        //좌우 입력
        xThrow = Input.GetAxis("Horizontal");
        //상하 입력
        yThrow = Input.GetAxis("Vertical");

        //시간에 따라 좌우 움직임을 준다 
        float xoffset = xThrow * Time.deltaTime * controlSpeed;
        float rawXPos = transform.localPosition.x + xoffset;

        //플레이어 좌우 이동 제한
        float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);

        //시간에 따라 상하 움직임을 준다 
        float yoffset = yThrow * Time.deltaTime * controlSpeed;
        float rawYpos = transform.localPosition.y + yoffset;

        //플레이어 상하 이동 제한
        float clampedYPos = Mathf.Clamp(rawYpos, -yRange, yRange);

        //키를 누르면 오브젝트를 상하좌우로 움직이게 한다 
        transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
    }

    

    void ProcessFiring()
    {
    //마우스 오른쪽 버튼을 누르면 미사일(ON)시켜 발사하고 그러지 않을땐 Off 한다
        if (Input.GetButton("Fire1"))
        {
            SetLasersActive(true);
        }
        else
        {
            SetLasersActive(false);
        }
    }
    

   
    //레이져 발사 이팩트를 껐다 켰다 한다
    void SetLasersActive(bool isActive)
    {
        foreach (GameObject laser in lasers)
        {
            var emissionModule = laser.GetComponent<ParticleSystem>().emission;
            emissionModule.enabled = isActive;
        }
    }
  
}

 

Laser_Right , Laser_Left  오브젝트를 드래그 하여 PlayerControls 스크립트 Lasers 위치에 드래그하여 올려놓습니다 

 

 

 

 

게임을 실행시켜서 비행기에 총알이 나가는지 확인합니다 

 

총알이 튕겨져 나갈땐  Lasers 프리팹을 열고 Particle System -> Collision을 열고 Min Kill Speed 값을 올려 봅니다 

 

PlayerShip를 선택하고 Physics -> Box Collider를 선택합니다  

 

 

 

PlayerShip 를 선택하고 Physics -> Rigidbody를 선택합니다  

 

 

Box Collider -> is Trigger 체크하고 Use gravity 체크해제 하고 is Kinematic 체크합니다 

 

 

Collision Handler 스크립트 작성

비행기가 외부 물체와 부딪칠때 게임이 재실행하는 스크립트를 작성합니다 

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


public class CollisionHandler : MonoBehaviour
{
    public float loadDelay = 1f;
    public ParticleSystem crashVFX;



    //외부 물체와 충돌하였을때 
    private void OnTriggerEnter(Collider other)
    {
        StartCrashSequence();
    }

    //crashVFX 이팩트를 실행, PlayerControl실행을 중지한다.그리고 1초후 다음 씬으로 이동한다 
    private void StartCrashSequence()
    {      
        crashVFX.Play();
        GetComponent<PlayerControl>().enabled = false;
        GetComponent<MeshRenderer>().enabled = false;
        GetComponent<BoxCollider>().enabled = false;

        Invoke("ReloadLevel", loadDelay);
    }

    void ReloadLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex);
    }
}

 

 

PlayerShip에 드래그하여 붙입니다 

 

 

PlayerRig 하위 밑으로 Player Explosion VFX 프리팹을 드래그하여 놓고 비행체 중앙에 놓습니다 

 

Collision Handler 스크립트 Crash VFX 공간에 드래그 하여 Player Explosion VFX를 놓습니다 

 

비행기가 비행하는 선로에 큐브를 생성하여 비행기가 부딪칠 수 있도록 합니다 

 

 

File -> Build Settings 들어가서 현재 씬을 드래그하여 Build Settings에 올립니다 

게임을 실행하여 장애물인 큐브에 부딪 칠때  이팩트가 터지고 게임이 재실행되는지 확인합니다 

 

 

반응형

+ Recent posts