敵の作成⑥(ミサイルの自動発射)
敵の攻撃
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyFireMissile : MonoBehaviour
{
public GameObject enemyMissilePrefab;
public float speed;
private int count;
// Updateを「FixedUpdate」に変更(ポイント)
private void FixedUpdate()
{
count += 1;
if (count % 30 == 0)
{
GameObject enemyMissile = Instantiate(enemyMissilePrefab, transform.position, Quaternion.identity);
Rigidbody enemyMissileRb = enemyMissile.GetComponent<Rigidbody>();
enemyMissileRb.AddForce(transform.forward * speed);
Destroy(enemyMissile, 3.0f);
}
}
}
自機狙い弾
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyFireMissile : MonoBehaviour
{
public GameObject enemyMissilePrefab;
public float speed;
private int count;
// ★追加
private GameObject target;
// ★追加
private void Start()
{
target = GameObject.Find("Player");
}
private void FixedUpdate()
{
// ★追加
transform.LookAt(target.transform);
count += 1;
if(count % 30 == 0)
{
GameObject enemyMissile = Instantiate(enemyMissilePrefab, transform.position, Quaternion.identity);
Rigidbody enemyMissileRb = enemyMissile.GetComponent<Rigidbody>();
enemyMissileRb.AddForce(transform.forward * speed);
Destroy(enemyMissile, 3.0f);
}
}
}
【2021版】Danmaku(基礎/全55回)
他のコースを見る敵の攻撃
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyFireMissile : MonoBehaviour
{
public GameObject enemyMissilePrefab;
public float speed;
private int count;
// Updateを「FixedUpdate」に変更(ポイント)
private void FixedUpdate()
{
count += 1;
if (count % 30 == 0)
{
GameObject enemyMissile = Instantiate(enemyMissilePrefab, transform.position, Quaternion.identity);
Rigidbody enemyMissileRb = enemyMissile.GetComponent<Rigidbody>();
enemyMissileRb.AddForce(transform.forward * speed);
Destroy(enemyMissile, 3.0f);
}
}
}
自機狙い弾
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyFireMissile : MonoBehaviour
{
public GameObject enemyMissilePrefab;
public float speed;
private int count;
// ★追加
private GameObject target;
// ★追加
private void Start()
{
target = GameObject.Find("Player");
}
private void FixedUpdate()
{
// ★追加
transform.LookAt(target.transform);
count += 1;
if(count % 30 == 0)
{
GameObject enemyMissile = Instantiate(enemyMissilePrefab, transform.position, Quaternion.identity);
Rigidbody enemyMissileRb = enemyMissile.GetComponent<Rigidbody>();
enemyMissileRb.AddForce(transform.forward * speed);
Destroy(enemyMissile, 3.0f);
}
}
}
敵の作成⑥(ミサイルの自動発射)