找回密码
 立即注册
查看: 5998|回复: 68

[AI] Unity3D AI人工智能(坦克模型自动寻怪,自动追击)

[复制链接]
发表于 2012-11-12 15:20 | 显示全部楼层 |阅读模式
[code=javascript]
var Enemy_tank: GameObject; //定义坦克
var firepoint: Transform; //定义开火点
var bullet: Rigidbody; //定义子弹
var bullspeed: int=50; //定义子弹速度
var attackRange = 100.0; //定义距离
var target : Transform;//定义共计目标
//static var value : float ;
private var cun_time: int=0;
function Start () //初始化目标
{
if (target == null && GameObject.FindWithTag("hero")) target = GameObject.FindWithTag("hero").transform;
}
function Update ()
{
//Tank Fire
// cun_time++;
// if(cun_time==260)
// {
// var clone : Rigidbody;
// clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);
// clone.velocity = transform.TransformDirection (Vector3.right*bullspeed);
// cun_time=0;
// }
//distance from A and B
//angle from A and B
//var targetDir = transform.position-other.position;
//var right = transform.right;
//var angle = Vector3.Angle(targetDir, right);
//print("angle is ="+angle);//if (angle < 5.0)//transform.Rotate(Vector3.up*0.5);
//var speed = 0.1;
//transform.rotation =Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);


Enemy_tank=GameObject.Find("Enemy_barrel"); //实例化
if (target == null)
return;
if (!CanSeeTarget ())
return;
// Rotate towards target
var targetPoint = target.position;
var targetRotation = Quaternion.LookRotation (targetPoint - Enemy_tank.transform.position,Vector3.up); //求出与目标之间的角度
Enemy_tank.transform.rotation = Quaternion.Slerp(Enemy_tank.transform.rotation, targetRotation, Time.deltaTime * 2.0); //旋转之角度与目标对齐
//If we are almost rotated towards target - fire one clip of ammo
var forward = Enemy_tank.transform.TransformDirection(Vector3.forward);
var targetDir =Enemy_tank.transform.position-target.position; //求出2者之间的距离
if((Vector3.Angle(forward, targetDir) <10.0)||((Vector3.Angle(forward, targetDir) >-10.0))) //这边是自动开炮的效果
{
cun_time++;
if(cun_time==260)
{
var clone : Rigidbody;
clone = Instantiate(bullet, firepoint.transform.position, firepoint.transform.rotation);
clone.velocity = Enemy_tank.transform.TransformDirection (Vector3.forward*bullspeed);
cun_time=0;
}
}
if((Vector3.Distance(transform.position, target.position) < attackRange)) //发现目标而且在范围之内,开始追击目标
{
var targetPoint1 = target.position;
//Calculation the angle with target
var targetRotation1 = Quaternion.LookRotation (targetPoint1 - transform.position,Vector3.up);
//Rotated Itself then make a line with target
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation1, Time.deltaTime * 1.0);
transform.Translate(Vector3.forward*0.05);
}
}
function CanSeeTarget (): System.Boolean //计算是否被发现
{
if (Vector3.Distance(transform.position, target.position) > attackRange) return false;
var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit))
return hit.transform == target; return false;
}[/code]

评分

参与人数 2鲜花 +1 +1 收起 理由
零零七 + 1
symoncabc + 1

查看全部评分

发表于 2012-12-5 12:50 | 显示全部楼层
不错,正研究这一块
发表于 2013-2-24 11:26 | 显示全部楼层
看不懂说的都是些什么。
发表于 2014-5-27 11:27 | 显示全部楼层
学习了 学习了 好东西
发表于 2017-4-20 11:21 | 显示全部楼层
很不错
发表于 2017-4-20 11:44 | 显示全部楼层
真心顶
发表于 2017-4-20 11:54 | 显示全部楼层
难得一见的好帖
发表于 2017-4-20 11:05 | 显示全部楼层
说的非常好
发表于 2017-4-20 11:26 | 显示全部楼层
不错不错
发表于 2017-4-28 18:23 | 显示全部楼层
很不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-12-22 15:50 , Processed in 0.103746 second(s), 30 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表