飞行敌人鹰
1.添加敌人鹰到场景
新建空节点EnemyEagle
在EnemyEagle下添加Sprite鹰,命名为EagleSprite,设置Tag为Enemy
添加碰撞检测Circle Collider 2D,设置半径和偏移值,使在鹰的正中
2.添加鹰的动画
在EnemyEagle上,新建动画Eagle,保存在Animations/Enemies文件夹下
点击红色按钮录制,切换到EagleSprite上,分别在如图的帧上改变图片,完成后停止录制
3.新建脚本FlyingEnemyController
将脚本添加组件到EnemyEagle节点上
添加参数移动点、速度、当前点,在Update()控制移动(与MovingPlatform.cs类似)
public Transform[] points; //移动点
public float moveSpeed; //移动速度
private int currentPoint; //当前点
public SpriteRenderer theSR;
void Start()
{
//初始设置points点父节点为null,使其在场景下,EnemyEagle移动时相对位置
for (int i = 0; i < points.Length; i++)
{
points[i].parent = null;
}
}
void Update()
{
//鹰的位置
transform.position = Vector3.MoveTowards(transform.position, points[currentPoint].position, moveSpeed * Time.deltaTime);
if (Vector3.Distance(transform.position, points[currentPoint].position) < 0.05f)
{
currentPoint++;
if (currentPoint >= points.Length)
{
currentPoint = 0;
}
}
}
在Unity中,EnemyEagle下添加两个空节点Point_1和Point_2,调整相对位置,并绑定到对应的points移动点中
绑定EagleSprite到theSR上,并设置速度
在Update()后,添加设置老鹰移动时翻转
//设置翻转
if(transform.position.x < points[currentPoint].position.x)
{
theSR.flipX = true;
}
else if(transform.position.x > points[currentPoint].position.x)
{
theSR.flipX = false;
}
4.攻击角色
在FlyingEnemyController.cs中,添加参数
public float distanceToAttckPlayer; //攻击敌人距离
public float chaseSpeed; //攻击移动速度
在Update()中,判断距离来攻击Player
void Update()
{
if(Vector3.Distance(transform.position, PlayerController.sInstance.transform.position) > distanceToAttckPlayer)
{
//鹰的位置
transform.position = Vector3.MoveTowards(transform.position, points[currentPoint].position, moveSpeed * Time.deltaTime);
if (Vector3.Distance(transform.position, points[currentPoint].position) < 0.05f)
{
currentPoint++;
if (currentPoint >= points.Length)
{
currentPoint = 0;
}
}
//设置翻转
if (transform.position.x < points[currentPoint].position.x)
{
theSR.flipX = true;
}
else if (transform.position.x > points[currentPoint].position.x)
{
theSR.flipX = false;
}
}
else
{
transform.position = Vector3.MoveTowards(transform.position,
PlayerController.sInstance.transform.position,
chaseSpeed * Time.deltaTime);
}
}
5.优化
在FlyingEnemyController.cs中,添加参数
private Vector3 attackTarget; //攻击目标的位置
public float waitAfterAttack; //攻击后等待时间
private float attackCounter; //等待时间计数器
攻击角色时,else
加入设置attackTarget值,并使鹰目标移动位置为attackTarget
//攻击角色
if (attackTarget == Vector3.zero)
{
attackTarget = PlayerController.sInstance.transform.position;
}
transform.position = Vector3.MoveTowards(transform.position, attackTarget, chaseSpeed * Time.deltaTime);
判断移动到目标位置后,重置倒计时为等待时间,并重置attackTarget为zero(加在上面代码之后)
if (Vector3.Distance(transform.position, attackTarget) <= 0.1f)
{
attackCounter = waitAfterAttack;
attackTarget = Vector3.zero;
}
在不攻击时,也设置还原位置为zero,在if (Vector3.Distance(transform.position, PlayerController.sInstance.transform.position) > distanceToAttckPlayer)
里面开端
//不攻击角色时,还原位置
attackTarget = Vector3.zero;
最后,加入等待计数器判断,以及动态改变其值
if(attackCounter > 0)
{
attackCounter -= Time.deltaTime;
}
else
{
//调整里面所有代码到else里
//...
}
6.伤害角色
在EnemyEagle节点上,加入Circle Collider 2D,设置为Trigger,并调整大小,使其在EagleSprite碰撞体(其为角色攻击范围)之下,用于攻击角色
同时,添加脚本组件DamagePlayer.cs