Advertisement
LeeMace

Simple Pong Slice

Jan 18th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | Gaming | 0 0
  1. //game manager script
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using TMPro;
  8.  
  9. public class GameManager : MonoBehaviour
  10. {
  11.     [Header("Ball")]
  12.     public GameObject ball;
  13.  
  14.     [Header("Player 1")]
  15.     public GameObject player1Paddle;
  16.     public GameObject player1Goal;
  17.  
  18.     [Header("Player 2")]
  19.     public GameObject player2Paddle;
  20.     public GameObject player2Goal;
  21.  
  22.     [Header("Score UI")]
  23.     public GameObject Player1Text;
  24.     public GameObject Player2Text;
  25.  
  26.     private int Player1Score;
  27.     private int Player2Score;
  28.  
  29.     public void Player1Scored()
  30.     {
  31.         Player1Score++;
  32.         Player1Text.GetComponent<TextMeshProUGUI>().text = Player1Score.ToString();
  33.         ResetPosition();
  34.     }
  35.  
  36.     public void Player2Scored()
  37.     {
  38.         Player2Score++;
  39.         Player2Text.GetComponent<TextMeshProUGUI>().text = Player2Score.ToString();
  40.         ResetPosition();
  41.  
  42.     }
  43.  
  44.     private void ResetPosition()
  45.     {
  46.         ball.GetComponent<Ball>().Reset();
  47.         player1Paddle.GetComponent<Paddle>().Reset();
  48.         player2Paddle.GetComponent<Paddle>().Reset();
  49.  
  50.     }
  51.  
  52. }
  53.  
  54. //ball script
  55.  
  56. using System.Collections;
  57. using System.Collections.Generic;
  58. using UnityEngine;
  59.  
  60. public class Ball : MonoBehaviour
  61. {
  62.     public float speed;
  63.     public Rigidbody2D rb;
  64.     public Vector3 startPosition;
  65.     void Start()
  66.     {
  67.         Launch();
  68.     }
  69.  
  70.     private void Launch()
  71.     {
  72.         float x = Random.Range(0, 2) == 0 ? -1 : 1;
  73.         float y = Random.Range(0, 2) == 0 ? -1 : 1;
  74.         rb.velocity = new Vector2(speed * x, speed * y);
  75.     }
  76.  
  77.     public void Reset()
  78.     {
  79.         rb.velocity = Vector2.zero;
  80.         transform.position = startPosition;
  81.         Launch();
  82.     }
  83. }
  84.  
  85. //paddle script
  86.  
  87. using System.Collections;
  88. using System.Collections.Generic;
  89. using UnityEngine;
  90.  
  91. public class Paddle : MonoBehaviour
  92. {
  93.     public bool isPlayer1;
  94.     public float speed;
  95.     public Rigidbody2D rb;
  96.     public Vector3 startPosition;
  97.  
  98.     private float movement;
  99.  
  100.     void Start()
  101.     {
  102.         startPosition = transform.position;
  103.     }
  104.     void Update()
  105.     {
  106.         if (isPlayer1)
  107.         {
  108.             movement = Input.GetAxisRaw("Vertical");
  109.         }
  110.         else
  111.         {
  112.             movement = Input.GetAxisRaw("Vertical2");
  113.         }
  114.  
  115.         rb.velocity = new Vector2(rb.velocity.x, movement * speed);
  116.     }
  117.  
  118.     public void Reset()
  119.     {
  120.         rb.velocity = Vector2.zero;
  121.         transform.position = startPosition;
  122.     }
  123. }
  124.  
  125. //goal script
  126.  
  127. using System.Collections;
  128. using System.Collections.Generic;
  129. using UnityEngine;
  130.  
  131. public class Goal : MonoBehaviour
  132. {
  133.     public bool isPlayerGoal;
  134.  
  135.     private void OnTriggerEnter2D(Collider2D collision)
  136.     {
  137.         if (collision.gameObject.CompareTag("Ball"))
  138.         {
  139.             if (!isPlayerGoal)
  140.             {
  141.                 Debug.Log("Player 1 Scored...");
  142.                 GameObject.Find("GameManager").GetComponent<GameManager>().Player1Scored();
  143.             }
  144.             else
  145.             {
  146.                 Debug.Log("Player 2 Scored...");
  147.                 GameObject.Find("GameManager").GetComponent<GameManager>().Player2Scored();
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement