


이렇게 원본을 수정하면 일괄 적용된다.


하지만 구분되어 생성하는 것이 중요하다. 원본이 훼손되면 안될 경우가 있기 때문.
이것을 오브젝트에 아까처럼 객체 하나에다가 넣고 일괄 적용하면 원본에도 적용됨을 볼 수 있음.
이것을 통해 생성을 해볼 것이다. Update로.
이런 형식일 것인데
로 생성할 수 있고 엄청나게 많은 수가 생성이 된다.
파괴가 안된다.
파괴하는 시점에서는 반대로 게임 오브젝트를 꺼내서 제거해야 한다.
p를 꺼내오지 않으면 어떤 것을 파괴할지 모르기 때문에 두 줄이 핵심인 셈.
터렛을 예시로 만들어볼 것이다.
프리팹으로까지의 과정을 진행.
탄환까지 프리팹으로 진행.
Bullet을 만들었을 때 무엇이 필요할까?
이렇게 필요할 것이고 적용을 시키면
이제 TurretController로 이동.
하지만 충돌 처리를 배우고 나면 이런 느낌은 아닐 것이다.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurretController : MonoBehaviour
{
// 탄환 소환
// 총구 위치에
[SerializeField] private GameObject _bulletPrefab;
[SerializeField] private Transform _muzzlePoint;
[SerializeField] private float _coolTime;
[SerializeField] private Transform _playerTransform;
[SerializeField] private float _rotateSpeed;
[SerializeField] private float _detectionRange;
//private float _timer;
// 플레이어가 특정 거리 밖에 있을 때 돈다.
// 일정 거리 안이면 플레이어 응시.
private void Update()
{
//_timer += Time.deltaTime;
_coolTime += Time.deltaTime;
float distance = Vector3.Distance(transform.position, _playerTransform.position);
if (distance <= _detectionRange)
{
LookAtPlayer();
if (_coolTime >= 1)
{
SpawnBullet();
_coolTime = 0;
}
}
else
{
RotateTurret();
}
}
private void RotateTurret()
{
transform.Rotate(Vector3.up, _rotateSpeed * Time.deltaTime);
}
private void LookAtPlayer()
{
transform.LookAt(_playerTransform);
}
private void SpawnBullet()
{
// 쿨타임. deltaTime 누적
/*if(_coolTime >= _timer)
return;*/
Instantiate(_bulletPrefab, transform);
/*GameObject bullet = Instantiate(_bulletPrefab);
bullet.transform.position = _muzzlePoint.position;
bullet.transform.rotation = _muzzlePoint.rotation;*/
//_timer = 0;
}
// 격발음
}
지금 격발음이 한 번에 들어와야 하는데 음원 파일이라는 것이 나고 유지되는 시간이 필요하고 하나로만 처리하면 끊기고 소리가 나니 여러개가 있을 필요가 있음.
그럼 소스 코드에서 해보자.using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletController : MonoBehaviour
{
// 앞으로 날아가는 것
[SerializeField] private float _speed;
[SerializeField] private float _destroyTime;
[SerializeField] private AudioSource _audioSource;
[SerializeField] private AudioClip _audioClip;
private void Awake()
{
// 오디오 접근
_audioSource = GetComponent<AudioSource>();
_audioSource.loop = false;
_audioSource.Stop();
_audioSource.clip = _audioClip;
_audioSource.Play();
Destroy(gameObject, _destroyTime);
}
private void Update()
{
MoveForward();
}
private void MoveForward()
{
transform.Translate(Vector3.forward * _speed * Time.deltaTime);
}
// 충돌 체크
}