人气 115

[游戏程序] 一段简单的AI示例,UDK和Unity3D通用 [复制链接]

九艺网 2017-3-4 00:52:49

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x


这是一段控制僵尸向玩家移动的脚本,AI很sb但很有效。我将对unity3d引擎和UDK引擎的AI脚本编写做一个简单的编程介绍。
先是一段UDK引擎下的脚本示例
class TestZombieBot extends GameAIController;
var Pawn thePlayer; //variable to hold the target pawn
var name SeenPlayerAnim;
simulated event PostBeginPlay()
{
    super.PostBeginPlay();
}
event SeePlayer(Pawn SeenPlayer)   //bot sees player
{
    if (thePlayer ==none)   //if we didnt already see a player
    {
        thePlayer = SeenPlayer; //make the pawn the target
        TestZombiePawn(Instigator).FullBodyAnimSlot.PlayCustomAnim(SeenPlayerAnim);
        GoToState('Follow'); // trigger the movement code
    }
}
state Follow
{
Begin:
    if (thePlayer != None)   // If we seen a player
    {
        MoveTo(thePlayer.Location); // Move directly to the players location
        GoToState('Looking'); //when we get there
    }
}
state Looking
{
Begin:
    if (thePlayer != None)   // If we seen a player
    {
        MoveTo(thePlayer.Location); // Move directly to the players location
        GoToState('Follow');  // when we get there
    }
}
default(此处打断是防止自动论坛表情替换代码字符)properties
{
    Begin Object class=AnimNodeSequence Name=SeenPlayerAnim
        bCauseActorAnimEnd=true
        End Object
        AnimSets(0)=AnimSet'MyModContents.AnimSet.Zombie'
        Animations=SeenPlayerAnim
        SeenPlayerAnim=Fury
}
aff2ea1e2fa796492fa561db0f557b57.gif
TestZombieBot.uc(1.25 KB, 下载次数: 289)


以上这段脚本就是怪物攻击玩家的最简单形式,傻傻地跑向玩家,然后攻击玩家。很多游戏都是使用这样的AI,如果你希望你的AI不变成sb,可以参考另一篇文章http://bbs.game798.com/showtopic-141617-1.html

    接着介绍Unity3d引擎下的脚本编写。其实程序是相通的,还是按上面那串代码来说,需要改几个变量,首先是玩家坐标(thePlayer.Location),在Unity3D里面没有这个现成的变量,但是你也可以做一步转化获得玩家坐标thePlayer.Location=GameObject.FindWithTag("Player").transform.position
MoveTo函数的具体形式在Unity3D引擎中可以这样写
function MoveTo(position : Vector3)
{
var direction = position - transform.position;
direction.y = 0;
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
var forward = transform.TransformDirection(Vector3.forward);
var speedModifier = Vector3.Dot(forward, direction.normalized);
speedModifier = Mathf.Clamp01(speedModifier);
direction = forward * speed * speedModifier;
GetComponent (CharacterController).SimpleMove(direction);
}
如果你要跑动有声音,那么把这段插到MoveTo函数里面就可以
if (Time.time > SoundReloadTime +lastSound) {
     if (Sound)
            AudioSource.PlayClipAtPoint(Sound, transform.position);
      lastSound = Time.time;
}
这样就有脚步声了。
Unity3D下更复杂的AI脚本请看另外两个实例
http://bbs.game798.com/showtopic-133226-1.html
http://bbs.game798.com/showtopic-132155-1.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

QQ|手机版|小黑屋|九艺游戏动画论坛 ( 津ICP备2022000452号-1 )

GMT+8, 2024-5-10 16:34 , Processed in 0.148360 second(s), 26 queries .

Powered by Discuz! X3.4  © 2001-2017 Discuz Team.