There are many tutorials on the flight algorithm of missiles. The simple algorithm is simply to get the current position of the target point, and then the missile moves toward the target direction. The advanced point is to get the collision point by calculating and then move toward the target. If you can understand this advanced algorithm, you can see the original post: http://game.ceeger.com/forum/read.php?tid=3919
It is important to note that there are errors in the original posts. And some methods are unreasonable to use. Here is my integrated code, welcome to come up with different views.
To achieve the "interception" function of the missile, it is necessary to calculate the predicted point of intersection according to the velocity, position, velocity and position of the target object. The missile then moves toward the collision point.
Because the target may do irregular movement, it is necessary to calculate the average speed of the object. That is speed = distance/time. The direction of the object is the current position-the previous position. The following is a specific code for calculating the speed and direction of an object:
using unityengine;using system.collections;public class Speedtest:monobehaviour {private Floa T Lasttime; Private Vector3 Lastpos; private float dtime; [Hideininspector] public Vector3 currentvector; [Hideininspector] public float speed,//Update is called once per frame void onenable () {lasttime = Ti Me.time; Lastpos = transform.position;} void Update () {dtime = time.time- lasttime; if (dtime > 0 ) { Lasttime = time.time; Speed = phycismath.getspeed (lastpos, transform.position, dtime); Currentvector = Phycismath.getdir (lastpos, transform.position), if (Mathf.abs (speed) <0.001f ) { Currentvector = transform. Transformdirection (Vector3.forward); } Lastpos = transform.position;}}
The above is the speed and direction of the calculation by displacement, independent of the physical effect, so has a better applicability, can be used for irregular smooth motion calculation. For the sake of intuitive code, some common methods are encapsulated in a static method class.
UsingUnityengine;usingSystem.collections;public classphycismath{public static float getspeed (Vector3 Lastpos,vector3 newps,floatTime ) {if (time = = 0) return 0; Return Vector3.distance (Lastpos, newps)/Time ,} public static Vector3 Getdir (Vector3 lastpos, Vector3 newps) {R Eturn (Newps- lastpos). Normalized; public static float Getdelta (float a,float b,float c) {return b * b-4 * a * c;} public static float Getrad (float dis, float angle) {return-(2 * dis * MATHF.COS (angle * Mathf.deg2ra d)); } public static float Getpom (float A, float b) {return 1-Mathf.pow (A, b);} public static float Getsqrtofmath (flo At A,float B, float d) {Float A1 = (-B + mathf.sqrt (d))/(2 * a); float a2 = (-b-mathf.sqrt (d))/(2 * a); Return A1>A2? A1:a2;} Public Vector3 Gethitpoint () {return Vector3.zero;}}
The next is to write a radar that gets the location of the collision point through a series of "complex" operations.
UsingUnityengine;usingSystem.collections;public classRadarofrocket:monobehaviour {//Our missiles ' orbital calculations are based on Transform,//Pure math calculations, so more accurate, more adaptable to public Transform target;//target p Rivate SpeedTest rocketspeed;//PrivateSpeedTest Targetspeed; PrivateVector3 TargetDir; Private floatAngle Private floatDistence; private bool Isaim = False; public boolIsaim {get {returnIsaim; } set {Isaim =Value }} PrivateVector3 Aimpos; PublicVector3 Aimpos {get {return}Aimpos; } set {Aimpos =Value }} voidChecktarget () {if (!) ( Rocketspeed=getcomponent<speedtest>())) {gameobject.addcomponent<speedtest>(); Rocketspeed = getcomponent<speedtest>(); } if (target&&! ( Targetspeed = target. Getcomponent<speedtest>())) {target.gameobject.addcomponent<speedtest>(); Targetspeed = target. Getcomponent<speedtest>(); }} voidOnenable () {checktarget ();} voidUpdate () {if (target) Testaim ();} public void Testaim () {if (Mathf.abs (targetspeed.speed) < 0.01f ) {//object is too small, the default object is still. Isaim = True ; aimpos = target.position;} else {targetDir = transform.position- target.po sition; Angle = Vector3.angle (targetDir, targetspeed.currentvector); distence = targetdir.magnitude; float a = Phycismath.getpom ((Rocketspeed.speed/targetspeed.speed), 2 ), float B = Phycismath.getrad (distence, Angle ); float c = distence * distence; float d = Phycismath.getdelta (A, B, c); isaim = d >= 0 &&!float. IsNaN (d) &&!float . Isinfinity (d); If (isaim) {float R = Phycismath.getsqrtofmath (A, B, d); if (R < 0) Isaim = false;//If a negative value is obtained, the intersection is incorrect Aimpo s = target.transform.position + Targetspeed.currentvector * r;}} }}
The solution in the original blog is to get the smaller one. But as far as I can test, the target point is correct only when the solution is positive. You can also test them. It is worth noting that the missile must be faster than the target, or the missile will not be close to the target.
OK, the code to get the target point is complete. Then the missile's flight code. In this part, the general practice is to achieve the trajectory of the missile by moving + steering. The code is simple. The distance and angle of the filter I have omitted, basically the novice can write out.
UsingUnityengine;usingSystem.Collections; [Requirecomponent (typeof(Radarofrocket))] public classMissile:monobehaviour {PrivateRadarofrocket Radar; public float speed = 100 public Float rotespeed = 3 , public float Noise = 0 ; void onenable () {radar = Getcomponent<ra Darofrocket> ();} void Update () {Fly (); if (radar. Isaim) {Flytotarget (radar. aimpos- transform.position); }} private void flytotarget (Vector3 point) {if (point! = Vector3.zero) {quaternion missilerotation = Quaternion.lookrotation (Point, vector3.up); Transform.rotation = Quaternion.slerp (transform.rotation, missilerotation, Time.deltatime * RoteSpeed);}} private void Fly () {Move (Transform.forward.normalized+transform.right*mathf.pingpong (time.time,0.5f) *noise, speed* time.deltatime); } public void Move (Vector3 dir,float speed) {transform. Translate (dir* speed,space.world),} void Ontriggerenter (Collider other) {print ("hit" );}
Well, that's all the missile code. Thank you for watching.
The link of this article is: http://www.cnblogs.com/jqg-aliang/p/4768101.html reprint please declare source thank you!
Unity missile algorithm expects target point