Advertisement
BaneOfSmite

Untitled

Aug 1st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Ball : MonoBehaviour {
  5. public float speed = 10f;
  6. public float maxSpeed = 10f;
  7. public string lastplayertouch;
  8. private GameObject gamemanager;
  9.  
  10. public Vector2 BallDirection { get; internal set; }
  11.  
  12. float HitFactor(Vector2 ballPos, Vector2 racketPos,
  13. float racketHeight) {
  14. // ascii art:
  15. // || 1 <- at the top of the racket
  16. // ||
  17. // || 0 <- at the middle of the racket
  18. // ||
  19. // || -1 <- at the bottom of the racket
  20. return (ballPos.y - racketPos.y) / racketHeight;
  21. }
  22.  
  23. void Start() {
  24. gamemanager = GameObject.FindGameObjectWithTag("GameController");
  25. GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
  26. }
  27.  
  28. void FixedUpdate() {
  29. if (rigidbody.velocity.magnitude > maxSpeed) {
  30. rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;
  31. }
  32. }
  33.  
  34. void OnCollisionEnter2D(Collision2D col) {
  35. // Note: 'col' holds the collision information. If the
  36. // Ball collided with a racket, then:
  37. // col.gameObject is the racket
  38. // col.transform.position is the racket's position
  39. // col.collider is the racket's collider
  40.  
  41. // Hit the left Racket?
  42. if (col.gameObject.name == "Player1") {
  43. // Calculate hit Factor
  44. float y = HitFactor(transform.position,
  45. col.transform.position,
  46. col.collider.bounds.size.y);
  47.  
  48. // Calculate direction, make length=1 via .normalized
  49. Vector2 dir = new Vector2(1, y).normalized;
  50.  
  51. // Set Velocity with dir * speed
  52. GetComponent<Rigidbody2D>().velocity = dir * maxSpeed;
  53. GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Clamp(GetComponent<Rigidbody2D>().velocity.x, -8f, 8f), Mathf.Clamp(GetComponent<Rigidbody2D>().velocity.y, -8f, 8f));
  54.  
  55.  
  56. }
  57.  
  58. // Hit the right Racket?
  59. if (col.gameObject.name == "Player2") {
  60. // Calculate hit Factor
  61. float y = HitFactor(transform.position,
  62. col.transform.position,
  63. col.collider.bounds.size.y);
  64.  
  65. // Calculate direction, make length=1 via .normalized
  66. Vector2 dir = new Vector2(-1, y).normalized;
  67.  
  68. // Set Velocity with dir * speed
  69. GetComponent<Rigidbody2D>().velocity = dir * maxSpeed;
  70.  
  71. }
  72.  
  73.  
  74. if (col.gameObject.name == "Player1") {
  75. lastplayertouch = "player1";
  76. } else if (col.gameObject.name == "Player2") {
  77. lastplayertouch = "player2";
  78. }
  79.  
  80.  
  81.  
  82. if (col.gameObject.name.Contains("Brown")) {
  83. if (lastplayertouch == "player1") {
  84. gamemanager.GetComponent<Score>().player1score += 1;
  85. } else {
  86. gamemanager.GetComponent<Score>().player2score += 1;
  87. }
  88. } else if (col.gameObject.name.Contains("Peach")) {
  89. if (lastplayertouch == "player1") {
  90. gamemanager.GetComponent<Score>().player1score += 2;
  91. } else {
  92. gamemanager.GetComponent<Score>().player2score += 2;
  93. }
  94. } else if (col.gameObject.name.Contains("Black")) {
  95. if (lastplayertouch == "player1") {
  96. gamemanager.GetComponent<Score>().player1score += 2;
  97. } else {
  98. gamemanager.GetComponent<Score>().player2score += 2;
  99. }
  100. } else if (col.gameObject.name.Contains("Red")) {
  101. if (lastplayertouch == "player1") {
  102. gamemanager.GetComponent<Score>().player1score += 2;
  103. } else {
  104. gamemanager.GetComponent<Score>().player2score += 2;
  105. }
  106. } else if (col.gameObject.name.Contains("Green")) {
  107. if (lastplayertouch == "player1") {
  108. gamemanager.GetComponent<Score>().player1score += 2;
  109. } else {
  110. gamemanager.GetComponent<Score>().player2score += 2;
  111. }
  112. } else if (col.gameObject.name.Contains("Blue")) {
  113. if (lastplayertouch == "player1") {
  114. gamemanager.GetComponent<Score>().player1score += 2;
  115. } else {
  116. gamemanager.GetComponent<Score>().player2score += 2;
  117. }
  118. } else if (col.gameObject.name.Contains("Yellow")) {
  119. if (lastplayertouch == "player1") {
  120. gamemanager.GetComponent<Score>().player1score += 2;
  121. } else {
  122. gamemanager.GetComponent<Score>().player2score += 2;
  123. }
  124. } else if (col.gameObject.name.Contains("Purple")) {
  125. if (lastplayertouch == "player1") {
  126. gamemanager.GetComponent<Score>().player1score += 2;
  127. } else {
  128. gamemanager.GetComponent<Score>().player2score += 2;
  129. }
  130. } else if (col.gameObject.name.Contains("LeftWall")) {
  131. gamemanager.GetComponent<Score>().player1score -= 2;
  132. }
  133.  
  134. if (col.gameObject.name.Contains("RightWall")) {
  135. gamemanager.GetComponent<Score>().player2score -= 2;
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement