Bullet.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Bullet : MonoBehaviour {
  5. // Use this for initialization
  6. void Start () {
  7. }
  8. void Awake()
  9. {
  10. _ray = new Ray();
  11. }
  12. // Update is called once per frame
  13. void Update () {
  14. }
  15. private GameObject go;
  16. private Bullet gs;
  17. Ray _ray;
  18. RaycastHit _hit;
  19. public void CreateRay( Vector3 pos, Quaternion _rotation,int _type)
  20. {
  21. if (!this.go)
  22. {
  23. switch (_type)
  24. {
  25. case 1:
  26. this.go = GameObject.Instantiate(GameController.GetInstance().bulletG);
  27. go.name = GameController.GetInstance().bulletG.name;
  28. go.transform.SetParent(GameController.GetInstance().Bullet);
  29. break;
  30. case 2:
  31. this.go = GameObject.Instantiate(GameController.GetInstance().bulletR);
  32. go.name = GameController.GetInstance().bulletR.name;
  33. go.transform.SetParent(GameController.GetInstance().Bullet);
  34. break;
  35. }
  36. this.gs = this.go.AddComponent<Bullet>();
  37. }
  38. else
  39. {
  40. go.SetActive(true);
  41. }
  42. this.go.transform.position = pos;
  43. this.go.transform.rotation = _rotation;
  44. print(this.go.transform.eulerAngles);
  45. var forw = this.go.transform.forward;
  46. this._ray.origin = pos;
  47. this._ray.direction = forw;
  48. // float x= pos.x + temp.x * 0.01
  49. // pos = new Vector3(pos.x + temp.x * 0.01, pos.y + temp.y * 0.01, pos.z + temp.z * 0.01);
  50. if (Physics.Raycast(_ray, out _hit))
  51. {
  52. var poi = _hit.point;
  53. var dis = Vector3.Distance(pos, poi);
  54. var scale = this.go.transform.localScale;
  55. this.go.transform.localScale = new Vector3(scale.x, scale.y, dis);
  56. var tag = this._hit.collider.tag;
  57. if (tag == "player" || tag == "moster")
  58. {
  59. this.gs.SetNextNotActive();
  60. }
  61. else
  62. {
  63. var nor = this._hit.normal;
  64. Vector3 curDir = this.go.transform.TransformDirection(Vector3.forward);
  65. var newDir = Vector3.Reflect(curDir, nor);
  66. Quaternion rotation = Quaternion.FromToRotation(Vector3.forward, newDir);
  67. //transform.rotation = rotation;
  68. this.gs.CreateRay(poi, rotation, _type);
  69. }
  70. }
  71. else
  72. {
  73. var scale = this.go.transform.localScale;
  74. this.go.transform.localScale = new Vector3(scale.x, scale.y, 100);
  75. this.gs.SetNextNotActive();
  76. }
  77. }
  78. public void SetNextNotActive()
  79. {
  80. if (this.go)
  81. {
  82. this.go.SetActive(false);
  83. if (this.gs) this.gs.SetNextNotActive();
  84. }
  85. }
  86. }