アイテムの作成④(ミサイル速度アップ)


ミサイルの飛行速度をあげる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
// ★変更(privateに変更)
// 初期の速度は遅くすること
private float speed = 300;
public AudioClip sound;
private int count;
void FixedUpdate()
{
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);
}
}
}
// ★追加
public void AddShotSpeed(int amount)
{
speed += amount;
// 上限の設定(上限をどこにするかは自由)
if(speed > 1000)
{
speed = 1000;
}
}
}

ミサイルの速度アップアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotSpeedUp : ItemBase
{
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
ItemGet();
// Playerの子オブジェクトのコンポーネントを取得する(ポイント)
// 1個のアイテムゲットでどれだけ速度を上げるかは自由
other.GetComponentInChildren<FireMissile>().AddShotSpeed(300);
}
}
}



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

ミサイルの飛行速度をあげる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
// ★変更(privateに変更)
// 初期の速度は遅くすること
private float speed = 300;
public AudioClip sound;
private int count;
void FixedUpdate()
{
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);
}
}
}
// ★追加
public void AddShotSpeed(int amount)
{
speed += amount;
// 上限の設定(上限をどこにするかは自由)
if(speed > 1000)
{
speed = 1000;
}
}
}

ミサイルの速度アップアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotSpeedUp : ItemBase
{
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
ItemGet();
// Playerの子オブジェクトのコンポーネントを取得する(ポイント)
// 1個のアイテムゲットでどれだけ速度を上げるかは自由
other.GetComponentInChildren<FireMissile>().AddShotSpeed(300);
}
}
}



アイテムの作成④(ミサイル速度アップ)