Search Unity

Tab Targeting

Discussion in 'Scripting' started by Wolfsshadow, Aug 2, 2015.

  1. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    Good Evening All!

    I've got some code here that is not having any compiler errors, but it's not functioning correctly. I am able to tab target (somewhat reliably), however, my text stating my distance from the target will either function if I manually drag my target into the inspector, or give me a "AnotherTransform has not been assigned". This makes sense as I can physically see it empty, but I've tried to give it a null setting, I've tried to wait until I can tab target, and none of them seem to work unless I put a specific object in there first. So I get tab targeting, but my distance is always to the object I placed in there in the inspector. From "private void ShowSelectedTarget" down is really where I think the issue is that I can't seem to mix around any which way with any success. Any and all help would be most appreciated!



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class targeting : MonoBehaviour
    6. {
    7.     public Transform OneTransform;
    8.     public Transform AnotherTransform;
    9.     public UnityEngine.UI.Text distLabel; // <-- Assign the Text element in the Inspector view.
    10.     public List<Transform> targets;
    11.    
    12.  
    13.     public Transform selectedTarget;
    14.     public string targetTag = "Targetable";
    15.    
    16.  
    17.     private Transform myTransform;
    18.  
    19.     void Start ()
    20.     {
    21.         targets = new List<Transform>();
    22.         selectedTarget = null;
    23.  
    24.         myTransform = transform;
    25.  
    26.         AddAllTargetables();
    27.     }
    28.    
    29.     public void AddAllTargetables()
    30.     {
    31.         GameObject[] go = GameObject.FindGameObjectsWithTag("Targetable");
    32.        
    33.         foreach(GameObject enemy in go)
    34.         {
    35.             AddTarget(enemy.transform);
    36.         }
    37.     }
    38.    
    39.     public void AddTarget(Transform enemy)
    40.     {
    41.         targets.Add(enemy);
    42.     }
    43.    
    44.  
    45.     private void SortTargetsByDistance()
    46.     {
    47.         targets.Sort(delegate(Transform t1, Transform t2)
    48.                      {
    49.             return (Vector3.Distance(t1.position, myTransform.position).CompareTo)
    50.                 (Vector3.Distance(t2.position, myTransform.position));
    51.         });
    52.     }
    53.    
    54.     private void TargetTargetable()
    55.     {
    56.         if(selectedTarget == null)
    57.         {
    58.        
    59.             SortTargetsByDistance();
    60.             selectedTarget = targets[0];
    61.         }
    62.         //// EDIT: Add this code; now we can jump 2 all the found enemy's by pressing TAB
    63.         else
    64.         {
    65.             int index = targets.IndexOf(selectedTarget);  
    66.            
    67.             // because it starts at 0 so that's acually already 1
    68.             if (index < targets.Count -1)
    69.             {
    70.                 index++;
    71.             }
    72.             // if we are at the LAST enemy in ar found list, jump back to the first enemy in the list
    73.             else
    74.             {
    75.                 index = 0;  
    76.             }
    77.    
    78.             ShowDeSelectedTarget();
    79.            
    80.             selectedTarget = targets[index];
    81.         }
    82.    
    83.         ShowSelectedTarget();
    84.     }
    85.    
    86.     private void ShowSelectedTarget()
    87.     {
    88.  
    89.         Transform AnotherTransform = selectedTarget.FindChild ("Name");
    90.         Transform name = selectedTarget.FindChild ("Name");
    91.  
    92.         if (name == null) {
    93.             Debug.LogError ("Could not find the Name on " + selectedTarget.name);
    94.             return;
    95.         }
    96.  
    97.         name.GetComponent<TextMesh> ().text = selectedTarget.GetComponent<ring>().name;
    98.         name.GetComponent<MeshRenderer> ().enabled = true;
    99.            
    100.     }
    101.    
    102.  
    103.     private void ShowDeSelectedTarget()
    104.     {
    105.         selectedTarget.FindChild("Name").GetComponent<MeshRenderer> ().enabled = false;
    106.     //    name.GetComponent<MeshRenderer> ().enabled = false;
    107.         selectedTarget = null;
    108.     }
    109.  
    110.     void Update ()
    111.     {
    112.         if (OneTransform) {
    113.     //        Transform AnotherTransform = selectedTarget;
    114.             float dist = Vector3.Distance(OneTransform.position, AnotherTransform.position);
    115.             distLabel.text = dist.ToString("Tgt: 0.0ft");
    116.         }
    117.         if(Input.GetKeyDown(KeyCode.Tab))
    118.         {
    119.             Transform AnotherTransform = selectedTarget;
    120.             TargetTargetable();
    121.         }
    122.     }
    123. }
     
  2. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    Anybody have any ideas? Do I need to provide more information? Narrow something down? Please let me know what you need in order to help my helplessness