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

编程入门 行业动态 更新时间:2024-10-15 06:13:24

Unity实战篇:利用Easy Touch实现Moba游戏<a href=https://www.elefans.com/category/jswz/34/1769551.html style=技能释放 (非指向性直线技能)"/>

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

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

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

先在人物下面创建slider

编写脚本

这里我为了方便直接通过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#初始化坦克数据和UIm_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;}

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

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

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

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

所以就有以下逻辑

        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游戏技能释放 (非指向性直线技能)

本文发布于:2024-02-13 22:43:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:技能   直线   指向性   实战篇   游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!