Problem with homming missile targeting system

Hi Im making a multiplayer game and I created a weapon that fires homming missiles the problem is my missiles always go to the same target or if that target is dead the next not the closest target, here is my script:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class RocketProjectile : Projectile {
    private List<TankController> Targets = new List<TankController>();
    private Vector3 Direction;

    public override void AutoMove(){
        transform.LookAt(Direction);
        Vector3 Velocity = Speed * Time.deltaTime * Vector3.forward;
        transform.Translate(Velocity);
  
    }

    void FixedUpdate(){
        Targets.OrderByDescending(i => transform.position - i.gameObject.transform.position);
        Direction = Targets[Targets.Count()-1].transform.position;
  
  
    }

    public void FindTargets(){
        TankController[] TempArray = GameObject.FindObjectsOfType<TankController>();
        foreach (TankController _Player in TempArray.ToList().Where(i => i._PlayerProfile.Name != Name)) {
            Targets.Add(_Player);
        }
  
  
    }
}

Obviously the problem is : Targets.OrderByDescending(i => transform.position - i.gameObject.transform.position); But how can I get the target that is closest to the missile?

Thank you!.

You should be comparing distance between the two (magnitude), not a directional vector between them. Actually, I have no idea what happens when you try a < or > comparison with Vector3s.

1 Like

Yes, use a distance formula. This one is in 2D The Distance Formula: What it is and how it works | Purplemath. You can do the same in 3D but maybe you would want to ignore the height in the case of missiles. Also there is no need to take the square root because the smallest sum of the squares will be the closest without having to actually compute the true distance and it will be faster.

Hope this helps.

1 Like

Oops I somehow forgot that substracting two vectors was for direction not distance, anyways I end up using Vector3.Distance();

TempTargets = Targets.OrderBy(i => Vector3.Distance(transform.position, i.gameObject.transform.position)).ToList();
Direction = TempTargets[0].transform.position;

Thanks again

If you just want to compare distances (i.e. know which is longer/shorter or whether it exceeds a certain length), use the squared distance instead.
It saves you calculating the square-root, which is an expensive calculation.

// Instead of using this:
Vector3.Distance(v1, v2)
// Use this:
(v2 - v1).sqrMagnitude

The whole thing works because a < b => a² < b² :wink:
Same result, faster code.

1 Like

Thank a lot, the game is for console so Im always struggling to meet the target FPS (Im aiming for 60 FPS)

You are doing something else wrong then. Hitting 60 FPS on a console shouldn’t be massively difficult. Mobile is another story.

The game is for WiiU and Its a Split screen Multiplayer game, when playing 4 players I have to render the scene x4, there is no way to avoid the performance hit of four cameras rendering at the same time. I have unity pro and I use the profiler so I know that rendering is the only thing that has a significant footprint. I have done every optimization possible, including object pooling so I know the game is as fast as is ever gonna get and it runs 60 FPS almost all the time but sometimes when there are too many particles emitters on screen playing on all four cameras it drops to like 45FPS, I would like it too run at 60 FPS all the time but the drop its not noticeable at all. Its just that Im a little bit obsessed with trying to squish as much as possible of the console…

I call bs on that one.
Consoles aren’t very powerful. Not at all. Just look at the headlines regarding games on the current gen consoles.
Tell me how many games run at 1080p@60fps.

I’ll wear that. It just surprises me.