KNU_SpaceShooter/Assets/02_Scripts/WeaponController.cs
2024-09-12 15:26:12 +09:00

64 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Cinemachine;
using System;
[RequireComponent(typeof(AudioSource))]
public class WeaponController : MonoBehaviour
{
[SerializeField] private GameObject bulletPrefab;
[SerializeField] private Transform firePos;
[SerializeField] private AudioClip fireSfx;
private CinemachineImpulseSource impluse;
private new AudioSource audio;
private MeshRenderer muzzleFlash;
void Start()
{
audio = GetComponent<AudioSource>();
impluse = GetComponent<CinemachineImpulseSource>();
muzzleFlash = firePos.GetComponentInChildren<MeshRenderer>();
muzzleFlash.enabled = false;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
// 총알 생성 : bulletPrefab을 왼쪽 마우스 버튼 클릭할 때 마다 생성
// Instantiate(생성할프리팹, 위치, 각도)
Instantiate(bulletPrefab, firePos.position, firePos.rotation);
// 총소리 사운드 재생
audio.PlayOneShot(fireSfx, 0.8f);
// 타격감 연출
impluse.GenerateImpulse();
StartCoroutine(ShowMuzzleFlash());
}
}
IEnumerator ShowMuzzleFlash()
{
//MuzzleFlash 활성화
muzzleFlash.enabled = true;
// Waiting...
yield return new WaitForSeconds(0.2f);
//MuzzleFlash 비활성화
muzzleFlash.enabled = false;
}
}
/*
Co-routine 코루틴 <> 멀티 스레드
*/