- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public class GerakBola : MonoBehaviour
- {
- public int force;
- Rigidbody2D rigid;
- int scoreP1;
- int scoreP2;
- Text scoreUIP1;
- Text scoreUIP2;
- GameObject panelSelesai;
- Text txPemenang;
- AudioSource audioSource;
- public AudioClip hitSound;
- public AudioClip wallHitSound;
- void Start ()
- {
- rigid = GetComponent<Rigidbody2D> ();
- Vector2 arah = new Vector2 (2, 0).normalized;
- rigid.AddForce (arah * force);
- scoreP1 = 0;
- scoreP2 = 0;
- scoreUIP1 = GameObject.Find("Score1").GetComponent<Text>();
- scoreUIP2 = GameObject.Find("Score2").GetComponent<Text>();
- panelSelesai = GameObject.Find ("PanelSelesai");
- panelSelesai.SetActive (false);
- audioSource = GetComponent<AudioSource>();
- }
- // Update is called once per frame
- void Update ()
- {
- }
- private void OnCollisionEnter2D (Collision2D coll)
- {
- if (coll.gameObject.name == "TepiKanan") {
- audioSource.PlayOneShot(wallHitSound);
- scoreP1 += 1;
- TampilkanScore ();
- if (scoreP1 == 5) {
- panelSelesai.SetActive (true);
- txPemenang = GameObject.Find ("Pemenang").GetComponent<Text> ();
- txPemenang.text = "Darth Vader Wins!";
- Destroy (gameObject);
- return;
- }
- ResetBall ();
- Vector2 arah = new Vector2 (2, 0).normalized;
- rigid.AddForce (arah * force);
- }
- if (coll.gameObject.name == "TepiKiri") {
- audioSource.PlayOneShot(wallHitSound);
- scoreP2 += 1;
- TampilkanScore ();
- if (scoreP2 == 5) {
- panelSelesai.SetActive (true);
- txPemenang = GameObject.Find ("Pemenang").GetComponent<Text> ();
- txPemenang.text = "Baby Yoda Wins!";
- Destroy (gameObject);
- return;
- }
- ResetBall ();
- Vector2 arah = new Vector2 (-2, 0).normalized;
- rigid.AddForce (arah * force);
- }
- if (coll.gameObject.name == "pemukul1" || coll.gameObject.name == "pemukul2") {
- audioSource.PlayOneShot(hitSound);
- float sudut = (transform.position.y - coll.transform.position.y) * 5f;
- Vector2 arah = new Vector2 (rigid.velocity.x, sudut).normalized;
- rigid.velocity = new Vector2 (0, 0);
- rigid.AddForce (arah * force * 2);
- }
- if (coll.gameObject.name == "TepiAtas" || coll.gameObject.name == "TepiBawah") {
- audioSource.PlayOneShot(hitSound);
- }
- }
- void ResetBall ()
- {
- transform.localPosition = new Vector2 (0, 0);
- rigid.velocity = new Vector2 (0, 0);
- }
- void TampilkanScore ()
- {
- Debug.Log ("Score P1: " + scoreP1 + " Score P2: " + scoreP2);
- scoreUIP1.text = scoreP1 + "";
- scoreUIP2.text = scoreP2 + "";
- }
- }
[text] a
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: