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!