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. Debug.Log(max_jump_count);
  30. }
  31.  
  32. jumpingCountReset();
  33. sendJumpCountToUi(max_jump_count);
  34.  
  35. /*デバッグ用記述*/
  36. Debug.DrawRay(transform.position, Vector2.down * 1f,Color.white, 1f/*drawTime*/);
  37.  
  38.  
  39. }
  40.  
  41. bool isHitToGround()
  42. {
  43. //接地判定用のレイキャスト
  44. RaycastHit2D raycastHit = Physics2D.Raycast(transform.position , Vector2.down, 1f/*1fを第二引数に乗算する。第二引数は(0,-1)*/, groundLayer);
  45. return raycastHit.collider != null;
  46. }
  47. void moveFunction()
  48. {
  49. //操作方向
  50. float x = Input.GetAxis("Horizontal");
  51. float y = Input.GetAxis("Vertical");
  52. //左右移動
  53. rb.velocity = new Vector2(x * 10 ,rb.velocity.y);
  54. }
  55. void jumpingFunction()
  56. {
  57. switch(max_jump_count)
  58. {
  59. case 3:
  60. if(isHitToGround())
  61. {
  62. rb.AddForce(new Vector2(0,10),ForceMode2D.Impulse);
  63. max_jump_count -= 1;
  64. }
  65. break;
  66. case 2:
  67. //空中にいたら
  68. if(!isHitToGround())
  69. {
  70. rb.velocity = new Vector2(0,0);
  71. rb.AddForce(new Vector2(0,10),ForceMode2D.Impulse);
  72. max_jump_count -= 1;
  73. }
  74. break;
  75. case 1:
  76. //空中にいたら
  77. if(!isHitToGround())
  78. {
  79. rb.velocity = new Vector2(0,0);
  80. rb.AddForce(new Vector2(0,10),ForceMode2D.Impulse);
  81. max_jump_count -= 1;
  82. }
  83. break;
  84. }
  85. }
  86. void jumpingCountReset()
  87. {
  88. if(isHitToGround())
  89. {
  90. max_jump_count = maxJumpCountLimit;
  91. //print(max_jump_count+"reset");
  92. //uiにリセット値をセット
  93. countUi.GetComponent<Text>().text = max_jump_count.ToString();
  94. }
  95. }
  96. void sendJumpCountToUi(float count)
  97. {
  98. var ui_value = countUi.GetComponent<Text>().text;
  99. //もしtextvalueとcountに差があった場合、現在のcountをtextvalueにセットする
  100. if(count != float.Parse(ui_value))
  101. {
  102. countUi.GetComponent<Text>().text = count.ToString();
  103. }
  104. }
  105.  
  106. }
  107.  
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