I am making a tower defence game and one of my towers are having a problem where it aims off to the side, I think it is something with it needs to be in 0,0,0 couse then it can find its target fine.
Here is my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Ground_Turret : MonoBehaviour {
public GameObject myProjectile;
public float reloadTime = 1f;
public float turnSpeed = 5f;
public float firePauseTime = .25f;
public float errorAmount = .001f;
public Transform myTarget;
public Transform[] muzzlePositions;
public Transform turretBall;
private float nextFireTime;
private float nextMoveTime;
private Quaternion desiredRotation;
private float aimError;
public List<Transform> enemiesTarget = new List<Transform>();
void Start (){
}
void Update (){
if(myTarget)
{
if(Time.time >= nextMoveTime)
{
CalculateAimPosition(myTarget.position);
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
}
if(Time.time >= nextFireTime)
{
FireProjectile();
}
}
}
void OnTriggerEnter ( Collider other ){
if(other.gameObject.tag == "Ground Enemy")
{
enemiesTarget.Add(other.gameObject.transform);
nextFireTime = Time.time+(reloadTime*.5f);
myTarget = enemiesTarget[0];
}
}
void OnTriggerExit ( Collider other ){
if(other.gameObject.transform == myTarget)
{
enemiesTarget.Remove(other.gameObject.transform);
myTarget = null;
}
}
void CalculateAimPosition ( Vector3 targetPos ){
Vector3 aimPoint=new Vector3(targetPos.x+aimError, targetPos.y+aimError+1700
, targetPos.z+aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
void CalculateAimError (){
aimError = Random.Range(-errorAmount, errorAmount);
}
void FireProjectile (){
nextFireTime = Time.time+reloadTime;
nextMoveTime = Time.time+firePauseTime;
CalculateAimError();
foreach(Transform theMuzzlePos in muzzlePositions)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
}
}
}