Player.cs 2.4 KB

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