이동 및 회전 이어서, 프리펩, 이동 및 회전 실습
발사체 생성
예제
발사기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LauncherControl : MonoBehaviour
{
[SerializeField] GameObject _prefabMissileObj;
Transform _lFirePos;
Transform _rFirePos;
void Awake()
{
_rFirePos = transform.GetChild(1).GetChild(0).GetChild(0);
_lFirePos = transform.GetChild(1).GetChild(1).GetChild(0);
}
void Update()
{
if (Input.GetButtonDown("Jump")) //Spacebar
{
Instantiate(_prefabMissileObj, _rFirePos.position, _rFirePos.rotation);
Instantiate(_prefabMissileObj, _lFirePos.position, _lFirePos.rotation);
}
}
}
발사체
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MissileObj : MonoBehaviour
{
[SerializeField] float _force = 10;
[SerializeField] float _limitDist = 30;
[SerializeField] float _destroyTime = 5;
Rigidbody _rgdb3d;
Vector3 _spawnPosition;
float _checkTime;
void Awake()
{
_rgdb3d = GetComponent<Rigidbody>();
_rgdb3d.AddForce(transform.forward * _force);
_spawnPosition = transform.position;
//_checkTime = _destroyTime;
//_checkTime = Time.time;
Destroy(gameObject, _destroyTime);
}
//void LateUpdate()
//{
// //Debug.Log(Vector3.Distance(_spawnPosition, transform.position));
// //if (Vector3.Distance(_spawnPosition, transform.position) >= _limitDist)
// //{
// // Destroy(gameObject);
// //}
// //_checkTime -= Time.deltaTime;
// //if (_checkTime <= 0)
// //{
// // Destroy(gameObject);
// //}
// if(_checkTime + _destroyTime <= Time.time)
// {
// Destroy(gameObject);
// }
//}
}
과제 : 발사 기능에 딜레이 넣기
개인과제 : 캐넌 완성