現在Unity5.4.1で3Dのゲームを作っています。
今回の質問内容ですがユニティちゃんからBボタンが押されたら弾幕を表示し、それ以外は弾幕を非表示するというスクリプトを作成しています。
しかし、ゲームを起動するとユニティちゃんから初期状態で弾幕が出てきて、終了するとユニティちゃん(というよりPlayerオブジェクト)が消えてしまいます。```C#
弾幕の処理
using UnityEngine;
using System.Collections;
public class ShockwaveMove : MonoBehaviour
{
public GameObject effect;
//飛んでいく方向
private Vector3 direction;
//飛ばす力
[SerializeField]
private float power;
//Rigidbody
private Rigidbody rigid;//パーティクルシステム
private ParticleSystem particle; // Use this for initialization void Start () { rigid = GetComponent<Rigidbody>(); //自身の子要素からParticleSystemを取得 particle = GetComponentInChildren<ParticleSystem>(); } // Update is called once per frame void Update () { //パーティクルの再生が終わったら衝撃波を消す if (!particle.isPlaying) { Destroy(this.gameObject); } } //衝撃波の飛んでいく方向を設定 public void SetDirection(Vector3 direction) { this.direction = direction; } //衝撃波に力を加えて飛ばす void FixedUpdate() { rigid.AddForce(direction * power); } void OnTriggerEnter(Collider col) { //壁か敵にあたった場合 if (col.tag == "Block" || col.tag == "Enemy") { if (col.tag == "Enemy") { Destroy(effect, 0.5f); } Destroy(effect, 0.5f); } }
}
```C# 弾幕の生成 using UnityEngine; using System.Collections; public class ShockwaveTest : MonoBehaviour { [SerializeField] private GameObject shockwave; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.B)) { Debug.Log("00000"); // 衝撃波プレハブをインスタンス化 var obj = Instantiate(shockwave, transform.position + transform.forward, Quaternion.identity) as GameObject; // 衝撃波の飛んでいく方向を指定 obj.GetComponent<ShockwaveMove>().SetDirection(transform.forward); } } }
C#
1プレイヤーの処理
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Player : MonoBehaviour
{
//プレイヤーの移動スピード
public float moveSpeed = 5f;
//プレイヤーの回転速度
public float rotationSpeed = 360f;
//CharacterControllerをcharacterControllerで宣言 CharacterController characterController; //Animatorをanimatorで宣言 Animator animator; public GameObject effectPrefab; public GameObject Dot; public int PlayerHP; private Slider slider; public float Dot1 = 0f; public float Dot2 = 0f; // Use this for initialization void Start () { //characterControllerという型にCharacterControllerの値をコピーしている。 characterController = GetComponent<CharacterController>(); animator = GetComponentInChildren<Animator>();
/* slider = GameObject.Find("PlayerHPAlider").GetComponent<Slider>();
slider.maxValue = PlayerHP;
slider.value = PlayerHP;*/
}
// Update is called once per frame void Update() { //animatorのanimという型で作成 Animator anim = GetComponent<Animator>(); //Vector3に新しくVector3を作りGetAxis(X軸にHorizontal、Y軸に0、Z軸にVerticalを代入) Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //ベクトルが0.01F以上なら if (direction.sqrMagnitude > 0.01f) { //Vector3のforwardにVector3.Slerp(ゲームオブジェクトが向いている方向、方角、キャラクターのスピード×今フレームと全フレーム÷二つの方向の差分) Vector3 forward = Vector3.Slerp(transform.forward, direction, rotationSpeed * Time.deltaTime / Vector3.Angle(transform.forward, direction)); //描画 transform.LookAt(transform.position + forward); } //characterController内のMoveに(direction * moveSpeed * Time.deltaTime)を代入 characterController.Move(direction * moveSpeed * Time.deltaTime); //アニメーション処理 }
あなたの回答
tips
プレビュー