码迷,mamicode.com
首页 > 移动开发 > 详细

Demo_CS(移动,切换枪支,发射子弹)

时间:2016-11-02 00:44:54      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:art   9.png   state   显示   alt   后序   put   事件   执行   

技术分享技术分享技术分享技术分享技术分享

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(移动,切换枪支,发射子弹)

标签:art   9.png   state   显示   alt   后序   put   事件   执行   

原文地址:http://www.cnblogs.com/VR-1024/p/6021203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!