Skip to content

Instantly share code, notes, and snippets.

@IndieGameMaker
Created March 13, 2021 07:08
Show Gist options
  • Save IndieGameMaker/bd769fbccf665bbafd59a61e9ef4ae38 to your computer and use it in GitHub Desktop.
Save IndieGameMaker/bd769fbccf665bbafd59a61e9ef4ae38 to your computer and use it in GitHub Desktop.
[스크립트 9 3] FireCtrl - OnDamage 함수 호출
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//반드시 필요한 컴포넌트를 명시해 해당 컴포넌트가 삭제되는 것을 방지하는 어트리뷰트
[RequireComponent(typeof(AudioSource))]
public class FireCtrl : MonoBehaviour
{
//총알 프리팹
public GameObject bullet;
//총알 발사 좌표
public Transform firePos;
//총소리에 사용할 오디오 음원
public AudioClip fireSfx;
//AudioSource 컴포넌트를 저장할 변수
private new AudioSource audio;
//Muzzle Flash의 MeshRenderer 컴포넌트
private MeshRenderer muzzleFlash;
//Raycast 결괏값을 저장하기 위한 구조체 선언
private RaycastHit hit;
void Start ()
{
audio = GetComponent<AudioSource>();
//FirePos 하위에 있는 MuzzleFlash의 Material 컴포넌트를 추출
muzzleFlash = firePos.GetComponentInChildren<MeshRenderer>();
//처음 시작할 때 비활성화
muzzleFlash.enabled = false;
}
void Update ()
{
//Ray를 시각적으로 표시하기 위해 사용
Debug.DrawRay(firePos.position, firePos.forward * 10.0f, Color.green);
//마우스 왼쪽 버튼을 클릭했을 때 Fire 함수 호출
if (Input.GetMouseButtonDown(0))
{
Fire();
//Ray를 발사
if (Physics.Raycast(firePos.position, //광선의 발사 원점
firePos.forward, //광선의 발사 방향
out hit, //광선에 맞은 결과 데이터
10.0f, //광선의 거리
1 << 6)) //감지하는 범위인 레이어 마스크
{
Debug.Log($"Hit={hit.transform.name}");
hit.transform.GetComponent<MonsterCtrl>()?.OnDamage(hit.point,
hit.normal);
}
}
}
void Fire()
{
//Bullet 프리팹을 동적으로 생성
//Instantiate(bullet, firePos.position, firePos.rotation);
//총소리 발생
audio.PlayOneShot(fireSfx, 1.0f);
//총구화염 효과 코루틴 함수 호출
StartCoroutine(ShowMuzzleFlash());
}
IEnumerator ShowMuzzleFlash()
{
//오프셋 좌표값을 랜덤함수로 생성
Vector2 offset = new Vector2(Random.Range(0, 2), Random.Range(0, 2)) * 0.5f;
//텍스처의 오프셋 값 설정
muzzleFlash.material.mainTextureOffset = offset;
//MuzzleFlash의 회전 변경
float angle = Random.Range(0, 360);
muzzleFlash.transform.localRotation = Quaternion.Euler(0, 0, angle);
//MuzzleFlash의 크기 조절
float scale = Random.Range(1.0f, 2.0f);
muzzleFlash.transform.localScale = Vector3.one * scale;
//MuzzleFlash 활성화
muzzleFlash.enabled = true;
//0.2초 동안 대기(정지)하는 동안 메시지 루프로 제어권을 양보
yield return new WaitForSeconds(0.2f);
//MuzzleFlash 비활성화
muzzleFlash.enabled = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment