Objects refuses to rotate for no reason

Hi!

I am making a TD (Tower Defense) game and I am having trouble with my turrets. They won’t rotate correctly. The rotate a bit but they only rotate from 0 to 60 on the Y axis. They should rotate 360 degrees. Why do they do something like this?

Here is my huge chunk of code:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(SphereCollider))]
public class Turret : MonoBehaviour
{
	public enum TargetEnemy { Ground, Air };
	public TargetEnemy targetEnemy;

	public GameObject projectile;
	public float damage;
	public float reloadTime;
	public GameObject muzzleEffect;
	public Transform[] muzzlePositions;
	public Transform turretBody;

	public Transform target;

	float currentFireTime;

	void Start()
	{
		currentFireTime = reloadTime;

		SphereCollider sp = GetComponent<SphereCollider>();
		sp.isTrigger = true;
	}

	// Update is called once per frame
	void Update()
	{
		currentFireTime += 1 * Time.deltaTime;

		if (target)
		{
			Vector3 lookDirection = new Vector3(-target.transform.position.x, 0, -target.transform.position.z);
			Quaternion lookRotation = Quaternion.LookRotation(lookDirection);

			turretBody.transform.rotation = lookRotation;

			if (currentFireTime >= reloadTime)
			{
				Shoot();
			}
		}
	}

	void Shoot()
	{
		currentFireTime = 0;

		foreach (Transform shooter in muzzlePositions)
		{
			Instantiate(projectile, shooter.transform.position, shooter.transform.rotation);
			Instantiate(muzzleEffect, shooter.transform.position, shooter.transform.rotation);
		}

		Projectile ps = projectile.GetComponent<Projectile>();
		ps.damage = damage;
	}

	void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.tag == "Enemy-Ground")
		{
			target = other.transform;
		}
	}

	void OnTriggerStay(Collider other)
	{
		if (other.gameObject.tag == "Enemy-Ground")
		{
			target = other.transform;
		}
	}

	void OnTriggerExit(Collider other)
	{
		if (other.gameObject.transform == target)
		{
			target = null;
		}
	}
}

If you need any other information, please tell me.

Thanks in advance!

I believe your problem is in line 36:

 Vector3 lookDirection = new Vector3(-target.transform.position.x, 0, -target.transform.position.z);

This line of code would only work if 1) your turret was at the origin, and 2) if you constructed your turret backwards. Assuming you constructed your turret the expected way (front facing positive ‘z’ when the rotation is (0,0,0)), replace your line 36 with the following two lines of code:

 Vector3 lookDirection = target.transform.position - turretBody.transform.position;
 lookDirection.y = 0.0f;

Vector3 lookDirection = new Vector3(-target.transform.position.x, 0, -target.transform.position.z);

This line doesn’t look right to me. What if the target’s position is positive? You can try the built-in LookAt function. Might be easier to understand.

turretBody.transform.LookAt( target );