Unity实战篇:利用Easy Touch实现Moba游戏技能释放 (非指向性直线技能)

时间:2024-04-11 09:51:46

<1>非指向性技能,直线施法,这种施法涉及技能长度,人物转身,伤害计算三个关键点。

根据蓄力时间来显示辅助箭头长度,并且箭头长度和发射力度都有一个最大值。

Unity实战篇:利用Easy Touch实现Moba游戏技能释放 (非指向性直线技能)

先在人物下面创建slider

Unity实战篇:利用Easy Touch实现Moba游戏技能释放 (非指向性直线技能)

Unity实战篇:利用Easy Touch实现Moba游戏技能释放 (非指向性直线技能)Unity实战篇:利用Easy Touch实现Moba游戏技能释放 (非指向性直线技能)

编写脚本

这里我为了方便直接通过public方式取得实例,不推荐大家这样做,而是统一transform.Find和GameObject.Find。

public GameObject skill1;//技能游戏物体
public GameObject SKILL1;//摇杆
public Slider Skill1Slider;//技能指示条
private float offset;//用来记录摇杆与摇杆背景偏移角度(new Vector 3(0,1,0))
        public Rigidbody m_Shell;                   // Prefab of the shell.#炮弹刚体(有丶奇怪)
        public Transform m_FireTransform;           // A child of the tank where the shells are spawned.#坦克身上发射炮弹的部

        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 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 float timeVal=0.0f;                                          //用来优化炮弹发射时的时间标志

初始化数值,把技能施法辅助隐藏

        private void OnEnable()
        {
            // When the tank is turned on, reset the launch force and the UI#初始化坦克数据和UI
            m_CurrentLaunchForce = m_MinLaunchForce;
            Skill1Slider.value = m_MinLaunchForce;
            m_ChargeSpeed = (m_MaxLaunchForce - m_MinLaunchForce) / m_MaxChargeTime;
            skill1.SetActive(false);
        }

开火函数

private void Fire ()
        {
            timeVal = 0.0f;//重置时间标志

            // 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;
        }

技能一的事件三个函数,按下时使技能辅助显示,移动时,更新箭头状态。抬起时,人物转身,隐藏技能辅助并发射炮弹。

(注意其中的摇杆角度和箭头角度的转化)

Unity实战篇:利用Easy Touch实现Moba游戏技能释放 (非指向性直线技能)

我们注意到摇杆是可以看做二维的,我们以Y轴为基准

而玩家旋转也是以Y轴为基准旋转

Unity实战篇:利用Easy Touch实现Moba游戏技能释放 (非指向性直线技能)

所以就有以下逻辑

        public void OnSkill1Down()
        {
            skill1.SetActive(true);
            m_CurrentLaunchForce = m_MinLaunchForce;
            m_ShootingAudio.clip = m_ChargingClip;
        }
        public void OnSkill1Pressed()
        {
            timeVal += Time.deltaTime;
            if (timeVal >= 0.1f)//蓄力时间大于0.1的时候才播放声音
            {
                m_ShootingAudio.Play();
                timeVal = -10.0f;
            }
            // Increment the launch force and update the slider.#增加发射力,并且更新辅助箭头状态
            if(m_CurrentLaunchForce<m_MaxLaunchForce)
            m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;

            Skill1Slider.value = m_CurrentLaunchForce;
            
            offset = Vector3.Angle(SKILL1.transform.localPosition, new Vector3(0, 1, 0));
            if (SKILL1.transform.localPosition.x < 0)
            {
                offset = -offset;
            }
        }
        public void OnSkill1Up()
        {
            skill1.SetActive(false);
            transform.rotation = Quaternion.AngleAxis(offset, new Vector3(0, 1, 0));//绕Y轴旋转offset度,达到同步效果
            Fire();
        }

注册事件

Unity实战篇:利用Easy Touch实现Moba游戏技能释放 (非指向性直线技能)