前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >unity3d:弹道飞行

unity3d:弹道飞行

作者头像
立羽
发布2023-08-24 14:59:56
发布2023-08-24 14:59:56
26700
代码可运行
举报
文章被收录于专栏:Unity3d程序开发Unity3d程序开发
运行总次数:0
代码可运行

敌人间隔5-10s开枪,有子弹从我眼前飞过,粒子使用lean缓冲池循环使用

敌人:

代码语言:javascript
代码运行次数:0
复制
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);
    }
}

弹道粒子飞行

代码语言:javascript
代码运行次数:0
复制
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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-04-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档