I am having this problem where the towers aiming are off it is pretty wierd, and I dont know how to fix it I have been told a few ways i could fix it but i dont understand how i would add it to my code, I am still pretty new to this kind of coding.
I have been told the problem with my code is in this area
if(myTarget)
{
if(Time.time >= nextMoveTime)
{
CalculateAimPosition(myTarget.position);
turretBall.rotation = Quaternion.Slerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
}
if(Time.time >= nextFireTime)
{
FireProjectile();
}
}
}
with the turretBall.rotation
but I just cant fix it.
and here is the whole script if you need it
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.Slerp(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+1700/*+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);
}
}
}
Just as a note here at the end, I am making a Tower Defence game