Advertisement
gizoncio

Breakout aula 14 - bola.cs

Jun 5th, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. //////////////////////////////////////////////
  2. // Créditos Équilibré Cursos e Treinamentos //
  3. // http://www.equilibrecursos.net           //
  4. // http://www.youtube.com/equilibrecursos   //
  5. //////////////////////////////////////////////
  6.  
  7. using UnityEngine;
  8. using System.Collections;
  9.  
  10. public class bola : MonoBehaviour {
  11.  
  12.     public float velBola;
  13.     public Transform jogador;
  14.     public Rigidbody2D rb2D;
  15.  
  16.     public float posicaoBolinha;
  17.  
  18.     private bool comecou;
  19.  
  20.     public GameObject[] powerUps;
  21.  
  22.     public float porcentagem;
  23.  
  24.     private player scriptPlayer;
  25.  
  26.     public GameObject explosao;
  27.  
  28.     // Use this for initialization
  29.     void Start () {
  30.         //rigidbody2D.velocity = new Vector2(Random.Range(-2, 2), velBola);
  31.  
  32.         scriptPlayer = GameObject.Find("player").GetComponent<player>();
  33.         rb2D = GetComponent<Rigidbody2D>();
  34.     }
  35.  
  36.     void Update()
  37.     {
  38.         if (Input.GetKeyDown(KeyCode.Space) && !comecou)
  39.         {
  40.             rb2D.velocity = new Vector2(Random.Range(-2f, 2f), velBola);
  41.             comecou = true;
  42.         }
  43.  
  44.         if (!comecou)
  45.         {
  46.             transform.position = new Vector2(jogador.position.x, transform.position.y);
  47.         }
  48.  
  49.     }
  50.  
  51.     void FixedUpdate () {
  52.  
  53.         if (comecou)
  54.         {
  55.             if (rb2D.velocity.y < 2 && GetComponent<Rigidbody2D>().velocity.y > -2)
  56.             {
  57.                 rb2D.gravityScale = 3;
  58.             }
  59.             else
  60.             {
  61.                 rb2D.gravityScale = 0;
  62.             }
  63.         }
  64.     }
  65.  
  66.     float colisaoBolinha (Vector2 posicaoBolinha, Vector2 posicaoJogador, float larguraJogador)
  67.     {
  68.         return (posicaoBolinha.x - posicaoJogador.x) / larguraJogador;
  69.     }
  70.  
  71.     void OnCollisionEnter2D(Collision2D outro)
  72.     {
  73.         if (outro.gameObject.tag == "bloco_verde" || outro.gameObject.tag == "bloco_laranja" || outro.gameObject.tag == "bloco_azul" || outro.gameObject.tag == "bloco_amarelo")
  74.         {
  75.             if (scriptPlayer.ativaExplosao)
  76.                 Instantiate(explosao, outro.transform.position, Quaternion.identity);
  77.  
  78.             if (Random.Range(0f, 1f) <= porcentagem)
  79.             {
  80.                 Instantiate(powerUps[Random.Range(0,powerUps.Length)], outro.gameObject.transform.position, Quaternion.identity);
  81.             }
  82.  
  83.             Destroy(outro.gameObject);
  84.         }
  85.  
  86.         if (outro.gameObject.tag == "Player")
  87.         {
  88.             float resultadoCalculo = colisaoBolinha(transform.position, outro.transform.position, ((BoxCollider2D)outro.collider).size.x);
  89.  
  90.             Vector2 novaDirecao = new Vector2(resultadoCalculo, 1).normalized;
  91.  
  92.             rb2D.velocity = novaDirecao * velBola;
  93.         }
  94.  
  95.         if (outro.gameObject.tag == "colisorBaixo")
  96.         {
  97.             transform.position = new Vector2(jogador.position.x, jogador.position.y + posicaoBolinha);
  98.             comecou = false;
  99.  
  100.             rb2D.velocity = Vector2.zero;
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement