[text] a

Viewer

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7.  
  8. public class GerakBola : MonoBehaviour
  9. {
  10.  
  11.  
  12.     public int force;
  13.     Rigidbody2D rigid;
  14.  
  15.  
  16.     int scoreP1;
  17.     int scoreP2;
  18.  
  19.  
  20.     Text scoreUIP1;
  21.     Text scoreUIP2;
  22.  
  23.  
  24.     GameObject panelSelesai;
  25.     Text txPemenang;
  26.  
  27.     AudioSource audioSource;
  28.     public AudioClip hitSound;
  29.     public AudioClip wallHitSound;
  30.  
  31.  
  32.      void Start ()
  33.     {
  34.         rigid = GetComponent<Rigidbody2D> ();
  35.         Vector2 arah = new Vector2 (2, 0).normalized;
  36.         rigid.AddForce (arah * force);
  37.  
  38.  
  39.         scoreP1 = 0;
  40.         scoreP2 = 0;
  41.  
  42.  
  43.         scoreUIP1 = GameObject.Find("Score1").GetComponent<Text>();
  44.         scoreUIP2 = GameObject.Find("Score2").GetComponent<Text>();
  45.  
  46.  
  47.         panelSelesai = GameObject.Find ("PanelSelesai");
  48.         panelSelesai.SetActive (false);
  49.  
  50.         audioSource = GetComponent<AudioSource>();
  51.     }
  52.  
  53.  
  54.     // Update is called once per frame
  55.     void Update ()
  56.     {
  57.  
  58.  
  59.     }
  60.  
  61.  
  62.     private void OnCollisionEnter2D (Collision2D coll)
  63.     {
  64.         if (coll.gameObject.name == "TepiKanan") {
  65.             audioSource.PlayOneShot(wallHitSound);
  66.             scoreP1 += 1;
  67.             TampilkanScore ();
  68.             if (scoreP1 == 5) {
  69.                 panelSelesai.SetActive (true);
  70.                 txPemenang = GameObject.Find ("Pemenang").GetComponent<Text> ();
  71.                 txPemenang.text = "Darth Vader Wins!";
  72.                 Destroy (gameObject);
  73.                 return;
  74.             }
  75.             ResetBall ();
  76.             Vector2 arah = new Vector2 (2, 0).normalized;
  77.             rigid.AddForce (arah * force);
  78.         }
  79.  
  80.  
  81.         if (coll.gameObject.name == "TepiKiri") {
  82.             audioSource.PlayOneShot(wallHitSound);
  83.             scoreP2 += 1;
  84.             TampilkanScore ();
  85.             if (scoreP2 == 5) { 
  86.                 panelSelesai.SetActive (true);
  87.                 txPemenang = GameObject.Find ("Pemenang").GetComponent<Text> ();
  88.                 txPemenang.text = "Baby Yoda  Wins!";
  89.                 Destroy (gameObject);
  90.                 return;
  91.             }
  92.             ResetBall ();
  93.             Vector2 arah = new Vector2 (-2, 0).normalized;
  94.             rigid.AddForce (arah * force);
  95.         }
  96.  
  97.  
  98.         if (coll.gameObject.name == "pemukul1" || coll.gameObject.name == "pemukul2") {
  99.             audioSource.PlayOneShot(hitSound);
  100.             float sudut = (transform.position.y - coll.transform.position.y) * 5f;
  101.             Vector2 arah = new Vector2 (rigid.velocity.x, sudut).normalized;
  102.             rigid.velocity = new Vector2 (0, 0);
  103.             rigid.AddForce (arah * force * 2);
  104.         }
  105.         
  106.         if (coll.gameObject.name == "TepiAtas" || coll.gameObject.name == "TepiBawah") {
  107.             audioSource.PlayOneShot(hitSound);
  108.         }
  109.  
  110.  
  111.     }
  112.  
  113.  
  114.     void ResetBall ()
  115.     {
  116.         transform.localPosition = new Vector2 (0, 0);
  117.         rigid.velocity = new Vector2 (0, 0);
  118.     }
  119.  
  120.  
  121.     void TampilkanScore ()
  122.     {
  123.         Debug.Log ("Score P1: " + scoreP1 + " Score P2: " + scoreP2);
  124.         scoreUIP1.text = scoreP1 + "";
  125.         scoreUIP2.text = scoreP2 + "";
  126.     }
  127. }
  128.  

Editor

You can edit this paste and save as new:


File Description
  • a
  • Paste Code
  • 29 Apr-2024
  • 3.22 Kb
You can Share it: