天天看点

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;
    }
}      

继续阅读