Unity实战篇:利用Easy Touch实现Moba游戏技能释放 (指向性追踪技能,范围AOE技能)
<2>指向性追踪技能,通过射线检测决定施法目标,这仅仅是一个思路,对于普通攻击,只需要检测人物周身敌人的血量来选择目标即可。
<3>范围AOE技能同理,只是把Slider换成普通的圆形image,根据摇杆与摇杆背景距离之比和偏移角度按比例转换到技能辅助显示上(这个比例按自己需求来设置),并且在指定位置释放范围AOE技能。
效果图
直接ctrl+d一份slider
由于我们这个技能释放范围是一定的所以直接把值设置为最大值。
skill2摇杆
脚本
public GameObject go;//用来暂时储存相对偏移角度,方便计算
public GameObject skill2;
public GameObject SKILL2;//摇杆
public Slider Skill2Slider;
public Image skill2Image;//当我们选择到目标时,将它的指示器改为红色
OnEnable()初始化,同上一篇
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);
skill2.SetActive(false);
}
修改Fire函数
private void Fire(bool follow, GameObject target = null)
{
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;
if(follow)
{
shellInstance.gameObject.GetComponent<ShellExplosion>().targetEnemy = target;//设置追踪对象
shellInstance.useGravity = false;//如果是有目标的,我们要把炮弹重力设为零
}
else
// 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;
}
事件函数
public void OnSkill2Down()
{
skill2.gameObject.SetActive(true);
}
public void OnSkill2Pressed()
{
offset = Vector3.Angle(SKILL2.transform.localPosition, new Vector3(0, 1, 0));
if (SKILL2.transform.localPosition.x < 0)
{
offset = -offset;
}
go.transform.rotation = Quaternion.AngleAxis(offset, new Vector3(0, 1, 0));//绕Y轴旋转offset度,达到同步效果
FindTarget();
}
public void OnSkill2Up()
{
skill2Image.color = Color.white;
skill2.gameObject.SetActive(false);
if(FindTarget())
{
GameObject go = FindTarget();
transform.rotation = Quaternion.AngleAxis(offset, new Vector3(0, 1, 0));//绕Y轴旋转offset度,达到同步效果
Fire(true,go);
}
}
FindTarget函数
private GameObject FindTarget()
{
Ray ray = new Ray(transform.position, go.transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.transform.tag == "Player")//这里是敌人的tag
{
skill2Image.color = Color.red;
return hit.transform.gameObject;
}
else
{
skill2Image.color = Color.white;
return null;
}
}
else
{
skill2Image.color = Color.white;
return null;
}
}
ShellExplosion类
只需要加上这一句即可,用来追踪。
注册事件