using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassTankFire:MonoBehaviour{//炮弹物体,再untiy面板中赋值publicGameObject firePrefab;//keycode,按下发射炮弹,unity面板中赋值publicKeyCode mKeyCode;//炮弹速度,可在unity面板中赋值publicfloat shellSpeed =10;//发射的位置,我们放置的空物体privateTransform _firePosition;// Start is called before the first frame updatevoidStart(){//得到防止的空物体的位置
_firePosition = transform.Find("FirePosition");}// Update is called once per framevoidUpdate(){//如果按下自定义按键if(Input.GetKeyDown(mKeyCode)){//初始化炮弹实例GameObject fireInstance =
GameObject.Instantiate(firePrefab, _firePosition.position, _firePosition.rotation);//发射炮弹,给炮弹的刚体组件一个速度和方向
fireInstance.GetComponent<Rigidbody>().velocity = fireInstance.transform.forward * shellSpeed;}}}
using System.Collections;using System.Collections.Generic;using UnityEngine;publicclass shell : MonoBehaviour
{//爆炸效果publicGameObject boomPrefab;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){}//触发检测voidOnTriggerEnter(Collider other){//销毁炮弹本身Destroy(this.gameObject);//实例化爆炸效果,在触发的位置和方向
GameObject.Instantiate(boomPrefab, transform.position, transform.rotation);}}
给特效打上paly on Awake初始化时立即播放,如下图,并在一定时间(播放完毕后消失)
using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassShellExposition:MonoBehaviour{//播放时间privatefloat _existTime =1.5f;// Start is called before the first frame updatevoidStart(){//一初始化,就立即进行定时销毁Destroy(this.gameObject,_existTime);}// Update is called once per framevoidUpdate(){}}