0
votes

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.

2
Well you made edit as I was typing my answer, oh well. So now your problem is you need it to keep looking at selectedTarget? How often do you call transform.LookAt(selectedTarget)? Is there anything else modifying the rotation? - SonicBlue22
Yes my problem is that the player does not keep looking at the enemy. I was thinking that if the player did keep looking i could easily just use my character controller to move back forward etc.. expecting weird animations. what am i missing that is making it that as soon as i move the character stops looking at the enemy? - John M

2 Answers

1
votes

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());
1
votes

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;
        }

    }

}