John M
John M

Reputation: 23

Lookat targeted enemy and orbit

Previously I had a script that I could look at a player but only when I had the Tab buttoned pressed down but could not cycle through enemies or anything. Looking all day for answers I came by this and decided it would be great I can now find things tagged enemies and cycle though them but I have not been able to figure out a way to lookat the enemy and stay looking at them and be able to move around them in a circle.

public class debug : MonoBehaviour
 {

 public List<Transform> targets;
 public Transform selectedTarget;
 public string targetTag = "Enemy";
 private Transform myTransform;

 //Use this for initialization
 void Start()
 {
     targets = new List<Transform>();
     selectedTarget = null;
     myTransform = transform;
     AddAllEnemies();
 }

 public void AddAllEnemies()
 {
     GameObject[] go = GameObject.FindGameObjectsWithTag(targetTag);
     foreach (GameObject enemy in go)
     {
         AddTarget(enemy.transform);
     }
 }

 public void AddTarget(Transform enemy)
 {
     targets.Add(enemy);
 }

 /*private void SortTargetsByDistance()
 {
     targets.Sort(delegate (Transform t1, Transform t2) {
         return (Vector3.Distance(t1.position, myTransform.position).CompareTo)
             (Vector3.Distance(t2.position, myTransform.position));
     });
 } */
 private void SortTargetsByDistance()
 {
     targets.RemoveAll(target => target == null);
     targets.Sort(delegate (Transform t1, Transform t2) {
         return (Vector3.Distance(t1.position, myTransform.position).CompareTo)
             (Vector3.Distance(t2.position, myTransform.position));
     });
 }

 private void TargetEnemy()
 {
     if (selectedTarget == null)
     {
         SortTargetsByDistance();
         selectedTarget = targets[0];
     }
     else
     {
         int index = targets.IndexOf(selectedTarget);
         if (index < targets.Count - 1)
         {
             index++;
         }
         else
         {
             index = 0;
         }

         selectedTarget = targets[index];
     }
 }

 //Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Tab))
     {
         TargetEnemy();
     }
     }
 }

I am trying to use transform.LookAt(TargetEnemy); in void update but I get an error saying cannot convert from method group to transform. Im really stuck on this now any help is appreciated.

Edit: I am a big dummy I used transform.LookAt(selectedTarget); instead and the character for a split second will turn and lookat the enemy. Now i need to keep the player looking at the target and move around them.

Upvotes: 0

Views: 162

Answers (2)

John M
John M

Reputation: 23

Hello thanks to everyone that looked and thank @SonicBlue22 for your answer I figured it out after a couple more hours.

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Tab))
        {
            TargetEnemy();
            transform.LookAt(selectedTarget);
        }

        if (selectedTarget != null)
        {
            transform.LookAt(selectedTarget);
        }

        if (Input.GetKeyUp(KeyCode.Q))
        {
            selectedTarget = null;
            return;
        }

    }

}

Upvotes: 1

Grant Shotwell
Grant Shotwell

Reputation: 339

Transform.LookAt

You need to pass it a Transform, not void.

TargetEnemy();
transform.LookAt(selectedTarget);

Alternatively, you can make TargetEnemy() return a Transform.:

private Transform TargetEnemy()
 {
     /* stuff */

     return selectedTarget;
 }
transform.LookAt(TargetEnemy());

Upvotes: 1

Related Questions