質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

0回答

1053閲覧

エフェクトの生成について

14dai

総合スコア15

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/05/09 05:48

Unityの銃弾発射エフェクトを作っているのですが、2回に1回ほどシーンを再生すると正常にエフェクトオブジェクトが生成されない時があります。
このシーン上に関係のあるスクリプトは下記の3つでシーン上では
RotateToMouseスクリプトがSphere
SpawnスクリプトがScene
ProjectileMoveスクリプトがプレフハブ化した問題のエフェクトについています。
プレハブ化したエフェクトにはBoxcolliderもついています。

おそらく、オブジェクトの生成を指示しているSpawnに問題があると思っているのですがどなたか分かりますでしょうか?

イメージ説明

public class RotateToMouse : MonoBehaviour { public Camera cam; public float maximumLenght; private Ray rayMouse; private Vector3 pos; private Vector3 direction; private Quaternion rotation; void Update () { if (cam != null) { RaycastHit hit; var mousePos = Input.mousePosition; rayMouse = cam.ScreenPointToRay (mousePos); if (Physics.Raycast (rayMouse.origin, rayMouse.direction, out hit, maximumLenght)) { RotateToMouseDirection (gameObject, hit.point); } else { var pos = rayMouse.GetPoint (maximumLenght); RotateToMouseDirection (gameObject, pos); } } else { Debug.Log ("No Camera"); } } void RotateToMouseDirection (GameObject obj, Vector3 destination){ direction = destination - obj.transform.position; rotation = Quaternion.LookRotation (direction); obj.transform.localRotation = Quaternion.Lerp (obj.transform.rotation, rotation, 1); } public Quaternion GetRotation (){ return rotation; } }
public class ProjectileMove : MonoBehaviour { public float speed; public float fireRate; public GameObject muzzlePrefab; public GameObject hitPrefab; void Start () { if (muzzlePrefab != null) { var muzzleVFX = Instantiate (muzzlePrefab, transform.position, Quaternion.identity); muzzleVFX.transform.forward = gameObject.transform.forward; var psMuzzle = muzzleVFX.GetComponent<ParticleSystem> (); if (psMuzzle != null) Destroy (muzzleVFX, psMuzzle.main.duration); else { var psChild = muzzleVFX.transform.GetChild (0).GetComponent<ParticleSystem> (); Destroy (muzzleVFX, psChild.main.duration); } } } void Update () { if (speed != 0) { transform.position += transform.forward * (speed * Time.deltaTime); }else{ Debug.Log("No Speed"); } } void OnCollisionEnter (Collision co){ speed = 0; ContactPoint contact = co.contacts [0]; Quaternion rot = Quaternion.FromToRotation (Vector3.up, contact.normal); Vector3 pos = contact.point; if (hitPrefab != null) { var hitVFX = Instantiate (hitPrefab, pos, rot); var psHit = hitVFX.GetComponent<ParticleSystem> (); if (psHit != null) Destroy (hitVFX, psHit.main.duration); else { var psChild = hitVFX.transform.GetChild (0).GetComponent<ParticleSystem> (); Destroy (hitVFX, psChild.main.duration); } } Destroy (gameObject); } }
public class Spawn : MonoBehaviour { public GameObject firePoint; public List<GameObject> vfx = new List<GameObject> (); public RotateToMouse rotateToMouse; private GameObject effectToSpawn; private float timeToFire = 0; void Start () { effectToSpawn = vfx [0]; } void Update () { if(Input.GetMouseButton (0) && Time.time >= timeToFire){ timeToFire = Time.time + 1 / effectToSpawn.GetComponent<ProjectileMove> ().fireRate; SpawnVFX (); } } void SpawnVFX (){ GameObject vfx; if (firePoint != null) { vfx = Instantiate (effectToSpawn, firePoint.transform.position, Quaternion.identity); if (rotateToMouse != null) { vfx.transform.localRotation = rotateToMouse.GetRotation (); } } else { Debug.Log ("No Fire Point"); } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mattrick

2020/05/15 08:24

「正常にエフェクトオブジェクトが生成されない」というのはどういう意味でしょうか。 エフェクトが生成されないのですか? それともエフェクトの表示が異常なのですか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問