Moster.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Moster : MonoBehaviour {
  5. private GameObject _bulletPos;
  6. // public GameObject prefab;
  7. private GameObject go;
  8. private Bullet gs;
  9. // Use this for initialization
  10. void Awake()
  11. {
  12. this._bulletPos = transform.Find("GunPos").GetChild(0).GetChild(0).Find("bulletPos").gameObject;
  13. }
  14. void Start()
  15. {
  16. _ray = new Ray();
  17. // this.CreateRay();
  18. }
  19. // Update is called once per frame
  20. void Update()
  21. {
  22. }
  23. Ray _ray;
  24. RaycastHit _hit;
  25. void CreateRay()
  26. {
  27. var forw = this.transform.forward;
  28. this._ray.origin = this._bulletPos.transform.position;
  29. this._ray.direction = forw;
  30. if (!this.go)
  31. {
  32. this.go = GameObject.Instantiate(GameController.GetInstance().bulletR);
  33. go.name = GameController.GetInstance().bulletR.name;
  34. go.transform.SetParent(GameController.GetInstance().Bullet);
  35. this.gs = this.go.AddComponent<Bullet>();
  36. }
  37. this.go.transform.position = this._bulletPos.transform.position;
  38. this.go.transform.rotation = this.transform.rotation;
  39. if (Physics.Raycast(_ray, out _hit))
  40. {
  41. var poi = _hit.point;
  42. var dis = Vector3.Distance(this._bulletPos.transform.position, poi);
  43. var scale = this.go.transform.localScale;
  44. this.go.transform.localScale = new Vector3(scale.x, scale.y, dis);
  45. var tag = this._hit.collider.tag;
  46. if (tag == "player" || tag == "moster")
  47. {
  48. this.gs.SetNextNotActive();
  49. }
  50. else
  51. {
  52. var nor = this._hit.normal;
  53. Vector3 curDir = transform.TransformDirection(Vector3.forward);
  54. var newDir = Vector3.Reflect(curDir, nor);
  55. Quaternion rotation = Quaternion.FromToRotation(Vector3.forward, newDir);
  56. this.gs.CreateRay(poi, rotation, 2);
  57. }
  58. }
  59. else
  60. {
  61. var scale = this.go.transform.localScale;
  62. this.go.transform.localScale = new Vector3(scale.x, scale.y, 100);
  63. this.gs.SetNextNotActive();
  64. }
  65. }
  66. public void RotationSelf(float rota)
  67. {
  68. transform.Rotate(Vector3.up, rota);
  69. this.CreateRay();
  70. }
  71. }