您的位置:首页 > 移动开发 > Unity3D

Unity2d 机器人被攻击后自动追击的实现

2020-06-21 04:20 567 查看

子弹挂载的代码

通过奇偶判断是否击杀

奇数次:打开机器人AI脚本

偶数次:关闭机器人AI脚本并修复机器人

[code]    //子弹命中
private void OnCollisionEnter2D(Collision2D collision)
{
//调用机器人控制脚本的修复函数,修改机器人的状态
RobotControl ec = collision.gameObject.GetComponent<RobotControl>();
if (ec!=null)
{
ec.Fixed();//杀敌
if (RobotControl.isFixed%2==0)
{
collision.gameObject.GetComponent<AIPath>().enabled = false;
PlayerControl.enemyleft--;//更改玩家控制中的击杀数,以此判断任务是否完成,格式:脚本名.静态变量--;
Debug.Log("当前敌人数:" + PlayerControl.enemyleft);
}
if (RobotControl.isFixed%2 != 0)
{
//打开命中对象的AI组件
collision.gameObject.GetComponent<AIPath>().enabled = true;
//collision.gameObject.GetComponent<RobotControl>().enabled = false;
//RobotControl.AIon();
//ScriptSelect.changestatus();
}
Debug.Log("命中敌人了");
}
//播放命中声
AudioManager.instance.AudioPlay(hitClip);
//立即销毁子弹
Destroy(this.gameObject);
}

机器人控制代码

修复函数

[code] public void Fixed()
{
isFixed--;
Debug.Log("IsFixed:" + isFixed);
if (isFixed%2==0)
{
AIoff();
//ScriptSelect.closeAI();
if (brokeneffect.isPlaying == true)
{
brokeneffect.Stop();
}
AudioManager.instance.AudioPlay(fixedClip);
rbody.simulated = false;//禁用物理
anim.SetTrigger("fix");//播放被修复(打)的动画

//加载预制体资源,掉落子弹
RandomDrop();
}
}

附完整教程:

Unity2d Rubys Adventure 课程设计报告

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: