CS_Demo

using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour {
	private Animator ani;
	public AudioClip fireClip;//开火声音
	public AudioClip reloadClip;//装子弹声音
	public AudioClip readyClip;//准备声音
	public GameObject muzzleFlash;//火花特效
	public GameObject bullet;//预设体
	private Transform firePoint;//发射点
	void Awake(){
		ani = GetComponent<Animator> ();
		firePoint = transform.Find ("FirePoint");
	}

	public void Fire(){
		//如果当前动画状态信息为Normal
		if (ani.GetCurrentAnimatorStateInfo (0).IsName ("Normal")) {
		         //播放Fire动画
			ani.SetTrigger ("Fire");
			//播放Fire声音在当前位置
			AudioSource.PlayClipAtPoint(fireClip,transform.position);
			//显示火花特效
			muzzleFlash.SetActive(true);
		}
	}
	public void Reload(){
		//如果当前动画状态信息为Normal
		if (ani.GetCurrentAnimatorStateInfo (0).IsName ("Normal")) {
			//播放动画
			ani.SetTrigger("Reload");
			//播放声音
			AudioSource.PlayClipAtPoint(reloadClip,transform.position);
		}
	}
	/// <summary>
	/// 生成子弹
	/// </summary>
	void InitBullet(){
		//生成子弹
		GameObject currentblt=Instantiate(bullet,firePoint.position,firePoint.rotation)as GameObject;
		//给子弹添加速度
		currentblt.GetComponent<Rigidbody> ().velocity = firePoint.forward * 10f;
	
	}
}





using UnityEngine;
using System.Collections;

public class GunManager : MonoBehaviour {

	private int currentGunIndex = 0;//当前枪支序号
	private Gun currentGun;//当前枪支脚本
	void Start(){
		//找到默认枪支
		currentGun = transform.GetChild(currentGunIndex).GetComponent<Gun> ();
	}
	void Update(){
		if (Input.GetMouseButtonDown(0)) {
			currentGun.Fire ();
		}
		if (Input.GetKeyDown(KeyCode.R)) {
			currentGun.Reload ();
		}
		if (Input.GetKeyDown(KeyCode.Q)) {
			GunSwitch ();
		}
	}
	/// <summary>
	/// 换枪
	/// </summary>
	void GunSwitch(){
		//隐藏当前使用的枪支
		transform.GetChild (currentGunIndex).gameObject.SetActive (false);
		//换下一把枪
		currentGunIndex++;
		//Low
//		if (currentGunIndex==transform.childCount) {
//			currentGunIndex = 0;
//		}
	        //防止子对象越界
		//当序号大于枪支个数,取余后序号归零。
		currentGunIndex=currentGunIndex%transform.childCount;
		//显示新的枪支
		transform.GetChild (currentGunIndex).gameObject.SetActive (true);
		//更新枪支
		Start();
	}

}




using UnityEngine;
using System.Collections;

public class MuzzleFlash : MonoBehaviour {
	//火花显示时间
	public float interval = 0.1f;
	/// <summary>
	/// 在可用状态时执行
	/// </summary>
	void OnEnable(){
		//每隔一段时间执行方法
		Invoke("Hide",interval);
	
	}
	/// <summary>
	/// 隐藏当前游戏对象
	/// </summary>
	void Hide(){
		gameObject.SetActive (false);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值