Guest User

Untitled

a guest
Feb 23rd, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.    
  4. public class Ball1 : MonoBehaviour {
  5.     private Paddle paddle;
  6.     private bool hasStarted;
  7.     private Vector3 paddleToBallVector;
  8.     private Vector2 launch;
  9.     // Use this for initialization
  10.     void Start () {
  11.         paddle = GameObject.FindObjectOfType<Paddle> ();
  12.         paddleToBallVector = this.transform.position - paddle.transform.position;
  13.     }
  14.  
  15.  
  16.  
  17.     // Update is called once per frame
  18.     void Update () {
  19.         //print (Input.mousePosition / Screen.width * 16);
  20.  
  21.         if (!hasStarted) {
  22.             this.transform.position = paddle.transform.position + paddleToBallVector;
  23.         }
  24.         if (Input.GetMouseButtonDown (1)) {
  25.             hasStarted = true;
  26.             launch = new Vector2 (Random.Range(-5f , 5f), 10f);
  27.             this.GetComponent<Rigidbody2D> ().velocity = launch;
  28.         }
  29.     }
  30.  
  31. void OnCollisionEnter2D (Collision2D col){
  32.         print ("Velocity = " + launch );
  33.         Vector2 fly = new Vector2 (Random.Range (0f, 1f), 10f);
  34.         Debug.Log (Time.time.ToString () + ": New Vector 2 fly :" + fly);
  35.         GetComponent<Rigidbody2D>().velocity += fly;
  36.         Debug.Log (Time.time.ToString() + ": Ball velocity: " + GetComponent<Rigidbody2D>().velocity);
  37.         if (hasStarted) {
  38.             GetComponent<AudioSource> ().Play ();
  39.  
  40.             GetComponent<Rigidbody2D> ().velocity += fly;
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment