敌人
敌人一共有两种,一种再陆地上前进,另一种会飞行。玩家击杀敌人会获得金钱。
1、陆地上的敌人
1、创建敌人的脚本Enemy.cs。它的代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
//当前的路点
public Pathnode m_currentNode;
//敌人的生命
public int m_life = 15;
//敌人的最大生命
public int m_maxLife = 15;
//敌人的移动速度
public float m_Speed = 2.0f;
//敌人的死亡事件
public System.Action<Enemy> onDeath;
//旋转目标
public void RotateTo()
{
var position = m_currentNode.transform.position - transform.position;
//仅旋转y轴
position.y = 0;
//获得目标旋转度数
var targetRotation = Quaternion.LookRotation(position);
//获得中间旋转角度
float next = Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, 120 * Time.deltaTime);
//旋转
transform.eulerAngles = new Vector3(0, next, 0);
}
public void MoveTo()
{
Vector3 pos1 = this.transform.position;
Vector3 pos2 = m_currentNode.transform.position;
float dist = Vector2.Distance(new Vector2(pos1.x, pos1.z), new Vector2(pos2.x, pos2.z));
//如果到达路点
if