fork download
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class character_controll : MonoBehaviour
  7. {
  8. Rigidbody2D rb;
  9. [SerializeField] private LayerMask groundLayer;
  10.  
  11. const float maxJumpCountLimit = 3;//初期値
  12. float max_jump_count = maxJumpCountLimit;
  13.  
  14. //ui用フィールド
  15. public GameObject countUi;
  16. void Start()
  17. {
  18. rb = this.GetComponent<Rigidbody2D>();
  19. //初期値のジャンプカウントセット
  20. countUi.GetComponent<Text>().text = max_jump_count.ToString();
  21. }
  22.  
  23. void Update()
  24. {
  25. moveFunction();
  26. if(Input.GetKeyDown(KeyCode.Space))
  27. {
  28. jumpingFunction();
  29. print(max_jump_count);
  30. print(isHitToGround());
  31. }
  32.  
  33. jumpingCountReset();
  34. sendJumpCountToUi(max_jump_count);
  35.  
  36. /*デバッグ用記述*/
  37. if(isHitToGround())
  38. {
  39. Debug.DrawRay(transform.position, Vector2.down * 1f,Color.white, 1f/*drawTime*/);
  40. print(isHitToGround());
  41. print("検知できてる?");
  42. }
  43. }
  44.  
  45. bool isHitToGround()
  46. {
  47. //落下中であればrayを発射する 上昇中にヒットすることを防ぐ
  48. if(Mathf.Sign(rb.velocity.y) == 1)
  49. {
  50. //接地判定用のレイキャスト trueであれば
  51. RaycastHit2D raycastHit = Physics2D.Raycast(transform.position , Vector2.down, 1f/*1fを第二引数に乗算する。第二引数は(0,-1)*/, groundLayer);
  52. return raycastHit.collider != null;
  53.  
  54. }
  55. return false;
  56. }
  57. void moveFunction()
  58. {
  59. //操作方向
  60. float x = Input.GetAxis("Horizontal");
  61. float y = Input.GetAxis("Vertical");
  62. //左右移動
  63. rb.velocity = new Vector2(x * 10 ,rb.velocity.y);
  64. }
  65. void jumpingFunction()
  66. {
  67. switch(max_jump_count)
  68. {
  69. case 3:
  70. if(isHitToGround())
  71. {
  72. rb.AddForce(new Vector2(0,10),ForceMode2D.Impulse);
  73. max_jump_count -= 1;
  74. }
  75. break;
  76. case 2:
  77. //空中にいたら
  78. if(!isHitToGround())
  79. {
  80. rb.velocity = new Vector2(0,0);
  81. rb.AddForce(new Vector2(0,10),ForceMode2D.Impulse);
  82. max_jump_count -= 1;
  83. }
  84. break;
  85. case 1:
  86. //空中にいたら
  87. if(!isHitToGround())
  88. {
  89. rb.velocity = new Vector2(0,0);
  90. rb.AddForce(new Vector2(0,10),ForceMode2D.Impulse);
  91. max_jump_count -= 1;
  92. }
  93. break;
  94. }
  95. }
  96. void jumpingCountReset()
  97. {
  98. if(isHitToGround())
  99. {
  100. max_jump_count = maxJumpCountLimit;
  101. //uiにリセット値をセット
  102. countUi.GetComponent<Text>().text = max_jump_count.ToString();
  103. }
  104. }
  105. void sendJumpCountToUi(float count)
  106. {
  107. var ui_value = countUi.GetComponent<Text>().text;
  108. //もしtextvalueとcountに差があった場合、現在のcountをtextvalueにセットする
  109. if(count != float.Parse(ui_value))
  110. {
  111. countUi.GetComponent<Text>().text = count.ToString();
  112. }
  113. }
  114.  
  115. }
  116.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(3,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(4,7): error CS0246: The type or namespace name `UnityEngine' could not be found. Are you missing an assembly reference?
prog.cs(6,35): error CS0246: The type or namespace name `MonoBehaviour' could not be found. Are you missing an assembly reference?
prog.cs(8,2): error CS0246: The type or namespace name `Rigidbody2D' could not be found. Are you missing an assembly reference?
prog.cs(9,27): error CS0246: The type or namespace name `LayerMask' could not be found. Are you missing an assembly reference?
prog.cs(15,9): error CS0246: The type or namespace name `GameObject' could not be found. Are you missing an assembly reference?
Compilation failed: 6 error(s), 0 warnings
stdout
Standard output is empty