1-敌人AI的优化,游戏测试
避免两个两个AI相撞在一起
将玩家的初始值设置为 private float v=-1;
在Enemy脚本里添加下面的代码
/// <summary>
/// 这里防止敌人扎堆,两个敌人碰撞
/// </summary>
/// <param name="collision"></param>
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag=="Enemy")
{
timeValChangeDirection = 4;//一旦碰到就转向
}
}
游戏刚开始敌人往下走
private float timeValChangeDirection;//用来改变方向 刚开始没有旋转直接向下移动
2-玩家状态
新建空物体PlayerManager并挂载PlayerManager脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour
{
//属性值
public int lifeValue = 3;
public int playerScore = 0;
public bool isDead;
//引用
public GameObject born;//拿到出生的特效
//单例 全选private static PlayerManager instance;按住Ctrl不松,点击R E可以不全单例
private static PlayerManager instance;
public static PlayerManager Instance
{
get
{
return instance;
}
set
{
instance = value;
}
}
private void Awake()
{
Instance = this;//实例化一下单例
}
void Start ()
{
}
void Update ()
{
if (isDead)
{
Recover();
}
}
private void Recover()
{
if (lifeValue <= 0)
{
//游戏失败,返回主界面
}
else
{
lifeValue--;
GameObject go = Instantiate(born, new Vector3(-2,-8,0),Quaternion.identity);
go.GetComponent<Born>().createPlayer = true;
isDead = false;
}
}
}
在Player脚本中让死亡为true
在敌人Enemy脚本中添加,当击败敌人时获取得分