連射モードの実装


連射モード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
public float speed;
public AudioClip sound;
// ★追加(連射)
private int count;
// ★変更(連射)
// 「FixedUpdate」に変更すること(重要)
void FixedUpdate()
{
// ★変更(連射)
// 「GetKey」に変更
if (Input.GetKey(KeyCode.Space))
{
// ★追加(連射)
count += 1;
// ★追加(連射)
if (count % 5 == 0)
{
GameObject missile = Instantiate(missilePrefab, transform.position, Quaternion.identity);
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * speed);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
Destroy(missile, 2.0f);
}
}
}
}


【2021版】Danmaku(基礎/全55回)
他のコースを見る

連射モード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
public float speed;
public AudioClip sound;
// ★追加(連射)
private int count;
// ★変更(連射)
// 「FixedUpdate」に変更すること(重要)
void FixedUpdate()
{
// ★変更(連射)
// 「GetKey」に変更
if (Input.GetKey(KeyCode.Space))
{
// ★追加(連射)
count += 1;
// ★追加(連射)
if (count % 5 == 0)
{
GameObject missile = Instantiate(missilePrefab, transform.position, Quaternion.identity);
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * speed);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
Destroy(missile, 2.0f);
}
}
}
}


連射モードの実装