123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 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<Bullet>();
- }
- 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();
- }
- }
- }
|