Guest User

Untitled

a guest
Feb 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI; // これを忘れないこと!
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8. public float speed = 20; // 動く速さ
  9. public Text scoreText; // スコアの UI
  10. public Text winText; // リザルトの UI
  11.  
  12. private Rigidbody rb; // Rididbody
  13. private int score; // スコア
  14.  
  15. void Start()
  16. {
  17. // Rigidbody を取得
  18. rb = GetComponent<Rigidbody>();
  19.  
  20. // UI を初期化
  21. score = 0;
  22. SetCountText();
  23. winText.text = "";
  24. }
  25.  
  26. void Update()
  27. {
  28. // カーソルキーの入力を取得
  29. var moveHorizontal = Input.GetAxis("Horizontal");
  30. var moveVertical = Input.GetAxis("Vertical");
  31.  
  32. // カーソルキーの入力に合わせて移動方向を設定
  33. var movement = new Vector3(moveHorizontal, 0, moveVertical);
  34.  
  35. // Ridigbody に力を与えて玉を動かす
  36. rb.AddForce(movement * speed);
  37. }
  38.  
  39. // 玉が他のオブジェクトにぶつかった時に呼び出される
  40. void OnTriggerEnter(Collider other)
  41. {
  42. // ぶつかったオブジェクトが収集アイテムだった場合
  43. if (other.gameObject.CompareTag("Item"))
  44. {
  45. // その収集アイテムを非表示にします
  46. other.gameObject.SetActive(false);
  47.  
  48. // スコアを加算します
  49. score = score + 1;
  50.  
  51. // UI の表示を更新します
  52. SetCountText();
  53. }
  54. }
  55.  
  56. // UI の表示を更新する
  57. void SetCountText()
  58. {
  59. // スコアの表示を更新
  60. scoreText.text = "Count: " + score.ToString();
  61.  
  62. // すべての収集アイテムを獲得した場合
  63. if (score >= 10)
  64. {
  65. // リザルトの表示を更新
  66. winText.text = "You Win!";
  67. }
  68. }
  69. }
Add Comment
Please, Sign In to add comment