zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    Red Hat Enterprise Linux 7.2下使用RPM包安装SQL Server vNext
    VS2015解决方案资源管理器空白,不显示内容
    ArcEngine调用FeatureToLine工具传参问题
    ArcEngine调用GP里的Merge工具传参问题
    ArcGIS GP服务的发布及调用
    利用 Chrome 原生工具进行网页长截图
    关于ueditor与arcgis js api同用会报错的问题
    关于ueditor使用说明
    bootstraptable为行中的按钮添加事件
    关于html与body的高度问题
  • 原文地址:https://www.cnblogs.com/VR-1024/p/6021203.html
Copyright © 2011-2022 走看看