Avtopushka 2D and how to figure it out?

Hi all. Looking for video avtobuska in 2D and in the end almost no video tutorials or they are in 3D. Of course I do not interfere to alter them to your own project, but there is a caveat which I can't solve it.
public class AI : MonoBehaviour {
[Header ("Parameters")]
public Transform target;

public Transform Tower;
public float speedTurn = 5f;
public float range = 10f;

public string enemyTag = "Enemy";

private void Start()
{

 InvokeRepeating("UpdateTarget", 0f, 0.5 f);
}

void Update()
{

 if (target == null)
return;

 Vector3 direction = target.position - transform.position;
 Quaternion lookRotation = Quaternion.LookRotation(direction);
 Vector3 rotation = Quaternion.Lerp(Tower.rotation, lookRotation, Time.deltaTime * speedTurn).eulerAngles;
 Tower.rotation = Quaternion.Euler(0f, 0f, rotation.z); 



}

void UpdateTarget()
{

 GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag); 
 shortesDistance float = Mathf.Infinity;
 NearesEnemy GameObject = null;
 foreach (GameObject enemy in enemies)
{
 distanceToEnemy float = Vector3.Distance(transform.position, enemy.transform.position);
 if (distanceToEnemy < shortesDistance)
{
 shortesDistance = distanceToEnemy;
 nearesEnemy = enemy;
}
}

 if (nearesEnemy != null && shortesDistance <= range)
{
 target = nearesEnemy.transform;
}
else
{
 target = null;
}
}

private void OnDrawGizmosSelected()
{
 Gizmos.color = color.blue;
 Gizmos.DrawWireSphere(transform.position, range);
}


}

The problem is that "gun" is "enemy" at close distance, and it begins to turn on him.
June 8th 19 at 17:04
0 answer

Find more questions by tags Unity Game Engine