前提・実現したいこと
Unityで3Dゲームを作成しており、内容は斜め上から俯瞰した簡単な棒避けゲームです。
避ける動作をするオブジェクトをMainPerson、避けるべき棒オブジェクトをBarとしており、
Barは配置したEmptyオブジェクトから一定時間毎にPrefab化したbarオブジェクトを生成してx方向に動く処理となっています。
MainPersonはz軸方向に平行移動のみできます。
棒を5つ避けるたびにカメラをいろんな角度から写すようにしたいのですが、
どうすればよろしいでしょうか。
※カメラはパッと切り替わってしまうと操作側の反応が追いつけないと思いますので、
なるべくヌルッと動かしたいです。
MainPersonScript (避ける側のスクリプト)
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class MainPersonScript : MonoBehaviour 6{ 7 //MainPersonの移動速度 8 public float mainPersonSpeed = 5f; 9 10 // 初期処理 11 void Start() 12 { 13 14 } 15 16 // フレーム処理 17 void Update() 18 { 19 //z軸に平行移動 20 transform.position += new Vector3(0f, 0f, Input.GetAxis("Horizontal")*mainPersonSpeed*Time.deltaTime); 21 } 22}
BarObstScript (棒自身のスクリプト)
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class BarObstScript : MonoBehaviour 7{ 8 // Barの速度 9 public float bar_speed; 10 11 public GameObject MainPersonObject; 12 public MainPersonScript mainPersonScript; 13 14 // 実行時初期処理 15 void Start() 16 { 17 // Barの大きさは範囲内でランダム 18 transform.localScale = new Vector3(1f, 1f, Random.Range(3f, 6f)); 19 20 MainPersonObject = GameObject.Find("MainPerson"); 21 mainPersonScript = MainPersonObject.GetComponent<MainPersonScript>(); 22 } 23 24 // 毎フレーム実行処理 25 void Update() 26 { 27 // x+方向に動く 28 transform.position += new Vector3(bar_speed * Time.deltaTime, 0f, 0f); 29 } 30 31 private void OnCollisionEnter(Collision collision) 32 { 33 //MainPersonに衝突した場合 34 if (collision.gameObject.CompareTag("MainPerson")) 35 { 36 //ゲームオーバー 37 SceneManager.LoadScene("GameOverScene"); 38 } 39 } 40 41 private void OnTriggerEnter(Collider other) 42 { 43 //CubeBackに重なった場合 44 if (other.gameObject.CompareTag("Cube_Back")) 45 { 46 //Barを消す 47 Destroy(gameObject); 48 } 49 } 50}
BarObstReplicScript (棒を複製するスクリプト)
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BarObstReplicScript : MonoBehaviour 6{ 7 // Barオブジェクトと結びつけ 8 public GameObject BarObst; 9 10 // 初期処理 11 void Start() 12 { 13 //一定時間毎に実行する処理 14 InvokeRepeating("SpawnBar", 0f, 1.2f); 15 } 16 17 // フレーム毎処理 18 void Update() 19 { 20 21 } 22 23 void SpawnBar() 24 { 25 //壁(左)の座標を取得 26 float cubeLeftThis = GameObject.Find("Cube_left").transform.position.z; 27 28 //インスタンス生成 29 Instantiate(BarObst, new Vector3(transform.position.x, transform.position.y, Random.Range(cubeLeftThis + 3.0f, 2.0f)), transform.rotation); 30 } 31}
開発環境
macOS Mojave 10.14.4(18E226)
Unity 2018.3.11f1
Visual Studio 7.8.3.2