Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with homming missile targeting system

Discussion in 'Scripting' started by raycosantana, Jan 8, 2015.

  1. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5.  
    6. public class RocketProjectile : Projectile {
    7.     private List<TankController> Targets = new List<TankController>();
    8.     private Vector3 Direction;
    9.  
    10.     public override void AutoMove(){
    11.         transform.LookAt(Direction);
    12.         Vector3 Velocity = Speed * Time.deltaTime * Vector3.forward;
    13.         transform.Translate(Velocity);
    14.  
    15.     }
    16.  
    17.     void FixedUpdate(){
    18.         Targets.OrderByDescending(i => transform.position - i.gameObject.transform.position);
    19.         Direction = Targets[Targets.Count()-1].transform.position;
    20.  
    21.  
    22.     }
    23.  
    24.     public void FindTargets(){
    25.         TankController[] TempArray = GameObject.FindObjectsOfType<TankController>();
    26.         foreach (TankController _Player in TempArray.ToList().Where(i => i._PlayerProfile.Name != Name)) {
    27.             Targets.Add(_Player);
    28.         }
    29.  
    30.  
    31.     }
    32. }
    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!.
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    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.
     
    raycosantana likes this.
  3. Johnny-Photon

    Johnny-Photon

    Joined:
    Sep 4, 2011
    Posts:
    84
    Yes, use a distance formula. This one is in 2D http://www.purplemath.com/modules/distform.htm. 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.
     
    raycosantana likes this.
  4. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    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
     
  5. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    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.

    Code (csharp):
    1. // Instead of using this:
    2. Vector3.Distance(v1, v2)
    3. // Use this:
    4. (v2 - v1).sqrMagnitude
    The whole thing works because a < b => a² < b² ;)
    Same result, faster code.
     
    raycosantana likes this.
  6. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Thank a lot, the game is for console so Im always struggling to meet the target FPS (Im aiming for 60 FPS)
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You are doing something else wrong then. Hitting 60 FPS on a console shouldn't be massively difficult. Mobile is another story.
     
  8. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    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...
     
    Last edited: Jan 9, 2015
  9. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    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.
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'll wear that. It just surprises me.