首页 > 代码库 > Demo_CS(移动,切换枪支,发射子弹)
Demo_CS(移动,切换枪支,发射子弹)
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> public void InitBullet() { //生成子弹 GameObject currentblt = Instantiate (bullet, firePoint.position, firePoint.rotation) as GameObject; //给子弹添加速度 currentblt.GetComponent<Rigidbody> ().velocity = firePoint.forward * 10; } }
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++; //防止子对象序号越界 //当序号等于枪支个数,取余后序号归零 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() { //interval时间过后,执行Hide Invoke ("Hide", interval); } /// <summary> /// 隐藏当前游戏对象 /// </summary> void Hide() { gameObject.SetActive (false); } }
Demo_CS(移动,切换枪支,发射子弹)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。