简单自制跟踪导弹
来源:互联网 发布:移动宽带网络加速器 编辑:程序博客网 时间:2024/06/03 02:21
实现了跟踪导弹的基本功能,但是如果在复杂的场景中,比如有山,有树的场景,人与导弹如果之间隔着一个山,导弹就直接穿过山的模型去追人了,我想让导弹能绕开障碍物追人,要用光源(射线检测)
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public GameObject man;
public GameObject missile;
public float manSpeed = 6f;
public float missileSpeed = 4f;
public float missileRotateSpeed = 2f;
bool whehterShooted = false;
float distance;
float collisionDistance;
// Use this for initialization
void Start () {
if (man!=null&&missile!=null)
{
float manWidth = man.GetComponent<MeshFilter>().mesh.bounds.size.x*man.transform.localScale.x;
float missileLength = missile.GetComponent<MeshFilter>().mesh.bounds.size.z * missile.transform.localScale.z;
print("manWidth:"+manWidth.ToString()+",missileLength:"+missileLength.ToString());
collisionDistance = manWidth / 2 + missileLength / 2;
}
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.LeftArrow))
{
man.transform.Translate(Vector3.left*Time.deltaTime*manSpeed);
}
if (Input.GetKey(KeyCode.RightArrow))
{
man.transform.Translate(-Vector3.left * Time.deltaTime * manSpeed);
}
if (Input.GetKey(KeyCode.UpArrow))
{
man.transform.Translate(Vector3.forward * Time.deltaTime * manSpeed);
}
if (Input.GetKey(KeyCode.DownArrow))
{
man.transform.Translate(-Vector3.forward * Time.deltaTime * manSpeed);
if (Input.GetKey(KeyCode.Z))
{
man.transform.Translate(Vector3.up * Time.deltaTime * manSpeed);
}
if (Input.GetKey(KeyCode.C))
{
man.transform.Translate(-Vector3.up * Time.deltaTime * manSpeed);
}
}
if (Input.GetKeyDown(KeyCode.S))//按下S 发射导弹
{
whehterShooted = true;
}
if (whehterShooted&&missile!=null)
{
distance = Vector3.Distance(man.transform.position, missile.transform.position);
////导弹朝向人 法一
//missile.transform.LookAt(man.transform);
//导弹朝向人 法二
Quaternion missileRotation = Quaternion.LookRotation(man.transform.position - missile.transform.position, Vector3.up);
//missile.transform.rotation = Quaternion.Slerp(missile.transform.rotation, missileRotation, Time.deltaTime * missileRotateSpeed);
missile.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);
//}
missile.transform.Translate(Vector3.forward * Time.deltaTime * missileSpeed);
//检测是否发生碰撞。这里通过两者的distance来判断
if (distance<=collisionDistance)
{
Destroy(missile);
}
}
}
}
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public GameObject man;
public GameObject missile;
public float manSpeed = 6f;
public float missileSpeed = 4f;
public float missileRotateSpeed = 2f;
bool whehterShooted = false;
float distance;
float collisionDistance;
// Use this for initialization
void Start () {
if (man!=null&&missile!=null)
{
float manWidth = man.GetComponent<MeshFilter>().mesh.bounds.size.x*man.transform.localScale.x;
float missileLength = missile.GetComponent<MeshFilter>().mesh.bounds.size.z * missile.transform.localScale.z;
print("manWidth:"+manWidth.ToString()+",missileLength:"+missileLength.ToString());
collisionDistance = manWidth / 2 + missileLength / 2;
}
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.LeftArrow))
{
man.transform.Translate(Vector3.left*Time.deltaTime*manSpeed);
}
if (Input.GetKey(KeyCode.RightArrow))
{
man.transform.Translate(-Vector3.left * Time.deltaTime * manSpeed);
}
if (Input.GetKey(KeyCode.UpArrow))
{
man.transform.Translate(Vector3.forward * Time.deltaTime * manSpeed);
}
if (Input.GetKey(KeyCode.DownArrow))
{
man.transform.Translate(-Vector3.forward * Time.deltaTime * manSpeed);
if (Input.GetKey(KeyCode.Z))
{
man.transform.Translate(Vector3.up * Time.deltaTime * manSpeed);
}
if (Input.GetKey(KeyCode.C))
{
man.transform.Translate(-Vector3.up * Time.deltaTime * manSpeed);
}
}
if (Input.GetKeyDown(KeyCode.S))//按下S 发射导弹
{
whehterShooted = true;
}
if (whehterShooted&&missile!=null)
{
distance = Vector3.Distance(man.transform.position, missile.transform.position);
////导弹朝向人 法一
//missile.transform.LookAt(man.transform);
//导弹朝向人 法二
Quaternion missileRotation = Quaternion.LookRotation(man.transform.position - missile.transform.position, Vector3.up);
//missile.transform.rotation = Quaternion.Slerp(missile.transform.rotation, missileRotation, Time.deltaTime * missileRotateSpeed);
missile.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);
//}
missile.transform.Translate(Vector3.forward * Time.deltaTime * missileSpeed);
//检测是否发生碰撞。这里通过两者的distance来判断
if (distance<=collisionDistance)
{
Destroy(missile);
}
}
}
}
阅读全文
0 0
- 简单自制跟踪导弹
- 跟踪导弹轨迹的向量计算
- 自动跟踪导弹算法(未整理)
- 自制简单浏览器界面
- 自制简单分数类
- 自制简单Trie树
- 导弹
- 导弹
- 导弹
- 导弹
- 导弹
- 自制控件的简单例程
- 自制简单的Lambert光照
- Unity3D模拟导弹轨迹的简单方法
- LG>P1020导弹拦截~~A简单版
- 简单的执行跟踪
- 简单的跟踪算法
- 自制
- 学习android Activity的setContentView
- 创建第一个Shell脚本
- JQuery EasyUI知识点总结
- js基础-单体内置对象(Global、Math)
- 1072. 开学寄语(20)
- 简单自制跟踪导弹
- 离线下载
- Maven搭建SSH框架案例(三)-----测试框架是否可用
- Unity抛物线(根据时间)
- 路由的插入页面 子页面和主页面通用一个控制器
- 机器学习中的范数规则化之(一)L0、L1与L2范数
- python学习第一天
- 228. Summary Ranges
- LimeSDR实现小型FM广播台