Tower defense, torret rotation for sprites.

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

}
}

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

You may edit your post above.

If you have no idea what your code is doing, fix that first.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong: