Homing missiles issues : Never hits the target

Hello,
something is incorrect about my code, my homing missiles never hits the target they pass by the target rotate and pass from the side, they end up doing an atom animation … just like this http://bit.ly/1Jb3zrf
this is my code :

Rigidbody2D rb2d;
public float Speed;
public float TTL;
public Vector3 fakeTarget_01;

private GameObject targetEN;
private Vector2 dist;
private Vector2 dir;
private float angle;


void Start () {
    rb2d = GetComponent<Rigidbody2D>();
    targetEN = GameObject.Find("EN_TUT_01");
    Destroy(gameObject, TTL);

    fakeTarget_01 = new Vector3(transform.position.x + 3, transform.position.y, transform.position.z);

}

void Update ()
{
    dir = rb2d.velocity;
    angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    HomingPhysics();
}

public void HomingPhysics()
{
    if (targetEN != null)
    {
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        dist = fakeTarget_01 - transform.position;
        dist = dist.normalized;
        //rb2d.AddForce(fakeTarget_01 * Speed);
        rb2d.AddForce(dist * Speed);
        StartCoroutine(Fly());
    }

}

public IEnumerator Fly()
{
    yield return new WaitForSeconds(0.5f);
    fakeTarget_01 = targetEN.transform.position;
}

This is normal behaviour for seeking type misiles. Once they get too close to the target then there own speed keeps them moving faster then the targeting system can adjust, producing orbit like behaviour.

Typically you would solve this by turning guidance off once you are within a certian distance of the target.

The problem is, you’re trying to hit something while applying force directly to it. Let me give you a different system that actually guides your rockets to target instead blindly ‘gravitating’ towards it like planets.

Let’s divide force into two components: forward acceleration and rotation. Force you apply to rocket is sum of them. Rotation force should always act perpendicular(either left or right) to your speed vector while acceleration force must act to increase speed(forward).

First, we calculate if we need to rotate our speed vector(usually RigidBody2D.velocity) to face target. If we need to rotate our velocity, we apply full force perpendicular to target (making a circular trajectory so that our speed targets where we need). If our velocity is directed almost into enemy, we start ‘rebalancing’ force from rotation into acceleration. So when we’re nearly at set angle, we decrease our perpendicular force and increase forward force so we rotate slower but start to actually accelerate. At some point we don’t need to rotate anymore and we just accelerate at full speed.

If your target is moving, instead of adjusting to target you simply adjust to target position+target velocity * coefficient where coefficient depends on distance from rocket to enemy (limited from maximum though, otherwise you’ll see rockets flying in parallel to enemy sometimes) and is 0 when rocket is very very close to enemy.

Sample code on pastebin

Everything I wrote here is introduction into steering behariours, you might want to look into them.

I have one piece of additional advice: fast moving projectiles should use raycasting instead of physics as even with interpolation mode I’ve seen a lot of projectiles missing target at high speeds even though they should’ve hit them.

Same thing here. I solved using “Transform.Translate” instead of using AddForce.

private void Propel() {
        //m_rb.AddForce(m_rb.transform.up * m_acceleration * Time.fixedDeltaTime, ForceMode2D.Impulse);
        transform.Translate(transform.up * m_acceleration * Time.fixedDeltaTime);
    }