Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Rigidbody))]
  6.  
  7. public class Asteroid : MonoBehaviour
  8. {
  9. [Header("Inscribed")]
  10. public float speed = 10;
  11.  
  12. public int numSplit = 2;
  13.  
  14. [Header("Dynamic")]
  15. public int size = 3;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. Vector3 vel = Random.insideUnitCircle;
  20. if (vel.magnitude == 0)
  21. {
  22. vel = Random.insideUnitCircle;
  23. }
  24. vel.Normalize();
  25. vel *= speed / size;
  26. GetComponent<Rigidbody>().velocity = vel;
  27.  
  28. transform.localScale = Vector3.one * (size / 3f);
  29. }
  30.  
  31. // Update is called once per frame
  32. void Update()
  33. {
  34.  
  35. }
  36.  
  37. private void OnCollisionEnter(Collision collision)
  38. {
  39. bullet bull = collision.gameObject.GetComponent<bullet>();
  40. if (bull == null) return;
  41.  
  42.  
  43. bull.KillMe();
  44.  
  45. if (size > 1)
  46. {
  47. for (int i = 0; i < numSplit; i++)
  48. {
  49. GameObject go = Instantiate<GameObject>(this.gameObject);
  50. Asteroid ast = go.GetComponent<Asteroid>();
  51. ast.size = size - 1;
  52. }
  53. }
  54.  
  55. Destroy(gameObject);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement