# My turret is a pacifist.

I have the following code - and all the turret does is look at me.

first script - should control the elevation and fire the gun - not sure it is actually doing anything.

``````var shootAngleDistance = 10.0;
var target : Transform = null;
var rotateSpeed : float = 20.0;
var count : int = 0;
var bullitPrefab : Transform;

function Update ()
{
count = count - 1;
if (count < 0)
{
target = null;
count = 30;
}

if (target == null)
{
return;
}
else
{
RotateTowardsPosition(target.position, rotateSpeed);
print ("yay");
}

// If we are almost rotated towards target - fire one clip of ammo
var forward = transform.TransformDirection(Vector3.forward);
var targetDir = target.position - transform.position;
if (Vector3.Angle(forward, targetDir) < shootAngleDistance)
{
var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position, Quaternion.identity);

bullit.gameObject.tag = "enemyProjectile";

}
}

function targetAim (aquiredTarget : Transform)
{
target = aquiredTarget;
}

function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
{
// Compute relative point and get the angle towards it
var relative = transform.InverseTransformPoint(targetPos);
var angle = Mathf.Atan2 (relative.y, relative.z) * Mathf.Rad2Deg;
// Clamp it with the max rotation speed
var maxRotation = rotateSpeed * Time.deltaTime;
var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
// Rotate
transform.Rotate(clampedAngle, 0, 0);
// Return the current angle
return angle;
}
``````

second script - triggered by another with a collision event, of which the transform is sent to this one. This script works - as far as I am aware!

``````var shootAngleDistance = 10.0;
var target : Transform = null;
var rotateSpeed : float = 20.0;
var count : int = 0;

function Update ()
{
count = count - 1;
if (count < 0)
{
target = null;
count = 30;
}

if (target == null)
{
return;
//transform.Rotate(Vector3.up * (Time.deltaTime * 5));
}
else
{
// Rotate towards target
/*
var targetPoint = target.position;
var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
*/
RotateTowardsPosition(target.position, rotateSpeed);

}

// If we are almost rotated towards target - fire one clip of ammo
var forward = transform.TransformDirection(Vector3.forward);
var targetDir = target.position - transform.position;
if (Vector3.Angle(forward, targetDir) < shootAngleDistance)
{
}
}

function targetSighted (aquiredTarget : Transform)
{
target = aquiredTarget;
}

function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
{
// Compute relative point and get the angle towards it
var relative = transform.InverseTransformPoint(targetPos);
var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
// Clamp it with the max rotation speed
var maxRotation = rotateSpeed * Time.deltaTime;
var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
// Rotate
transform.Rotate(0, clampedAngle, 0);
// Return the current angle
return angle;
}

/*

function CanSeeTarget () : boolean
{
if (Vector3.Distance(transform.position, target.position) > attackRange)
{
return false;
}

var hit : RaycastHit;

if (!Physics.Linecast (Vector3(transform.position.x,transform.position.y + 0.5,transform.position.z), target.position,2)) {
return true;
}

//return false;
}

*/
``````

That's a weired script. What's the count good for? I guess that's one of your main problems.

• You count backwards each frame which is kinda crazy to do something framebased.
• The first frame count is 0. You subtract 1 and it becomes -1. Right below you null your target and "reset" your counter to 30. In the next line you leave to whole Update function because the target is null.
• Your 30 frames counter will null your target every 30 frames and since you don't set/reset the counter somewhere else it happens EVERY 30 frames.
• The second script do the same crazy things and in addition you have two functions (targetSighted, CanSeeTarget ) that seems to be important for the whole process but in your code you don't call them anywhere. I guess you don't posted this part.

I'm pretty sure that your two target-nulling-counters will break your whole script(s). It would be essential how you actually get a target.

// If we are almost rotated towards target - fire one clip of ammo
var forward = transform.TransformDirection(Vector3.forward);
var targetDir = target.position - transform.position;
if (Vector3.Angle(forward, targetDir) < shootAngleDistance)
{
var bullit = Instantiate(bullitPrefab ,transform.Find(“spawnPoint”).transform.position, Quaternion.identity);

``````        bullit.gameObject.tag = "enemyProjectile";