Unityの参考書をもとにゲームを作成しているのですが、画面をクリック(タップ)した所にイガグリを飛ばす処理が動作せず困っております。
的があり、そこにイガグリをなげて当たるとくっつくというゲームを作成しているのですが、
画面をクリック(タップ)した座標の方向ではなく、どこをタップしても同じ方法にイガグリが飛んで行ってしまいます。
ShootやGeneratorで座標がきちんと取得・渡せているか確認したのですが、座標はクリックした箇所の座標となっており、場所によって変わっておりました。
クリックした箇所にイガグリを飛ばすにはどうしたらいいのでしょうか。
■IgaguriGenerator
using System.Collections; using System.Collections.Generic; using UnityEngine; public class IgaguriGenerator : MonoBehaviour { public GameObject igaguriPrefab; // Update is called once per frame void Update () { if(Input.GetMouseButtonDown(0)){ GameObject igaguri = Instantiate(igaguriPrefab) as GameObject; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Vector3 worldDir = ray.direction; igaguri.GetComponent<IgaguriController>().Shoot(worldDir.normalized * 2000); } } }
■IgaguriController
using System.Collections; using System.Collections.Generic; using UnityEngine; public class IgaguriController : MonoBehaviour { public void Shoot(Vector3 dir){ GetComponent<Rigidbody>().AddForce(dir); } void OnCollisionEnter(Collision other){ GetComponent<Rigidbody>().isKinematic = true; GetComponent<ParticleSystem>().Play(); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。