Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. namespace CompletedAssets
  2. {
  3. public class Player : MonoBehaviour
  4. {
  5. // Spaceshipコンポーネント
  6. Spaceship spaceship;
  7.  
  8. IEnumerator Start ()
  9. {
  10. // Spaceshipコンポーネントを取得
  11. spaceship = GetComponent<Spaceship> ();
  12.  
  13. while (true) {
  14.  
  15. // 弾をプレイヤーと同じ位置/角度で作成
  16. spaceship.Shot (transform);
  17.  
  18. // ショット音を鳴らす
  19. GetComponent<AudioSource> ().Play ();
  20.  
  21. // shotDelay秒待つ
  22. yield return new WaitForSeconds (spaceship.shotDelay);
  23. }
  24. }
  25.  
  26. void Update ()
  27. {
  28. // 右・左
  29. float x = Input.GetAxisRaw ("Horizontal");
  30.  
  31. // 上・下
  32. float y = Input.GetAxisRaw ("Vertical");
  33.  
  34. // 移動する向きを求める
  35. Vector2 direction = new Vector2 (x, y).normalized;
  36.  
  37. // 移動の制限
  38. Move (direction);
  39. Debug.Log("x: " + transform.position.x + "y: " + transform.position.y);
  40.  
  41. }
  42.  
  43. // 機体の移動
  44. void Move (Vector2 direction)
  45. {
  46. // 画面左下のワールド座標をビューポートから取得
  47. Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (-3.7f, -0.8f));//ここのコード
  48.  
  49. // 画面右上のワールド座標をビューポートから取得
  50. Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));
  51.  
  52. // プレイヤーの座標を取得
  53. Vector2 pos = transform.position;
  54.  
  55. // 移動量を加える
  56. pos += direction * spaceship.speed * Time.deltaTime;
  57.  
  58. // プレイヤーの位置が画面内に収まるように制限をかける
  59. pos.x = Mathf.Clamp (pos.x, min.x, max.x);
  60. pos.y = Mathf.Clamp (pos.y, min.y, max.y);
  61.  
  62. // 制限をかけた値をプレイヤーの位置とする
  63. transform.position = pos;
  64. }
  65.  
  66. // ぶつかった瞬間に呼び出される
  67. void OnTriggerEnter2D (Collider2D c)
  68. {
  69. // レイヤー名を取得
  70. string layerName = LayerMask.LayerToName (c.gameObject.layer);
  71.  
  72. // レイヤー名がBullet (Enemy)の時は弾を削除
  73. if (layerName == "Bullet (Enemy)") {
  74. // 弾の削除
  75. Destroy (c.gameObject);
  76. }
  77.  
  78. // レイヤー名がBullet (Enemy)またはEnemyの場合は爆発
  79. if (layerName == "Bullet (Enemy)" || layerName == "Enemy") {
  80. // Managerコンポーネントをシーン内から探して取得し、GameOverメソッドを呼び出す
  81. FindObjectOfType<Manager> ().GameOver ();
  82.  
  83. // 爆発する
  84. spaceship.Explosion ();
  85.  
  86. // プレイヤーを削除
  87. Destroy (gameObject);
  88. }
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement