前提・実現したいこと
このスクリプトをつけたオブジェクトとEnemyBulletのタグを持つオブジェクトが接触した場合シーンの読み込み
発生している問題・エラーメッセージ
ゲームオーバーにならない
該当のソースコード
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Move : MonoBehaviour { [SerializeField] void OnTriggerEnter(Collider other) { if(other.gameObject.tag == "EnemyBullet") { SceneManager.LoadScene("GameOver"); } } // Use this for initialization void Start () { } public GameObject BulletPrefab; // Update is called once per frame void Update () { if (Input.GetKey (KeyCode.LeftArrow)) { transform.Translate (-0.2f, 0, 0); } if (Input.GetKey (KeyCode.RightArrow)) { transform.Translate ( 0.2f, 0, 0); } if (Input.GetKey (KeyCode.UpArrow)) { transform.Translate ( 0, 0.2f, 0); } if (Input.GetKey (KeyCode.DownArrow)) { transform.Translate ( 0, -0.2f, 0); } if (Input.GetKeyDown (KeyCode.Space)) { Instantiate (BulletPrefab, transform.position, Quaternion.identity); } // 移動の制限 Clamp(); } void Clamp () { // 画面左下のワールド座標をビューポートから取得 Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); // 画面右上のワールド座標をビューポートから取得 Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); // プレイヤーの座標を取得 Vector2 pos = transform.position; // プレイヤーの位置が画面内に収まるように制限をかける pos.x = Mathf.Clamp (pos.x, min.x, max.x); pos.y = Mathf.Clamp (pos.y, min.y, max.y); // 制限をかけた値をプレイヤーの位置とする transform.position = pos; } }
### 試したこと isTriggerのオンオフ 判定の確認 EnemyBulletのスクリプトからのゲームオーバーの読み込み(失敗)
回答3件
あなたの回答
tips
プレビュー