using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI ; using TMPro; public class BallScript : MonoBehaviour { public Text Player1score; public Text Player2score; TextMesh P1S; TextMesh P2S; private Vector3 direction; float speed; bool goal; bool player2point; bool player1point; int p1point = 0; int p2point = 0; bool gameend; int maxscore; TextMeshPro p1p2s; TextMeshPro p2p2s; TextMeshPro p1p1s; TextMeshPro p2p1s; void Start () { maxscore = 10; this.direction = new Vector3(1.0f,0.0f,1.0f).normalized; this.speed = 0.1f; goal = false; player2point = false; player1point = false; p1point = 0; p2point = 0; p1p2s = GameObject.Find("P1Player2score").GetComponent(); p2p2s = GameObject.Find("P2Player2score").GetComponent(); p1p1s = GameObject.Find("P1Player1score").GetComponent(); p2p1s = GameObject.Find("P2Player1score").GetComponent(); gameend = false; } void Update() { if (goal == false) { this.transform.position += direction * speed; } else if(player1point==true) { this.transform.position = new Vector3(GameObject.Find("Player2").transform.position.x, 0.430f, GameObject.Find("Player2").transform.position.z - 0.625f); } else if (player2point == true) { this.transform.position = new Vector3(GameObject.Find("Player1").transform.position.x, 0.430f, GameObject.Find("Player1").transform.position.z + 0.625f); } if (gameend == false) { if (Input.GetKeyUp(KeyCode.UpArrow) && player2point == true) { player2point = false; goal = false; this.direction = new Vector3(1.0f, 0.0f, 1.0f).normalized; this.speed = 0.1f; } if (Input.GetKeyUp(KeyCode.W) && player1point == true) { player1point = false; goal = false; this.direction = new Vector3(1.0f, 0.0f, -1.0f).normalized; this.speed = 0.1f; } } else { if (Input.GetKeyUp(KeyCode.Space)) { gameend = false; p1p2s.SetText ("Player2"); p2p2s.SetText("Player2"); p1p1s.SetText("Player1"); p2p1s.SetText("Player1"); p1point = 0; p2point = 0; this.speed = 0.1f; } } } void OnCollisionEnter(Collision collision) { if (collision.collider.name == "Player1") { speed = speed + 0.02f; } else if (collision.collider.name == "Player2") { speed = speed + 0.02f; } else if (collision.collider.name == "WallP1") { if (player2point == false) { p2point++; p1p2s.SetText("Player2-" + p2point.ToString()); p2p2s.SetText("Player2-" + p2point.ToString()); if (p2point == maxscore) { p2p2s.SetText("WIN"); p2p1s.SetText("WIN"); p1p2s.SetText("LOSS"); p1p1s.SetText("LOSS"); gameend = true; } } this.speed = 0.1f; player2point = true; goal = true; this.transform.position = new Vector3(GameObject.Find("Player1").transform.position.x, 0.430f, GameObject.Find("Player1").transform.position.z + 0.625f); } else if (collision.collider.name == "WallP2") { if (player1point == false) { p1point++; p1p1s.SetText("Player1-" + p1point.ToString()); p2p1s.SetText("Player1-" + p1point.ToString()); if (p1point == maxscore) { p2p2s.SetText("LOSS"); p2p1s.SetText("LOSS"); p1p2s.SetText("WIN"); p1p1s.SetText("WIN"); gameend = true; } } this.speed = 0.1f; player1point = true; goal = true; this.transform.position = new Vector3(GameObject.Find("Player2").transform.position.x, 0.430f, GameObject.Find("Player2").transform.position.z - 0.625f); } if (goal == false) { Vector3 normal = collision.contacts[0].normal; direction = Vector3.Reflect(direction, normal); } } }