Advertisement
mcflybrah

lockingOn

Nov 11th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class lockingOn : MonoBehaviour
  6. {
  7.  
  8.     public List<Transform> targets;
  9.     public Transform selectedTarget;
  10.     public string targetTag = "enemyChest";
  11.     private Transform myTransform;
  12.     //Use this for initialization
  13.     void Start()
  14.     {
  15.         targets = new List<Transform>();
  16.         selectedTarget = null;
  17.         myTransform = transform;
  18.         AddAllEnemies();
  19.     }
  20.  
  21.     public void AddAllEnemies()
  22.     {
  23.        
  24.         //Vector3 myVector3f;
  25.         //myVector3f = new Vector3(0, 3f, 0);
  26.         GameObject[] go = GameObject.FindGameObjectsWithTag(targetTag);
  27.         foreach (GameObject enemy in go)
  28.         {
  29.  
  30.             AddTarget(enemy.transform);
  31.             //AddTarget(enemy.transform + myVector3f);
  32.         }
  33.     }
  34.  
  35.     public void AddTarget(Transform enemy)
  36.     {
  37.         targets.Add(enemy);
  38.     }
  39.  
  40.     private void SortTargetsByDistance()
  41.     {
  42.         targets.RemoveAll(target => target == null);
  43.         targets.Sort(delegate (Transform t1, Transform t2)
  44.         {
  45.             return (Vector3.Distance(t1.position, myTransform.position).CompareTo)
  46.                 (Vector3.Distance(t2.position, myTransform.position));
  47.         });
  48.     }
  49.  
  50.     private void TargetEnemy()
  51.     {
  52.         if (selectedTarget == null)
  53.         {
  54.             SortTargetsByDistance();
  55.             selectedTarget = targets[0];
  56.         }
  57.         else
  58.         {
  59.             int index = targets.IndexOf(selectedTarget);
  60.             if (index < targets.Count - 1)
  61.             {
  62.                 index++;
  63.             }
  64.             else
  65.             {
  66.                 index = 0;
  67.             }
  68.  
  69.             selectedTarget = targets[index];
  70.         }
  71.     }
  72.  
  73.     //Update is called once per frame
  74.     void Update()
  75.     {
  76.         if (Input.GetKeyUp(KeyCode.Tab))
  77.         {
  78.             TargetEnemy();
  79.  
  80.         }
  81.     }
  82.        
  83.    
  84.  
  85.    private void LateUpdate()
  86.    {
  87.         lockOnTab();
  88.        
  89.    }
  90.  
  91.     void lockOnTab()
  92.     {      
  93.                 if (Input.GetKeyUp(KeyCode.Tab))
  94.                 {
  95.  
  96.                     Vector3 myVector;
  97.                     myVector = new Vector3(0, .1f, 0);
  98.                     Vector3 relativePos = selectedTarget.position - transform.position + myVector;
  99.  
  100.                     // the second argument, upwards, defaults to Vector3.up
  101.                     Quaternion rotation = Quaternion.LookRotation(relativePos, myVector);
  102.                     transform.rotation = rotation;
  103.                 }
  104.  
  105.                 if (selectedTarget != null)
  106.                 {
  107.  
  108.                     Vector3 myVector;
  109.                     myVector = new Vector3(0, .1f, 0);
  110.                     Vector3 relativePos = selectedTarget.position - transform.position + myVector;
  111.  
  112.                     // the second argument, upwards, defaults to Vector3.up
  113.                     Quaternion rotation = Quaternion.LookRotation(relativePos, myVector);
  114.                     transform.rotation = rotation;
  115.                 }
  116.  
  117.                 if (Input.GetKeyUp(KeyCode.Q))
  118.                 {
  119.                     selectedTarget = null;
  120.                     return;
  121.                 }
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement