using UnityEngine;
//Bulletに
public class BulletSc : MonoBehaviour
{
public GameObject explosionPrefab;
public AudioClip explosionSE;
Rigidbody2D rid2d;
private void Awake()
{
rid2d = GetComponent();
}
public void Init(Vector3 dir)
{
transform.rotation = Quaternion.FromToRotation(Vector3.up, dir);
rid2d.linearVelocity = dir * 15f;
}
void Update()
{
Destroy(gameObject, 2);
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.CompareTag("Player"))
{
gameObject.SetActive(false);
AudioSource.PlayClipAtPoint(explosionSE, transform.position);
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
}
}
}
////////////////
//Turretに
using UnityEngine;
public class Turret : MonoBehaviour
{
[SerializeField] private BulletSc bullet;
[SerializeField] private Transform targetPos;
private float timer;
Rigidbody2D rid2d;
void Update()
{
timer += Time.deltaTime;
if (timer > 1.0f)
{
timer = 0f;
Shoot();
}
}
void Shoot()
{
Vector3 dir = (targetPos.position - transform.position).normalized;
BulletSc bulletIns = Instantiate(bullet, transform);
bulletIns.Init(dir);
}
}
////////////
//Turretに
using UnityEngine;
public class Rotate : MonoBehaviour
{
[SerializeField] private Transform target;
[SerializeField]
private float rotationSpeed = 5f; // オブジェクトの回転速度
// public GameObject enemy;
void Start()
{
// enemy.SetActive(false);
}
void Update()
{
if (target)
{
Vector3 direction = target.position - transform.position;
// 角度を求める。
float angle = Mathf.Atan2(direction.x, direction.y);
//オブジェクトをQuaternion.AngleAxisを使って回転させる。
transform.rotation = Quaternion.AngleAxis(angle * Mathf.Rad2Deg - 90, Vector3.back);
}
}
}