Hello i have this code im trying to change them with a single sprite, just to make the sensation that the tower is rotating to the target, problem is like the tower only detects top right,top left but detects the rightdown and right left like the first two.
public class Turret : MonoBehaviour
{
[Header(“Atributes”)]
public Transform target;
public float range = 4f;
public float fireRate = 1f;
public float fireCountdown = 0;
[Header(“Dont touch”)]
public string enemyTag = “Enemy”;
public SpriteRenderer spriteManager;
public Sprite[ ] sprites= new Sprite[6];
public Transform turret;
public float rotationSpeed = 4f;
public Transform firePosition1;
public Transform firePosition2;
public GameObject bulletPrefab;
private Vector3 enemiPos;
[SerializeField] private FieldofView fieldofView;
[SerializeField] private Transform origin;
// Start is called before the first frame update
void Start()
{
InvokeRepeating(“UpdateTarget”, 0f, 0.5f);
}
void UpdateTarget()
{
GameObject[ ] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
float shortestDistance = Mathf.Infinity;
GameObject nearestEnemy = null;
foreach(GameObject enemy in enemies)
{
float distancetoEnemy = Vector3.Distance(transform.position, enemy.transform.position);
if(distancetoEnemy < shortestDistance)
{
shortestDistance = distancetoEnemy;
nearestEnemy = enemy;
}
if(nearestEnemy != null && shortestDistance <= range)
{
target = nearestEnemy.transform;
enemiPos = nearestEnemy.transform.position - transform.position;
}
else
{
target = null;
}
}
}
// Update is called once per frame
void Update()
{
if (target == null)
return;
Vector3 dir = target.position - transform.position;
//Quaternion lookRotation = Quaternion.LookRotation(dir);
// Vector3 rotation = Quaternion.Lerp(turret.rotation,lookRotation,Time.deltaTime* rotationSpeed).eulerAngles;
// turret.rotation = Quaternion.Euler(0f, 0f, rotation.z);
if(fireCountdown <= 0f)
{
Shoot();
fireCountdown = 1f / fireRate;
}
fireCountdown -= Time.deltaTime;
float arco;
arco = Mathf.Acos(enemiPos.x) * 180 / Mathf.PI;
Debug.Log(arco + " Arco Actual");
if (arco <30 && arco >=330)
{
Debug.Log(arco + " Der");
spriteManager.sprite = sprites[5];
}else if(arco<90 && arco >= 30)
{
Debug.Log(arco + " DerUp");
spriteManager.sprite = sprites[4];
}else if(arco<150 && arco>=90)
{
Debug.Log(arco + " IzqUp");
spriteManager.sprite = sprites[3];
}else if(arco<210 && arco >=150)
{
Debug.Log(arco + " Izq");
spriteManager.sprite = sprites[2];
}
else if(arco<270 && arco >= 210)
{
Debug.Log(arco + " IzqDown");
spriteManager.sprite = sprites[1];
}
else if(arco<270 && arco>= 330)
{
Debug.Log(arco + " DerDown");
spriteManager.sprite = sprites[0];
}
/fieldofView.SetOrigin(origin.transform.position);
fieldofView.SetAimDirection(firePosition.transform.position);/
}
void Shoot()
{
//Debug.Log(“Shoot!”);
GameObject bulletGO1 = Instantiate(bulletPrefab, firePosition1.position,Quaternion.identity);
GameObject bulletGO2 = Instantiate(bulletPrefab, firePosition2.position, Quaternion.identity);
Bullet bullet1 = bulletGO1.GetComponent();
Bullet bullet2 = bulletGO2.GetComponent();
if(bullet1 != null && bullet2 != null)
{
bullet1.Seek(target);
bullet2.Seek(target);
}
}
public void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, range);
}
}