fork download
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class JumpTest : MonoBehaviour
  6. {
  7. private const int maxJumpCount = 3;
  8.  
  9. private Counter counter = new Counter(maxJumpCount);
  10.  
  11. [SerializeField]
  12. private Rigidbody2D rigid;
  13. [SerializeField]
  14. private ContactFilter2D contactFilter2D;
  15. // IsNormalAngleをtrue,角度を70~110に設定,レイヤーも設定
  16.  
  17.  
  18. [SerializeField]
  19. private float jumpPower;
  20.  
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. if (Input.GetKeyDown("a"))
  25. {
  26. if (counter.IsConsumable(1))
  27. {
  28. counter.Consume(1);
  29. Jump();
  30. }
  31. }
  32.  
  33. }
  34.  
  35. private void Jump()
  36. {
  37. rigid.velocity = new Vector2(rigid.velocity.x, 0);
  38. rigid.AddForce(new Vector2(0,jumpPower),ForceMode2D.Impulse);
  39. }
  40.  
  41.  
  42. private void OnCollisionEnter2D(Collision2D collision)
  43. {
  44. if (rigid.IsTouching(contactFilter2D))
  45. counter.SetValue(maxJumpCount);
  46. }
  47. }
  48.  
  49.  
  50.  
  51. public class Counter
  52. {
  53. private int _value;
  54.  
  55. public bool IsConsumable(int value)
  56. {
  57. return _value - value >= 0;
  58. }
  59.  
  60. public void Consume(int value)
  61. {
  62. _value = _value - value;
  63. }
  64.  
  65. public int GetValue()
  66. {
  67. return _value;
  68. }
  69. public void SetValue(int value)
  70. {
  71. _value = value;
  72. }
  73.  
  74. public Counter(int value)
  75. {
  76. _value = value;
  77. }
  78. }
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(5,25): error CS0246: The type or namespace name `MonoBehaviour' could not be found. Are you missing an assembly reference?
prog.cs(12,13): error CS0246: The type or namespace name `Rigidbody2D' could not be found. Are you missing an assembly reference?
prog.cs(14,13): error CS0246: The type or namespace name `ContactFilter2D' could not be found. Are you missing an assembly reference?
prog.cs(42,37): error CS0246: The type or namespace name `Collision2D' could not be found. Are you missing an assembly reference?
Compilation failed: 5 error(s), 0 warnings
stdout
Standard output is empty