CS_Demo

来源:互联网 发布:centos 超时时间 编辑:程序博客网 时间:2024/04/29 15:20
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(){//如果当前动画状态信息为Normalif (ani.GetCurrentAnimatorStateInfo (0).IsName ("Normal")) {         //播放Fire动画ani.SetTrigger ("Fire");//播放Fire声音在当前位置AudioSource.PlayClipAtPoint(fireClip,transform.position);//显示火花特效muzzleFlash.SetActive(true);}}public void Reload(){//如果当前动画状态信息为Normalif (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
原创粉丝点击