public static Vector2 Patrol(GameObject target, humanBody body, AIController aiController, int speed) { //如果有巡逻点 并且无目标时进行巡逻操作 Vector2 position = new Vector2(0, 0); int size = aiController.patrolDot.Length; //记录巡逻点的个数 if (target == null && size > 0) //无目标且巡逻个数大于0 { //向这个巡逻点靠近 position += AITool.ApproachOrAway(aiController.patrolDot[aiController.patrolState], body, aiController.faceDistance, speed, 1); //Debug.Log("qwe"); //如果和巡逻点无限靠近了,则patrolstate++ float dis = (aiController.patrolDot[aiController.patrolState].transform.position - body.transform.position).magnitude; //Debug.Log(size-1); if (dis < 1) { int waitTime = aiController.patrolDot[aiController.patrolState].GetComponent <patrolDot>().waitTime; //获取巡逻节点的等待时间 if (aiController.timer < waitTime) { aiController.timer++; } else { if (aiController.patrolState < size - 1) { aiController.patrolState++; } else { //回到起点 aiController.patrolState = 0; } aiController.timer = 0; //计时器归零 } } } return(position); }
private Vector2 UpdateInfo(Vector2 position) { position += AITool.Patrol(body.target, body, this, body.speed); position += AITool.ApproachOrAway(body.target, body, faceDistance, body.speed, 1); body.DetectGround(); position += body.Gravity(Vector2.zero); //因为跳跃是要连续使用的函数,所以放在update中 position += AITool.Jump(body, jumpHeight); if (body.target != null) { AITool.RandomAttack(body, 5); AITool.IfLost(body.target, body, lostDistance); } return(position); }
public static Vector2 ApproachOrAway(GameObject target, humanBody body, float faceDistance, int speed, int e) { //e参数为1时表示靠近目标,为0时表示远离目标 Vector2 position = new Vector2(0, 0); float distance = 0; //根据目标的类型来决定要靠多近,如果是巡逻点则为0即可 //当有目标后靠近目标或者远离 if (target != null) { //靠近还是远离由这个参数决定 int leftX = 0; int rightX = 1; if (e == 1) { //靠近 leftX = 0; rightX = 1; } else if (e == 0) { //远离 leftX = 1; rightX = 0; } //如果是玩家,则要根据faceDirection来决定靠的距离 if (target.layer == LayerMask.NameToLayer("Player")) { //表示目标是玩家 distance = faceDistance; //Debug.Log("目标为玩家"); } //Debug.Log("正在调用"); //判断目标在左边还是右边来靠近 if (body.transform.position.x - target.transform.position.x >= 0.9 + distance) { // Debug.Log(body.transform.position.x - target.transform.position.x); //向左 position += AITool.Move(body, leftX, speed); } else if (target.transform.position.x - body.transform.position.x >= 0.9 + distance) { //Debug.Log(target.transform.position.x - body.transform.position.x); //向右 position += AITool.Move(body, rightX, speed); } else //表示背对着的时候 分别为两个方向背对的时候 { //前面有讲到distance不为0时表示目标为玩家,此时进行转向的判定 //表示目标为人 if (distance != 0) { //humanBody targetBody = target.GetComponent<humanBody>(); if (((target.transform.position.x < body.transform.position.x && body.direction == 1) || (target.transform.position.x > body.transform.position.x && body.direction == 0))) { position += AITool.ApproachOrAway(target, body, -0.9f, speed, e); } else { body.StopWalk(); } //if (targetBody.direction == body.direction) //{ // //表示要转向了. // //再调用一次,这次肯定会转向 //} } else { //目标为物体 body.StopWalk(); } } } return(position); }