Unity3D经典案例游戏:TANKS! Unity Tutorial - Phase 5 of 8 - Shells——TankShooting 相关源C#代码解析

一、代码自我解析

二、油管学习地址

三、Unity3D源代码

 

 

一、源代码自我解析

 

using UnityEngine;
using UnityEngine.UI;

public class TankShooting : MonoBehaviour
{
    public int m_PlayerNumber = 1;              // 玩家名
    public Rigidbody m_Shell;                   // 炮弹
    public Transform m_FireTransform;           // 开火位置
    public Slider m_AimSlider;                  // 显示当前发射力的坦克的子坦克
                                                //(个人理解检测炮弹和玩家坦克是否接触的一个变量)
    public AudioSource m_ShootingAudio;         // 引用用于播放拍摄音频的音频源。注:不同于运动音源。
    public AudioClip m_ChargingClip;            // 由于有一个蓄力的音效,这就是每次发射时蓄力的音效
    public AudioClip m_FireClip;                // 每次射击时发出的音效
    public float m_MinLaunchForce = 15f;        // 发射炮弹的最小的力
    public float m_MaxLaunchForce = 30f;        // 发射炮弹的最大的力
    public float m_MaxChargeTime = 0.75f;       // 最大的蓄力时间(达到最大力前)

    
    private string m_FireButton;                // 开火按钮
    private float m_CurrentLaunchForce;         // 记录发射炮弹实时的力(在共有变量的最小和最大之间变化)
    private float m_ChargeSpeed;                // 蓄力的速度
    private bool m_Fired;                       // 判断是否按下发射按钮


    private void OnEnable()
    {
        // 先将对炮弹的力设为最小
        m_CurrentLaunchForce = m_MinLaunchForce;
        m_AimSlider.value = m_MinLaunchForce;
    }


    private void Start()
    {
        // 搞清楚炮弹是谁打出去的(二营长?)
        m_FireButton = "Fire" + m_PlayerNumber;

        // 设置炮弹的发射速度,实际上要靠区间大小以及最大蓄力时间来决定(使炮弹打出去看起来更自然)
        m_ChargeSpeed = (m_MaxLaunchForce - m_MinLaunchForce) / m_MaxChargeTime;
    }
    

    private void Update()
    {
        // Track the current state of the fire button and make decisions based on the current launch force.
        // 将对炮弹的力置为最小
        m_AimSlider.value = m_MinLaunchForce;

        // 发射炮弹环节(如果蓄力过久则直接发射,如果蓄力不够也发射炮弹不过不会比你设置的最小更小)
        if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
        {
            m_CurrentLaunchForce = m_MaxLaunchForce;
            Fire();
        }
        else if (Input.GetButtonDown(m_FireButton))
        {
            m_Fired = false;
            m_CurrentLaunchForce = m_MinLaunchForce;

            m_ShootingAudio.clip = m_ChargingClip;
            m_ShootingAudio.Play();
        }
        else if (Input.GetButton(m_FireButton) && !m_Fired)
        {
            m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;

            m_AimSlider.value = m_CurrentLaunchForce;
        }
        else if (Input.GetButtonUp(m_FireButton) && !m_Fired)
        {
            Fire();
        }
    }


    private void Fire()
    {
        // Instantiate and launch the shell.
        // 开火!加载和更新炮弹!
        m_Fired = true;

        // 让炮弹动起来!
        Rigidbody shellInstance = Instantiate(m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;

        // 炮弹动的时候的速度设置
        shellInstance.velocity = m_CurrentLaunchForce * m_FireTransform.forward;

        // 播放炮弹发射音效
        m_ShootingAudio.clip = m_FireClip;
        m_ShootingAudio.Play();

        // 视频里说这样是为了确保炮弹不会出现一些不必要的BUG
        m_CurrentLaunchForce = m_MinLaunchForce;
    }
}

// 以上只是自己对于该代码的理解,如有误还望指出让我及时改正。

 

二、油管学习Unity地址  

 

https://www.youtube.com/watch?v=paLLfWd2k5A

 

三、Unity3D中该案例源代码:

using UnityEngine;
using UnityEngine.UI;

namespace Complete
{
    public class TankShooting : MonoBehaviour
    {
        public int m_PlayerNumber = 1;              // Used to identify the different players.
        public Rigidbody m_Shell;                   // Prefab of the shell.
        public Transform m_FireTransform;           // A child of the tank where the shells are spawned.
        public Slider m_AimSlider;                  // A child of the tank that displays the current launch force.
        public AudioSource m_ShootingAudio;         // Reference to the audio source used to play the shooting audio. NB: different to the movement audio source.
        public AudioClip m_ChargingClip;            // Audio that plays when each shot is charging up.
        public AudioClip m_FireClip;                // Audio that plays when each shot is fired.
        public float m_MinLaunchForce = 15f;        // The force given to the shell if the fire button is not held.
        public float m_MaxLaunchForce = 30f;        // The force given to the shell if the fire button is held for the max charge time.
        public float m_MaxChargeTime = 0.75f;       // How long the shell can charge for before it is fired at max force.


        private string m_FireButton;                // The input axis that is used for launching shells.
        private float m_CurrentLaunchForce;         // The force that will be given to the shell when the fire button is released.
        private float m_ChargeSpeed;                // How fast the launch force increases, based on the max charge time.
        private bool m_Fired;                       // Whether or not the shell has been launched with this button press.


        private void OnEnable()
        {
            // When the tank is turned on, reset the launch force and the UI
            m_CurrentLaunchForce = m_MinLaunchForce;
            m_AimSlider.value = m_MinLaunchForce;
        }


        private void Start ()
        {
            // The fire axis is based on the player number.
            m_FireButton = "Fire" + m_PlayerNumber;

            // The rate that the launch force charges up is the range of possible forces by the max charge time.
            m_ChargeSpeed = (m_MaxLaunchForce - m_MinLaunchForce) / m_MaxChargeTime;
        }


        private void Update ()
        {
            // The slider should have a default value of the minimum launch force.
            m_AimSlider.value = m_MinLaunchForce;

            // If the max force has been exceeded and the shell hasn't yet been launched...
            if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
            {
                // ... use the max force and launch the shell.
                m_CurrentLaunchForce = m_MaxLaunchForce;
                Fire ();
            }
            // Otherwise, if the fire button has just started being pressed...
            else if (Input.GetButtonDown (m_FireButton))
            {
                // ... reset the fired flag and reset the launch force.
                m_Fired = false;
                m_CurrentLaunchForce = m_MinLaunchForce;

                // Change the clip to the charging clip and start it playing.
                m_ShootingAudio.clip = m_ChargingClip;
                m_ShootingAudio.Play ();
            }
            // Otherwise, if the fire button is being held and the shell hasn't been launched yet...
            else if (Input.GetButton (m_FireButton) && !m_Fired)
            {
                // Increment the launch force and update the slider.
                m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;

                m_AimSlider.value = m_CurrentLaunchForce;
            }
            // Otherwise, if the fire button is released and the shell hasn't been launched yet...
            else if (Input.GetButtonUp (m_FireButton) && !m_Fired)
            {
                // ... launch the shell.
                Fire ();
            }
        }


        private void Fire ()
        {
            // Set the fired flag so only Fire is only called once.
            m_Fired = true;

            // Create an instance of the shell and store a reference to it's rigidbody.
            Rigidbody shellInstance =
                Instantiate (m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;

            // Set the shell's velocity to the launch force in the fire position's forward direction.
            shellInstance.velocity = m_CurrentLaunchForce * m_FireTransform.forward; 

            // Change the clip to the firing clip and play it.
            m_ShootingAudio.clip = m_FireClip;
            m_ShootingAudio.Play ();

            // Reset the launch force.  This is a precaution in case of missing button events.
            m_CurrentLaunchForce = m_MinLaunchForce;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值