Search Unity

Targetting enemy nearest to middle of screen/y-axis.

Discussion in 'Scripting' started by Topsu, Jan 22, 2014.

  1. Topsu

    Topsu

    Joined:
    Jan 7, 2014
    Posts:
    6
    So, I need my targeting script to make enemy that's y-axis is nearest to Player's z-axis to be set as Player's target.

    So far I have it so that when I press tab it collects all Enemies in the map into an array and sorts that array by distance and tab cycles through them.

    Code (csharp):
    1.     private void SortTargetsByDistance()
    2.     {
    3.         targets.Sort(delegate(Transform t1, Transform t2){
    4.             return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
    5.             });
    This is the code for sorting by distance, but I do not know what to change to make it sort by who's y-axis is nearest to player's z-axis


    Code that returns values from 1 to -1 depending where you are aiming in relation to target's y-axis: 1 = target is straight to front, 0 = target is in left or right side, -1 = behind and few decimals behind that depending on angle where your z-axis is relating to your target's y-axis.


    Code (csharp):
    1.         Vector3 dir = (target.transform.position - transform.position).normalized;
    2.  
    3.         float direction = Vector3.Dot (dir, transform.forward);
    But I have no idea how to use that to compare the elements in array like the sorting by distance.


    Thanks for any help.
     
    Last edited: Jan 22, 2014
  2. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    what you could do is take the enemy positions and set the x and z components to the same as the player and then sort them so this way you are only having the enemy y component.
    so
    Code (csharp):
    1.  Vector3 dir = (target.transform.position - transform.position).normalized;
    would be

    Code (csharp):
    1. Vector3 dir = (new Vector3(transform.position.x,target.transform.position.y,transform.position.z) - transform.position).normalized;
    Or something like that
     
  3. Topsu

    Topsu

    Joined:
    Jan 7, 2014
    Posts:
    6
    Something like that does not does not sound really good, but I will try that out tomorrow.

    So the
    Code (csharp):
    1. Vector3 dir = (new Vector3(transform.position.x,target.transform.position.y,transform.position.z) - transform.position).normalized;
    Should set dir value to what? Number based on if I look at the target?
     
  4. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    I dont know what that means.

    Also when you press tab to collect the enemies into an array sorted by distance then press tab again to cycle through them, what do you mean by cycle through them and why do you do this.

    What I mean is that you should be sorting the array on just the y component of the enemy position so that Transform t1 should be a vector like
    Code (csharp):
    1.  
    2. Transform t1 = new Vector3 (transform.position.x, Transform t1.y, transform.position.z);
    The same for Transform t2
    Code (csharp):
    1.  
    2. Transform t2 = new Vector3 (transform.position.x, Transform t2.y, transform.position.z);
    Then sort those wrt the player transform

    Or you could try this
    Code (csharp):
    1. GameObject FindClosestY()
    2.     {
    3.         GameObject[] gos;
    4.         gos = GameObject.FindGameObjectsWithTag("Enemy");
    5.         GameObject closest = null;
    6.         float distance = Mathf.Infinity;
    7.         Vector3 position = transform.position;
    8.         foreach (GameObject go in gos)
    9.         {
    10.                 Vector3 et = new Vector3 (transform.position.x, go.transform.position.y, transform.position.z);
    11.                 Vector3 diff = et - position;
    12.                 float curDistance = diff.sqrMagnitude;
    13.                 if (curDistance < distance)
    14.                 {
    15.                     closest = go;
    16.                     distance = curDistance;
    17.                 }
    18.         }
    19.         return closest;
    20.     }
    And you can call it with something like:
    Code (csharp):
    1. void Update()
    2.     {
    3.         GameObject ClosestEnemy = FindClosestY();
    4.     }
     
    Last edited: Jan 22, 2014