fork download
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Ball : MonoBehaviour
  6. {
  7. private float speed = 15;
  8. public string winner = "";
  9. protected int playerOneScore, playerTwoScore = 0;
  10.  
  11. /// <summary>
  12. /// responsible for calc
  13. /// the ball hit dir
  14. /// </summary>
  15. /// <returns>the ball position</returns>
  16. protected float hitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)
  17. {
  18. return (ballPos.y - racketPos.y) / racketHeight;
  19. }
  20.  
  21. /// <summary>
  22. /// responsible for ball reaction
  23. /// depending on the gameobj hit
  24. /// </summary>
  25. protected void BallReaction(Collision2D col, int x)
  26. {
  27. float y = hitFactor(transform.position,
  28. col.transform.position,
  29. col.collider.bounds.size.y);
  30. Vector2 dir = new Vector2(x, y).normalized;
  31. GetComponent<Rigidbody2D>().velocity = dir * speed;
  32. }
  33.  
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. /// <param name="col"></param>
  38. /// <param name="winner"></param>
  39. /// <param name="side"></param>
  40. protected void WinAction(Collision2D col, string winner, Vector2 side)
  41. {
  42. Debug.Log("The goal is scored by " + winner);
  43. Debug.Log(playerOneScore + ":" + playerTwoScore);
  44. GetComponent<Rigidbody2D>().velocity = side * speed;
  45. }
  46.  
  47. // Start is called before the first frame update
  48. void Start()
  49. {
  50. GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
  51. }
  52.  
  53. private void Update()
  54. {
  55. }
  56.  
  57. /// <summary>
  58. /// Changes the pos of the ball
  59. /// when hit the racket
  60. /// </summary>
  61. void OnCollisionEnter2D(Collision2D col)
  62. {
  63. string objName = col.gameObject.name;
  64.  
  65. switch (objName)
  66. {
  67. case "Racket_Left":
  68. BallReaction(col, 1);
  69. break;
  70. case "Racket_Right":
  71. BallReaction(col, -1);
  72. break;
  73. case "Wall_Left":
  74. winner = "Player 2";
  75. playerTwoScore++;
  76. Vector2 right = Vector2.right;
  77. WinAction(col, winner, right);
  78. break;
  79. case "Wall_Right":
  80. winner = "Player 1";
  81. playerOneScore++;
  82. Vector2 left = Vector2.left;
  83. WinAction(col, winner, left);
  84. break;
  85.  
  86. default: break;
  87.  
  88. }
  89. }
  90. }
  91.  
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,21): error CS0246: The type or namespace name `MonoBehaviour' could not be found. Are you missing an assembly reference?
prog.cs(16,31): error CS0246: The type or namespace name `Vector2' could not be found. Are you missing `System.Numerics' using directive?
prog.cs(16,48): error CS0246: The type or namespace name `Vector2' could not be found. Are you missing `System.Numerics' using directive?
prog.cs(25,33): error CS0246: The type or namespace name `Collision2D' could not be found. Are you missing an assembly reference?
prog.cs(40,30): error CS0246: The type or namespace name `Collision2D' could not be found. Are you missing an assembly reference?
prog.cs(40,62): error CS0246: The type or namespace name `Vector2' could not be found. Are you missing `System.Numerics' using directive?
prog.cs(61,29): error CS0246: The type or namespace name `Collision2D' could not be found. Are you missing an assembly reference?
Compilation failed: 8 error(s), 0 warnings
stdout
Standard output is empty