RPG2.0战斗框架学习3之技能镜头效果

前言

这一篇讲讲流程中的第二步,PartyAI到RoleAI传递的过程中,可以执行技能特效,比如摄像机动画和全屏特效


步骤


1 执行技能特效,肯定需要一个协程,看伪代码


    public void InputSkilButton(int playerindex, int skillindex)//角色索引,索引
    {
		//程序健壮性判断
        if (Time.timeScale < 0.5f) return;//如果正在播放技能特效,则不能释放招数
        if (playerindex >= playerParty.Count) return;		
        if (playerParty[playerindex].CanInputSkill(skillindex))
        {
			//处理对释放技能后的变化			
            GameObject fxscrIns = 根据技能索引得到技能信息,再根据信息取到 镜头特效(释放大招时才有哦)        
            if (fxscrIns == null)//不是大招
            { 
                if (逻辑判断)
                {
                    StartCoroutine(PlaySwordArt(playerindex, skillindex, null));//执行特效的协程
                    //继续执行逻辑处理
                }
            }
            else
            {
                GameObject tempFXscr = Instantiate(fxscrIns) as GameObject;//实例化镜头特效
				if (逻辑判断)
				{
					StartCoroutine(PlaySwordArt(playerindex, skillindex, tempFXscr));
				}
            }
        }
    }
	
	///  释放技能时的特效,屏幕变暗,全屏动画等 
    IEnumerator PlaySwordArt(int playerindex, int skillindex, GameObject tempFXscr)//角色索引,技能索引,技能
    {
		//实例一个相机,只看UI层和Skill层,将depth高于MainCamera,放上黑色遮罩,就会造成全屏变黑,但是释放技能的
		//角色 (和被攻击到的敌人)是亮着的。
        foreach (CharacterAI ai1 in playerParty)
        {
            if (ai1 != null) UnlitToolS.setLayerName(ai1.gameObject, LayerMask.NameToLayer("Character"));
        }
        CharacterAI aitemp = playerParty[playerindex];
        showArtPlayer = playerParty[playerindex];
        WorldMgr.instance.timeScale = 0.02f;///暂停世界时间
        int layetemp = playerParty[playerindex].gameObject.layer;
        UnlitToolS.setLayerName(playerParty[playerindex].gameObject, LayerMask.NameToLayer( "SkillShow"));
        GameObject tempCamera =读取生成一个SkillCamera;
		//设置坐标和主相机一样
        tempCamera.transform.position = Camera.main.transform.position;
        tempCamera.transform.rotation = Camera.main.transform.rotation;
        tempCamera.transform.parent = Camera.main.transform;
        tempCamera.transform.localScale = Vector3.one;
        GameObject tempmask = 读取生成一个遮罩ScreenMask;
		//设置坐标在主相机之前,挡住主相机
        tempmask.transform.position =  Camera.main.transform.position + Camera.main.transform.forward;
        tempmask.transform.rotation = Camera.main.transform.rotation;
        tempmask.transform.parent = Camera.main.transform;
        tempmask.transform.localScale = Vector3.one;
		//得到被攻击的敌人,改变他们的层级
        List skilltargettemp = 得到敌人列表
        foreach (GameObject o in skilltargettemp)
        {
            UnlitToolS.setLayerName(o, LayerMask.NameToLayer("SkillShow"));
        }
		//技能特效2 绑定在人物身上
        GameObject fxdeadtemp = SAOResources.SAOToolObj("FXSkillPlayer");//技能特效
        fxdeadtemp.SendMessage("AttachBodyPart", showArtPlayer.gameObject , SendMessageOptions.DontRequireReceiver);
        Destroy(fxdeadtemp, 3);
		//生成一个物体,这个物体之后被销毁,作用就是给一个相机移动的坐标
        GameObject skillanimatorcamera = 读取生成一个SkillCameraNew;
        playerParty[playerindex].InputSkill(skillindex);
        skillanimatorcamera.transform.position = playerParty[playerindex].transform.position;
        skillanimatorcamera.transform.forward = new Vector3(Camera.main.transform.forward.x, 0, Camera.main.transform.forward.z);
		//该方法用来控制相机移动,
        CameraControlS.Instance.BeginAnimatorCameraObj(skillanimatorcamera.transform.GetChild(0).gameObject);
		Destroy(skillanimatorcamera,时间);//多久时间后删除该物体
        yield return null;        
        //屏幕特效
        //GameObject tempFXscr = 实例化特效
        if (tempFXscr != null)
        {
			//设置坐标
            tempFXscr.transform.position = Camera.main.transform.position + Camera.main.transform.forward;
            tempFXscr.transform.rotation = Camera.main.transform.rotation;
            tempFXscr.transform.parent = Camera.main.transform;
            tempFXscr.transform.localScale = Vector3.one;
            tempFXscr.AddComponent();
            等待全屏效果播放完毕
            if (tempFXscr.GetComponent() != null)
            {
                while (tempFXscr.GetComponent().GetCurrentAnimatorStateInfo(0).normalizedTime < 0.9f) yield return null;
            }
            else
            {

                for (int i = 0; i < 60; i++)
                {
                    yield return null;
                    if (skillanimatorcamera == null) break;
                }
            }
        }
        else
        {
            for (int i = 0; i < 60; i++)
            {
                yield return null;
                if (skillanimatorcamera == null) break;
            }
        }
		//将敌人层级还原
        foreach (GameObject o in skilltargettemp)
        {
            UnlitToolS.setLayerName(o, LayerMask.NameToLayer("Character"));
        }
		//删除 SkillCamera和遮罩
        Destroy(tempCamera);
        Destroy(tempmask);
		//还原TimeScale
        WorldMgr.instance.timeScale = 1;
		//还原角色层级,动画播放模式
        UnlitToolS.setLayerName(aitemp.gameObject, layetemp);
        aitemp.GetComponent().updateMode = AnimatorUpdateMode.Normal;
		//可以进行别的特效扩展
        //等待角色技能释放完毕,删除全平特效
        while (!aitemp.gameRoleAnimator.attackEnd)
        {
            yield return null;
        }
        Destroy(tempFXscr);
    }

2 控制相机移动的脚本 CameraControlS 做成单例,挂在MainCamera上


    public void BeginAnimatorCameraObj(GameObject animatorcameraobj)
    {
        animatorCameraObj = animatorcameraobj;
        StopAllCoroutines();
		//得出相机需要移动的距离
        animatorDis = (animatorcameraobj.transform.position - transform.position).magnitude;      
    }
	void LateUpdate()
	{
		if (animatorCameraObj != null)
        {
			//对相机的坐标和旋转做插值计算 
            transform.position = Vector3.Lerp(transform.position, animatorCameraObj.transform.position, animatorCameraforce);
            transform.rotation = Quaternion.Lerp(transform.rotation, animatorCameraObj.transform.rotation, animatorCameraforce *
                (1f - ((animatorCameraObj.transform.position - transform.position).magnitude / animatorDis)));
            return; 
        }
	}


你可能感兴趣的:(【unity3d】【项目】,unity3d,战斗)