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);
        }
    }
  • 相关阅读:
    C# 之 FTPserver中文件上传与下载(一)
    net-snmp-5.7.3配置编译安装
    Linux下编译安装Apache Http Server
    linux回收站设计
    String封装——读时共享,写时复制
    4-python学习——数据操作
    3-python学习——变量
    2-python学习——hello world
    1 python学习——python环境配置
    04-树7. Search in a Binary Search Tree (25)
  • 原文地址:https://www.cnblogs.com/VR-1024/p/6021203.html
Copyright © 2011-2022 走看看