public class hero : MonoBehaviour{ public float superGunTime = 10f; private float resetSuperGunTime; private int GunCount = 1; //用fire脚本去声明三个变量来存放三个位置的枪口 public fire upGun; public fire rightGun; public fire leftGun;void Start() { // player = GameObject.FindGameObjectWithTag("Player").transform; resetSuperGunTime = superGunTime; superGunTime = 0; GunCount = 1; upGun.OnFire(); } void Update() { superGunTime -= Time.deltaTime; if (superGunTime > 0) { if (GunCount==1) { GunToSuperGun(); GunCount = 3; } } else { GunToNormal(); GunCount = 1; } } private void GunToSuperGun() { //得到fire脚本的OnFire方法 rightGun.OnFire(); leftGun.OnFire(); } private void GunToNormal() { rightGun.StopFire(); leftGun.StopFire(); } public void OnTriggerEnter2D(Collider2D collider) { if (collider.tag=="Award")//判断是否碰到了Tag标签 { print("是Awar"); Award award = collider.GetComponent();//得到Award脚本的组件 if (award.type==1)//判断type的类型 { print("是1"); superGunTime = resetSuperGunTime; Destroy(collider.gameObject);//销毁碰撞到的物体的 } } }}
这里是fire脚本的代码
using System;using UnityEngine;using System.Collections;public class fire : MonoBehaviour{ public float rate = 0.2f; public GameObject bullet; //实例化子弹 public void Fire() { GameObject.Instantiate(bullet, transform.position, Quaternion.identity); } //如何实例化子弹 public void OnFire() { InvokeRepeating("Fire",0.1f,rate); } //停止子弹发射 public void StopFire() { //取消invoke的指令 CancelInvoke("Fire"); }}