using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { // Use this for initialization void Start () { } void Awake() { _ray = new Ray(); } // Update is called once per frame void Update () { } private GameObject go; private Bullet gs; Ray _ray; RaycastHit _hit; public void CreateRay( Vector3 pos, Quaternion _rotation,int _type) { if (!this.go) { switch (_type) { case 1: this.go = GameObject.Instantiate(GameController.GetInstance().bulletG); go.name = GameController.GetInstance().bulletG.name; go.transform.SetParent(GameController.GetInstance().Bullet); break; case 2: this.go = GameObject.Instantiate(GameController.GetInstance().bulletR); go.name = GameController.GetInstance().bulletR.name; go.transform.SetParent(GameController.GetInstance().Bullet); break; } this.gs = this.go.AddComponent(); } else { go.SetActive(true); } this.go.transform.position = pos; this.go.transform.rotation = _rotation; print(this.go.transform.eulerAngles); var forw = this.go.transform.forward; this._ray.origin = pos; this._ray.direction = forw; // float x= pos.x + temp.x * 0.01 // pos = new Vector3(pos.x + temp.x * 0.01, pos.y + temp.y * 0.01, pos.z + temp.z * 0.01); if (Physics.Raycast(_ray, out _hit)) { var poi = _hit.point; var dis = Vector3.Distance(pos, poi); var scale = this.go.transform.localScale; this.go.transform.localScale = new Vector3(scale.x, scale.y, dis); var tag = this._hit.collider.tag; if (tag == "player" || tag == "moster") { this.gs.SetNextNotActive(); } else { var nor = this._hit.normal; Vector3 curDir = this.go.transform.TransformDirection(Vector3.forward); var newDir = Vector3.Reflect(curDir, nor); Quaternion rotation = Quaternion.FromToRotation(Vector3.forward, newDir); //transform.rotation = rotation; this.gs.CreateRay(poi, rotation, _type); } } else { var scale = this.go.transform.localScale; this.go.transform.localScale = new Vector3(scale.x, scale.y, 100); this.gs.SetNextNotActive(); } } public void SetNextNotActive() { if (this.go) { this.go.SetActive(false); if (this.gs) this.gs.SetNextNotActive(); } } }