Advertisement
Guest User

PongBall

a guest
May 10th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5.  
  6. public class Ball : MonoBehaviour
  7. {
  8.     //Prędkość piłki
  9.     public float speed = 5f;
  10.     public void StartGame()
  11.     {
  12.         //losowanie kierunku, w który poleci piłka
  13.         float x = Random.Range(0, 2) == 0 ? -1 : 1;
  14.         float y = Random.Range(0, 2) == 0 ? -1 : 1;
  15.         GetComponent<Rigidbody>().velocity = new Vector3(x * speed, y * speed, 0f);
  16.     }
  17.  
  18.  
  19.     //Ustawienie pozycji startowej
  20.     public void SetStartPosition()
  21.     {
  22.         //położenie na środku sceny
  23.         transform.position = new Vector3(0, 0, 0);
  24.         //zatrzymanie prędkości
  25.         GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
  26.     }
  27.  
  28.     private void OnCollisionEnter(Collision collision)
  29.     {
  30.         if(collision.gameObject.name == "Player2Area")
  31.         {
  32.             PongManager.instance.UpdatePoints(true);
  33.             PongManager.instance.gameRun = false;
  34.             SetStartPosition();
  35.         }
  36.  
  37.         if (collision.gameObject.name == "Player1Area")
  38.         {
  39.             PongManager.instance.UpdatePoints(false);
  40.             PongManager.instance.gameRun = false;
  41.             SetStartPosition();
  42.         }
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement