三类子弹的设计

using UnityEngine;
using System.Collections; public class AI : AssembleModel
{
private Hashtable table;
private Transform target;
void Start()
{
table = new Hashtable();
target = Spawner("Target");//单个创建
}
void Update()
{
//方向
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(mousePos);
iTween.LookUpdate(gameObject, iTween.Hash("looktarget", worldPos, "time", , "axis", "y")); //瞄准图
RaycastHit hit = new RaycastHit();
Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(cameraRay.origin, cameraRay.direction, out hit, ))
{
target.position = hit.point;
} //fire:开火
if (Input.GetMouseButtonDown())
{
// Transform transforms = Spawner("Capsule");//炮弹
// transforms.GetComponent<Bom>().OnPosition(this.transform, target); // Transform transforms = Spawner("bullet");//子弹
// transforms.GetComponent<Emission>().OnPosition(this.transform); Transform transforms = Spawner("daodan");//导弹
transforms.GetComponent<Missile>().OnPosition(this.transform, target);
}
}
//离开碰撞
void OnTriggerExit(Collider other)
{
//如果消失
if (other.transform.name == otherName)
{
touch = false;
otherName = "";
}
table.Remove(other.transform.name);
StartCoroutine(Independent(0.1f));
}
//多物体碰撞 ,当移除后,必须检测已有碰撞中是否还有符合条件的
IEnumerator Independent(float i)
{
if (touch == false) //没碰
{
foreach (DictionaryEntry de in table)
{
//检测碰撞,发现导入方法
//加入
Transform transform = de.Value as Transform;
if (OnDetection(transform))
{
otherName = transform.name;
touch = true;
}
}
}
yield return new WaitForSeconds(i);//n秒执行一次 遍历,提高效率
} private bool touch = false;//和目标物体发生碰撞没有
private string otherName;//目标物体
//进入碰撞
void OnTriggerEnter(Collider other)
{
table.Add(other.transform.name, other.transform); if (this.transform.GetComponent<Attribute>().att == )
{
//测试用
print(other.transform.name);
print(table.Count);
}
if (touch == false) //没碰
{
foreach (DictionaryEntry de in table)
{
//检测碰撞,发现导入方法
//加入
Transform transform = de.Value as Transform;
if (OnDetection(transform))
{
otherName = other.transform.name;
touch = true;
}
}
}
}
//检测碰撞
bool OnDetection(Transform tr)
{
if (tr.name != transform.name)//碰点不是自己
{
//这里写方法判断逻辑
if (tr.GetComponent<Attribute>().att != this.transform.GetComponent<Attribute>().att)//不同属性对打,相同属性 不打
{
return true;
}
else
{//重新选择敌人
return false;
}
}
return false;
} //逗留碰撞
void OnTriggerStay(Collider other)
{
if (other.transform.name == otherName)
{
//检测距离
float distance = Vector3.Distance(this.transform.position, other.transform.position);//距离公式 //根据距离 发射子弹,
if (this.transform.name == "momo(Clone)001")//测试用
{
this.transform.LookAt(other.transform);
// print(this.transform.name + "发射" + otherName);//发射等通知 或 发射不等
}
}
} }
using UnityEngine;
using System.Collections;
//子弹轨迹
public class Emission : MonoBehaviour { // Use this for initialization
void Start () { }
private Transform transforms;
public void OnPosition(Transform vec)
{
transforms = vec;
this.transform.position = new Vector3(transforms.position.x, 1.5f, transforms.position.z);
this.transform.rotation = transforms.rotation;
this.transform.rigidbody.velocity = transforms.TransformDirection(Vector3.forward * );
}
void Update () { }
}
using UnityEngine;
using System.Collections;
//导弹
public class Missile : AssembleModel
{
public void OnPosition(Transform transforms, Transform target)
{
missileSpeed = 31f;//子弹速度
missileRotateSpeed = 2f;//子弹方向
missile = transforms;
man = target;
if (man != null && missile != null)
{
float manWidth = man.GetComponent<MeshFilter>().mesh.bounds.size.x * man.localScale.x;
float missileLength = missile.GetComponent<MeshFilter>().mesh.bounds.size.z * missile.localScale.z;
collisionDistance = manWidth / + missileLength / ;
transform.position = transforms.position;
}
}
private Transform man;
private Transform missile;
private float missileSpeed;
private float missileRotateSpeed;
bool whehterShooted = false;
float distance;
float collisionDistance; // Update is called once per frame
void Update()
{
if (missile != null)
{
distance = Vector3.Distance(this.transform.position, man.position); ////导弹朝向人 法一
// transform.LookAt(man.transform); //导弹朝向人 法二
Quaternion missileRotation = Quaternion.LookRotation(man.transform.position - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, missileRotation, Time.deltaTime * missileRotateSpeed);
transform.rotation = missileRotation; //导弹朝向人 法三
//Vector3 targetDirection = man.transform.position - missile.transform.position;
//float angle = Vector3.Angle(targetDirection,missile.transform.forward);//取得两个向量间的夹角
//print("angle:"+angle.ToString());
//if (angle > 5)
//{
// missile.transform.Rotate(Vector3.up, angle);
//} transform.Translate(Vector3.forward * Time.deltaTime * missileSpeed);
//检测是否发生碰撞。这里通过两者的distance来判断
if (distance <= collisionDistance)
{
Despawn(transform);
}
}
}
}
using UnityEngine;
using System.Collections;
//炮弹轨迹
public class Bom : AssembleModel
{
public float speed = ;
private float distanceToTarget;
private bool move = true; void Start()
{ }
private Vector3 thisVector;//本地坐标
private Vector3 otherVector;//目标坐标 private Transform transforms1;
private Transform target1;
public void OnPosition(Transform transforms, Transform target)
{
transforms1 = transforms;
target1 = target;
thisVector = transforms.position;
otherVector = target.position;
distanceToTarget = Vector3.Distance(thisVector, otherVector); Vector3 ve = new Vector3();
ve = thisVector;
ve.y += ;
this.transform.position = ve;
StartCoroutine(Shoot()); } IEnumerator Shoot()
{
move = true;//因为是协成,会导致 异步改变
while (move)
{
Vector3 targetPos = otherVector;
this.transform.LookAt(targetPos);
float angle = Mathf.Min(, Vector3.Distance(transforms1.position, target1.position) / distanceToTarget) * ;
this.transform.rotation = this.transform.rotation * Quaternion.Euler(Mathf.Clamp(-angle, -, ), , );
float currentDist = Vector3.Distance(this.transform.position, otherVector);
// print("currentDist" + currentDist + "=="+targetPos);
if (currentDist < 0.5f)
{
move = false; CleanUp();
}
else
{
this.transform.Translate(Vector3.forward * Mathf.Min(speed * Time.deltaTime, currentDist));
}
yield return null;
}
} void CleanUp()
{
Despawn(this.transform);//单个删除
}
}

AI 人工智能 探索 (三)的更多相关文章

  1. AI 人工智能 探索 (四)

    在写之前,先对昨天寻路插件再做一些补充,因为该插件不是很完善,所以当我发现有不能满足需求的时候,就会试图更改源代码,或增加接口来符合我的需求. 昨天补充了一条是 自身转向代码,今天补充另外一条,是及时 ...

  2. AI 人工智能 探索 (六)

    这次我为 角色 attribute 添加了 多个属性 其中 att 是 好人 坏人 等属性, 显然 数字不同 就要打起来. grade 是智商属性 ,今天先做了 3的智商.也就是小兵智商.碰到就打 逃 ...

  3. AI 人工智能 探索 (五)

    我们把做好的 角色 拖到 内存池,如图所示,这样我们可以动态生成角色并给予他 寻路目标. //逗留碰撞 void OnTriggerStay(Collider other) { if (other.t ...

  4. AI 人工智能 探索 (七)

    我简单画了一幅图,来讲下 ai 中的设计模式.图形中的这些人物,我想大家都能看的明白. 当 盗贼出现,人们发现了他们,就 呼叫 主类,然后主类再  通知 下面对应的管理局,然后管理局再 分配人手过去 ...

  5. AI 人工智能 探索 (二)

    完整被动技能代码 using UnityEngine; using System.Collections; public class AI : MonoBehaviour { private Hash ...

  6. AI 人工智能 探索 (十)

    呼叫事件图形结构如下 蓝色代表 警察局 红色代表警察 黄色代表 死亡人 蓝色球代表呼救人 黑色代表 敌人 警察目标是 攻击 黑色人,但 路中 会碰到 黄色人,如果警察有 救人功能 则会先救人去医院再看 ...

  7. AI 人工智能 探索 (九)

    链接:http://pan.baidu.com/s/1c0AM3g0 密码:uccw 今天补充 创建物体 移动物体 ,当点击创建后 ,会出来一个 上图的 ui,他跟随 物体,当你把物体拖动到 指定的地 ...

  8. AI 人工智能 探索 (八)

    绑定下,用来释放内存 布局框架.链接:http://pan.baidu.com/s/1eQzSXZO 密码:25ir 这次 我采用 ngui 来设定界面.除工具栏模块外,其他各类ui模块都是 内存池动 ...

  9. AI 人工智能 探索 (一)

    碰撞检测 //逗留碰撞 void OnTriggerStay (Collider other) { if (other.transform.name == name) { //检测距离 //根据距离 ...

随机推荐

  1. Oberon程序设计语言简介

    Oberon奥伯龙是一种通用编程语言,也是一种同名操作系统(由Oberon语言开发,且参考过贝尔实验室的新一代网络操作系统Plan9),是由原Pascal程序设计语言的发明者Niklaus Wirth ...

  2. 使用 mulan-1.5.0 如何构造.arff文件

    1. 为什么要使用mulan 我用mulan来做多标签数据的分类,但是mulan的输入数据由两个文件控制,一个是data.arff文件,这个文件列出的所有的属性以及这些属性值的类型和他们对应的值.la ...

  3. chapter9_4 非抢占式的多线程

    协同程序与常规的多线程不同之处:协同程序是非抢占式的. 当一个协同程序运行时,是无法从外部停止它的.只有当协同程序显式地调用yield时,它才会停止. 当不存在抢先时,编程会变得简单很多,无须为同步的 ...

  4. chapter8_4 错误处理

    在Lua中,对于大多数程序都不用作任何错误处理,应用程序本身会负责这类问题. 所有的Lua活动都是由应用程序的一次调用开始的,这类调用要求Lua执行一个程序块. 执行过程中发生了错误,此调用会返回一个 ...

  5. mysql sql语句大全(转载)

      1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 ...

  6. 行列转换之静态、动态、PIVOT方法

    /* 标题:普通行列转换(version 2.0) 作者:爱新觉罗.毓华  时间:2008-03-09 地点:广东深圳 说明:普通行列转换(version 1.0)仅针对sql server 2000 ...

  7. 通过实例来理解ajax

    点击一个按钮,然后将信息显示到指定的div内. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"& ...

  8. ios隐藏键盘的方式简单应用

    iOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我们可以实现点击键盘以外的空白区域来将键盘隐藏, ...

  9. 算法入门(C++)

    iostream,这个头文件里有很多常用的函数,比如swap交换两个变量的值,max求两个值的最大值等. cstdio头文件,这个头文件里包含C风格的输入输出.如果你之前学习过C++语言应该知道cin ...

  10. DOM操作-遍历一个元素的所有属性

    代码: <!DOCTYPE html> <html> <head> <title>遍历打印一个元素的所有属性</title> <met ...