Offset produced while making tower shoot an object

hello! I was going through cg cookie tutorial for tower defense.
And I followed it step by step!

Tutorial

The objective is : create a turret which will aim at an enemy no matter where you move it !
When my tower shoots though, it keeps missing the object when I move it. It rotates fine. But there is an offset when I shoot the object. It always shoots to the left of the object. At times it even stops rotating to look at the object.

This is the code for the projectile!

#pragma strict
var mySpeed : float = 10;
var myRange : float =10;

private var myDist : float;
 
 
function Update () 
{
	transform.Translate(Vector3.forward * Time.deltaTime * mySpeed);
	myDist += Time.deltaTime * mySpeed;
	if(myDist>= myRange)
	Destroy(gameObject);	 


}

and this is the code for the tower! Help please!

#pragma strict

var myProjectile : GameObject;
var reloadTime : float = 1.0f;
var turnSpeed : float = 5.0f;
var firePauseTime : float = .25f;
//var muzzleEffect : GameObject;
var errorAmount: float = 0;
var myTarget : Transform;
var muzzlePositions : Transform[];
var turretBall : Transform;

private var nextFireTime : float;
private var nextMoveTime : float;
private var desiredRotation : Quaternion;
private var aimError: float;

function 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();
		}
		
	}
}
function OnTriggerEnter(other: Collider)
{
	if(other.gameObject.tag == "Enemy")
	{
		nextFireTime = Time.time + (reloadTime * .5);
		myTarget = other.gameObject.transform;
	}
}

function OnTriggerExit(other : Collider)
{
	if(other.gameObject.transform == myTarget)
	{
		myTarget = null;
	}
}

function CalculateAimPosition(targetPos : Vector3)
{
	var aimPoint = Vector3(targetPos.x+aimError, targetPos.y+aimError, targetPos.z+aimError);
	desiredRotation = Quaternion.LookRotation(aimPoint);
}

function CalculateAimError()
{
	aimError = Random.Range(-errorAmount,errorAmount);
}

function FireProjectile()
{
	nextFireTime = Time.time + reloadTime;
	nextMoveTime = Time.time + firePauseTime;
	CalculateAimError();
	
	for(theMuzzlePos in muzzlePositions)
	{
		Instantiate(myProjectile, theMuzzlePos.transform.position, theMuzzlePos.transform.rotation);
	}
}

I also keep getting an error saying

UnassignedReferenceException: The variable myProjectile of ‘Turret’ has not been assigned.
You probably need to assign the myProjectile variable of the Turret script in the inspector.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:44)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:53)
Turret.FireProjectile () (at Assets/Scripts/Turret.js:81)
Turret.Update () (at Assets/Scripts/Turret.js:37)

I am so sorry if this post is long. I am new to this forum.

found it !

var aimPoint = Vector3(targetPos.x-transform.position.x+aimError, targetPos.y-transform.position.y+aimError, targetPos.z-transform.position.z+aimError);

relative position was not calculated! :slight_smile: