Unity3D导弹追踪目标(三维效果)
2017-09-15 16:04
330 查看
网上搜到的大多是用Quaternion.LookRotation(),不知道为什么完全打不准目标(也许是因为要用在2D?),懒人已经哭晕在厕所,为了完成目标,不得不自己写(苦逼),此记结思路和结果。 主要追踪过程在update里面。
下图为最基本的效果,实际上飞机可以运行,导弹可以进行追踪,由于gif制作不熟练,只体现基本效果。
飞机模型和导弹模型都不属于我,属于我的只有代码。
使用球坐标进行定位,使用z轴的旋转角度表示俯仰角,y轴的旋转角度表示在xz平面的方位角。根据目标与导弹的位置调整导弹的姿态。
以下为代码:
下图为最基本的效果,实际上飞机可以运行,导弹可以进行追踪,由于gif制作不熟练,只体现基本效果。
飞机模型和导弹模型都不属于我,属于我的只有代码。
导弹追踪移动目标
追踪理论(导弹的姿态)使用球坐标进行定位,使用z轴的旋转角度表示俯仰角,y轴的旋转角度表示在xz平面的方位角。根据目标与导弹的位置调整导弹的姿态。
以下为代码:
using UnityEngine; using System.Collections; public class missile3 : MonoBehaviour { public GameObject target; public GameObject missile; // Use this for initialization private const float Max_Rotation = 180; private static float Max_Rotation_frame = Max_Rotation / ((float)(Application.targetFrameRate == -1 ? 60 : Application.targetFrameRate));//每帧最大转动角度。 void Start () { target = GameObject.Find ("F-15E"); missile=GameObject.Find ("missile2"); } // Update is called once per frame void Update () { float dx = target.transform.position.x - this.transform.position.x; float dy = target.transform.position.y - this.transform.position.y; float dz = target.transform.position.z - this.transform.position.z;//目标与导弹位置差 //y轴为 float rotationTheta = Mathf.Atan(dz/dx)*180/Mathf.PI;//在dx,dz的旋转角度 float dxz = Mathf.Sqrt(d 4000 z * dz + dx * dx); float rotationPhi = Mathf.Atan(dy/dxz)*180/Mathf.PI;//俯仰角 if (dx < 0) { rotationTheta = rotationTheta > 0 ? 360 - rotationTheta : -rotationTheta; } else { rotationTheta=180-rotationTheta; }//在世界坐标中的,在xz平面的旋转. rotationPhi = 90 - rotationPhi;//俯仰角 float originRotationY = MakeSureRightRotation (transform.eulerAngles.y); float originRotationZ = MakeSureRightRotation (transform.eulerAngles.z); float addRotationY = rotationTheta- originRotationY; float addRotationZ = rotationPhi- originRotationZ; addRotationZ= Mathf.Max (-Max_Rotation_frame, Mathf.Min (Max_Rotation_frame, addRotationZ));//每frame可旋转的角度 addRotationY= Mathf.Max (-Max_Rotation_frame, Mathf.Min (Max_Rotation_frame, addRotationY)); this.transform.eulerAngles = new Vector3 (0,MakeSureRightRotation(this.transform.eulerAngles.y + addRotationY), MakeSureRightRotation(this.transform.eulerAngles.z + addRotationZ)); this.transform.Translate (new Vector3 (0, 2.0f * Time.deltaTime, 0));//导弹运行方向,根据自身坐标的y轴运行. } //碰撞触发爆炸 void OnCollisionStay(Collision collision){ Debug.Log ("S"); GameObject boom = Resources.Load ("boom object") as GameObject; Object.Instantiate (boom,this.transform.position,this.transform.rotation); Destroy (this.gameObject); Debug.Log ("E"); } //确保导弹在0到360度之间. private float MakeSureRightRotation(float rotation){ rotation += 360; rotation %= 360; return rotation; } }
相关文章推荐
- Unity 3D追踪效果的实现 目标箭头指引
- Unity 3D追踪效果的实现 目标箭头指引
- Opencv目标追踪Kalman结合camshift特定情况下效果不错
- Unity 3D追踪效果的实现 目标箭头指引
- Unity3D之飞机游戏追踪导弹制作
- [Unity3D]Unity3D游戏开发之从《魂斗罗》游戏说起(上)——目标追踪
- 目标追踪综述
- 三维翻动效果的jquery特效代码
- 圆形目标中心点在tof相机坐标系下的三维坐标的计算(2)
- [unity基础教程]史上最简单效果最好的unity3D分辨率适应教程!(版本已经更新2013.5.3
- Unity3D实现闪光灯效果
- unity3d Human skin real time rendering with blood and water drop effect真实模拟人皮实时渲染之血液和水珠掉落效果
- Unity3D中目标相对自身的前后左右方位判断
- Unity3D ShaderLab <六>通过改变纹理的UV坐标实现简单的水流效果
- QT+OpenCv实现在410c开发板上实现视频目标追踪预研——(二)粒子滤波算法测试
- wpf中给一个三维模型设置多种材质效果
- unity3d Crease效果分析
- Unity3D研究院之处理摄像机与角色之间被挡时的局部透明效果
- unity3d Vignetting效果分析
- QT+OpenCv实现在410c开发板上实现视频目标追踪预研-(四)Opencv环境的安装与配置