ロケットランチャーを作成する






ロケット弾を発射する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotRocket : MonoBehaviour {
public GameObject rocketPrefab;
public AudioClip shotSound;
public float rocketSpeed;
private float timeBetweenShot = 1.2f; // 発射間隔の調整(ロケット弾は連射不可)
private float timer;
void Update () {
timer += Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Space) && timer > timeBetweenShot) {
// ポイント;タイマーの時間を0に戻す。
timer = 0.0f;
// (ポイント)ロケット弾の生成「角度」を「親の角度(向き)」と一致させる。
// この意味を考えてみましょう!
GameObject rocket = (GameObject)Instantiate (rocketPrefab, transform.position, Quaternion.Euler (transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, 0));
Rigidbody rocketRb = rocket.GetComponent<Rigidbody> ();
rocketRb.AddForce (transform.forward * rocketSpeed);
AudioSource.PlayClipAtPoint (shotSound, Camera.main.transform.position);
Destroy (rocket, 3.0f);
}
}
}




EscapeCombat(メモ)
他のコースを見る





ロケット弾を発射する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShotRocket : MonoBehaviour {
public GameObject rocketPrefab;
public AudioClip shotSound;
public float rocketSpeed;
private float timeBetweenShot = 1.2f; // 発射間隔の調整(ロケット弾は連射不可)
private float timer;
void Update () {
timer += Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Space) && timer > timeBetweenShot) {
// ポイント;タイマーの時間を0に戻す。
timer = 0.0f;
// (ポイント)ロケット弾の生成「角度」を「親の角度(向き)」と一致させる。
// この意味を考えてみましょう!
GameObject rocket = (GameObject)Instantiate (rocketPrefab, transform.position, Quaternion.Euler (transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, 0));
Rigidbody rocketRb = rocket.GetComponent<Rigidbody> ();
rocketRb.AddForce (transform.forward * rocketSpeed);
AudioSource.PlayClipAtPoint (shotSound, Camera.main.transform.position);
Destroy (rocket, 3.0f);
}
}
}




ロケットランチャーを作成する