Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Ball : MonoBehaviour {
- public float speed = 10f;
- public float maxSpeed = 10f;
- public string lastplayertouch;
- private GameObject gamemanager;
- public Vector2 BallDirection { get; internal set; }
- float HitFactor(Vector2 ballPos, Vector2 racketPos,
- float racketHeight) {
- // ascii art:
- // || 1 <- at the top of the racket
- // ||
- // || 0 <- at the middle of the racket
- // ||
- // || -1 <- at the bottom of the racket
- return (ballPos.y - racketPos.y) / racketHeight;
- }
- void Start() {
- gamemanager = GameObject.FindGameObjectWithTag("GameController");
- GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
- }
- void FixedUpdate() {
- if (rigidbody.velocity.magnitude > maxSpeed) {
- rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;
- }
- }
- void OnCollisionEnter2D(Collision2D col) {
- // Note: 'col' holds the collision information. If the
- // Ball collided with a racket, then:
- // col.gameObject is the racket
- // col.transform.position is the racket's position
- // col.collider is the racket's collider
- // Hit the left Racket?
- if (col.gameObject.name == "Player1") {
- // Calculate hit Factor
- float y = HitFactor(transform.position,
- col.transform.position,
- col.collider.bounds.size.y);
- // Calculate direction, make length=1 via .normalized
- Vector2 dir = new Vector2(1, y).normalized;
- // Set Velocity with dir * speed
- GetComponent<Rigidbody2D>().velocity = dir * maxSpeed;
- GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Clamp(GetComponent<Rigidbody2D>().velocity.x, -8f, 8f), Mathf.Clamp(GetComponent<Rigidbody2D>().velocity.y, -8f, 8f));
- }
- // Hit the right Racket?
- if (col.gameObject.name == "Player2") {
- // Calculate hit Factor
- float y = HitFactor(transform.position,
- col.transform.position,
- col.collider.bounds.size.y);
- // Calculate direction, make length=1 via .normalized
- Vector2 dir = new Vector2(-1, y).normalized;
- // Set Velocity with dir * speed
- GetComponent<Rigidbody2D>().velocity = dir * maxSpeed;
- }
- if (col.gameObject.name == "Player1") {
- lastplayertouch = "player1";
- } else if (col.gameObject.name == "Player2") {
- lastplayertouch = "player2";
- }
- if (col.gameObject.name.Contains("Brown")) {
- if (lastplayertouch == "player1") {
- gamemanager.GetComponent<Score>().player1score += 1;
- } else {
- gamemanager.GetComponent<Score>().player2score += 1;
- }
- } else if (col.gameObject.name.Contains("Peach")) {
- if (lastplayertouch == "player1") {
- gamemanager.GetComponent<Score>().player1score += 2;
- } else {
- gamemanager.GetComponent<Score>().player2score += 2;
- }
- } else if (col.gameObject.name.Contains("Black")) {
- if (lastplayertouch == "player1") {
- gamemanager.GetComponent<Score>().player1score += 2;
- } else {
- gamemanager.GetComponent<Score>().player2score += 2;
- }
- } else if (col.gameObject.name.Contains("Red")) {
- if (lastplayertouch == "player1") {
- gamemanager.GetComponent<Score>().player1score += 2;
- } else {
- gamemanager.GetComponent<Score>().player2score += 2;
- }
- } else if (col.gameObject.name.Contains("Green")) {
- if (lastplayertouch == "player1") {
- gamemanager.GetComponent<Score>().player1score += 2;
- } else {
- gamemanager.GetComponent<Score>().player2score += 2;
- }
- } else if (col.gameObject.name.Contains("Blue")) {
- if (lastplayertouch == "player1") {
- gamemanager.GetComponent<Score>().player1score += 2;
- } else {
- gamemanager.GetComponent<Score>().player2score += 2;
- }
- } else if (col.gameObject.name.Contains("Yellow")) {
- if (lastplayertouch == "player1") {
- gamemanager.GetComponent<Score>().player1score += 2;
- } else {
- gamemanager.GetComponent<Score>().player2score += 2;
- }
- } else if (col.gameObject.name.Contains("Purple")) {
- if (lastplayertouch == "player1") {
- gamemanager.GetComponent<Score>().player1score += 2;
- } else {
- gamemanager.GetComponent<Score>().player2score += 2;
- }
- } else if (col.gameObject.name.Contains("LeftWall")) {
- gamemanager.GetComponent<Score>().player1score -= 2;
- }
- if (col.gameObject.name.Contains("RightWall")) {
- gamemanager.GetComponent<Score>().player2score -= 2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement