项目文件找出来了,老版本的脚本有报错,我在新版2019.4.21f1c1下解决了报错,战斗场景可以正常跑的。
需要的同学点下面地址下载(关注就行啦不用积分),祝大家都早日学成
————————————————————————
上一篇文章里实现了较为初级的回合制战斗系统,仅限与1v1的战斗,且目标固定,比较low,昨晚又研究了一种进阶的回合制战斗。
中级篇
回合制战斗系统实现效果简介
1. 多目标战斗,不管你放多少个战斗单位都OK(只要给参战单位设置相应的tag,PlayerUnit或EnemyUnit);
2. 加入了攻击速度排序,初始读取参战单位时会对列表进行一次出手排序;
3. 玩家手动选择技能及攻击目标:先在UI上选择技能(影响伤害系数),再通过射线选择攻击目标;
4.实时血条,单位头顶显示血条并实时更新;
5.战败界面及小动画,使用UGUI做了个结束动画(为方便,战败和战胜用了同一个)
准备工作:
1. 还是先准备模型资源
下载自AssetStore,资源名:Animated Knight and Slime Monster(免费)
下载自AssetStore,资源名:Toon RTS Units - Demo(免费)
2. 场景添加模型并为模型添加Animator
我从模型中选出了骑士作为玩家角色,小僵尸作为怪物,分别添加了待机、攻击、受击、死亡动画片段
(这几步和初级篇实现一致)
3. 为参战单位添加tag
玩家单位设置为PlayerUnit,怪物单位设置为EnemyUnit
顺便把场景中的位置和相机视角调整到比较合理的位置,可以参考截图角度
4. 创建空物体BattleManager和BattleUIManager
分别用于挂载回合控制脚本和血条UI脚本
5. 之前漏了关于血条预制体的说明
创建一个Image命名为“BloodBar”作为血条底图,下面包含2个子物体:
BloodFill,Image类型作为血条(红色会变化的图),这张图的锚点设置为(0,0.5),并设置Image Type为Filled(后面的脚本可以通过修改FillAmout直接改变长度)
OwnerName,Text类型,用于显示血条主人的名字
然后给血条上添加一个脚本BloodUpdate(),脚本内容如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BloodUpdate : MonoBehaviour {
public GameObject owner;
private Image ownerBloodFill;
private BattleUIManager uiManager;
private Vector3 playerBlood3DPosition;
private Vector2 playerBlood2DPosition;
void Start()
{
//显示血条主人的名字
Text ownerText = gameObject.transform.Find("OwnerName").GetComponent<Text>();
ownerText.text = owner.name;
//获取UI控制脚本的引用
uiManager = GameObject.Find("BattleUIManager").GetComponent<BattleUIManager>();
}
void Update()
{
if (owner.tag=="PlayerUnit" || owner.tag == "EnemyUnit")
{
//更新血条长度
ownerBloodFill = gameObject.transform.Find("BloodFill").GetComponent<Image>();
ownerBloodFill.fillAmount = owner.GetComponent<UnitStats>().bloodPercent;
//更新血条位置
playerBlood3DPosition = owner.transform.position + new Vector3(uiManager.bloodXOffeset, uiManager.bloodYOffeset, uiManager.bloodZOffeset);
playerBlood2DPosition = Camera.main.WorldToScreenPoint(playerBlood3DPosition);
gameObject.GetComponent<RectTransform>().position = playerBlood2DPosition;
}
if (owner.GetComponent<UnitStats>().IsDead())
{
gameObject.SetActive(false);
}
}
}
添加完脚本后把血条拖到Prefabs文件夹中生成为预制体。
</