unity3d:彈道飛行

敵人間隔5-10s開槍,有子彈從我眼前飛過,粒子使用lean緩衝池循環使用

敵人:

using Lean.Pool;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GunEffectCtrl : MonoBehaviour {
    public Transform m_bornTrans;
    public Transform m_toTrans;

    public LeanGameObjectPool m_poolDanDao;
    public LeanGameObjectPool m_poolQiangKou;

    public float m_nextMin = 5;
    public float m_nextMax = 10;
    float m_nextTime = 1; 
    float m_time = 0;
    // Use this for initialization
    void Start () {
        m_time = Time.time;
        m_nextTime = Time.time + (float)Random.Range(m_nextMin, m_nextMax);

    }
	
	// Update is called once per frame
	void Update () {
        m_time += Time.deltaTime;
        if (m_time >= m_nextTime)
        {
            m_nextTime = Time.time + (float)Random.Range(m_nextMin, m_nextMax);
            Fire();
        }
	}


    void Fire()
    {
        Vector3 dir = m_toTrans.position - m_bornTrans.position;
        

        GameObject danDao = m_poolDanDao.Spawn(m_bornTrans.position, m_bornTrans.rotation);
        danDao.transform.forward = dir.normalized;
        GameObject qiangKou = m_poolQiangKou.Spawn(m_bornTrans.position, m_bornTrans.rotation);
        //PublicFunc.PlayParticle(qiangKou.transform);

        if (danDao.GetComponent<DanDaoFlyCtrl>() == null)
        {
            danDao.AddComponent<DanDaoFlyCtrl>();
        }

        danDao.GetComponent<DanDaoFlyCtrl>().SetInfo(m_toTrans, 15);
    }


    void OnDrawGizmos()
    {
       
        // color depends on status
        Color c = Color.white;
        
       
       Debug.DrawLine(m_bornTrans.position, m_toTrans.position, c);
    }
}

彈道粒子飛行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DanDaoFlyCtrl : MonoBehaviour {
    Transform m_trans;
    bool m_isSet = false;
    Transform m_toTrans;
    float m_speed;
	// Use this for initialization
	void Start () {
        m_trans = this.transform;

    }

    public void SetInfo(Transform toTrans, float speed)
    {
        m_toTrans = toTrans;
        m_speed = speed;
        m_isSet = true;
    }
	// Update is called once per frame
	void Update () {
        if (m_isSet == true)
        {
            m_trans.Translate(Vector3.forward * Time.deltaTime * m_speed); 

            //m_trans.position = Vector3.MoveTowards(m_trans.position, m_toTrans.position, Time.deltaTime * m_speed);
        }
    }

    private void OnDisable()
    {
        m_isSet = false;
    }
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章