您的位置:首页 > 移动开发 > Unity3D

Unity之机器人AI

2015-07-24 16:59 399 查看
接着上篇简单机器人AI的那个,下午搜了下相关的看到“Unity3D研究院之游戏开发中的人工智能AI(三十八)”,便仔细尝试按照他的思路着写了并测试了,效果杠杠的

我就贴自已写的一遍的代码了,作为一个小小小笔记吧!!!

using UnityEngine;
using System.Collections;

/// <summary>
/// 类型
/// </summary>
public enum RobotAIType
{
Gremlins,
Monster,
Boss,
}

/// <summary>
/// 状态
/// </summary>
public enum Robot_Do_type
{
Robot_idle=0,
Robot_run=1,
Robot_pause=2,
Robot_walk=3,
Robot_chasePlayer=4,
Robot_attackPlayer=5
}

public class Robot : MonoBehaviour
{
//怪物
public RobotAIType robotAIType = RobotAIType.Gremlins;
//主角
public GameObject player;
//状态
private Robot_Do_type robotState;
private float robotStopThinkTime;
public float robotThinkingTime = 5f;

Robot_Do_type currentState;

public void Start()
{
robotState = Robot_Do_type.Robot_idle;
StartCoroutine(RobotAI());
}

IEnumerator RobotAI()
{
while (enabled)
{
switch (robotAIType)
{
case RobotAIType.Boss:
BossExecute();
break;
case RobotAIType.Gremlins:
GremlinsExecute();
break;
case RobotAIType.Monster:
MonsterExecute();
break;
}
yield return null;
}
yield return null;
}

private void GremlinsExecute()
{
//判断精灵与主角的距离
if (Vector3.Distance(player.transform.position, transform.position) <= 10f)
{
transform.LookAt(player.transform);
}
}

private void MonsterExecute()
{
if (IsThinking())
{
AIThinking();
}
else
{
UpdateState();
}
}

public void BossExecute()
{

}

bool IsThinking()
{
if (Time.time - robotStopThinkTime >= 3f)
{
robotStopThinkTime = Time.time;
return true;
}
return false;
}

void AIThinking()
{
int r = Random.Range(0, 4);
switch (r)
{
case (int)Robot_Do_type.Robot_idle:
SetRobotState(Robot_Do_type.Robot_idle);
break;
case (int)Robot_Do_type.Robot_pause:
SetRobotState(Robot_Do_type.Robot_pause);
break;
case (int)Robot_Do_type.Robot_run:
SetRobotState(Robot_Do_type.Robot_run);
break;
case (int)Robot_Do_type.Robot_walk:
SetRobotState(Robot_Do_type.Robot_walk);
break;
}
}

void SetRobotState(Robot_Do_type type)
{
if (currentState == type)
return;
currentState = type;
switch (type)
{
case Robot_Do_type.Robot_attackPlayer:
transform.LookAt(player.transform);
Debug.Log("attackPlayer attackPlayer");
break;
case Robot_Do_type.Robot_chasePlayer:
transform.LookAt(player.transform);
Debug.Log("chasePlayer chasePlayer");
break;
case Robot_Do_type.Robot_idle:
Debug.Log("idle idle idle idle");
break;
case Robot_Do_type.Robot_pause:
Debug.Log("pause pause pause pause");
break;
case Robot_Do_type.Robot_run:
Debug.Log("run run run run");
break;
case Robot_Do_type.Robot_walk:
Debug.Log("walk walk walk walk");
break;
}
//如果是播放动画,判断动画是否正在播放,若没播放则播放
}

private void UpdateState()
{
//判断与主角的距离
float distance = Vector3.Distance(player.transform.position, transform.position);
if(distance>=10f)
{
SetRobotState(Robot_Do_type.Robot_idle);
}
else
{
if(distance<3f)
{
SetRobotState(Robot_Do_type.Robot_attackPlayer);
}
else
{
SetRobotState(Robot_Do_type.Robot_chasePlayer);
}
}
}
}


下面是我测试的小图图:

一张0.4M的图为什么上传不上去咧!!!不高兴,不传了

---------------------------------------------------------------------

我的小啊小啊小笔记
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: