Turret always changing to the next target.

Problem: The turret I’ve created keeps aiming and firing at the next new target. When a set of enemies come through the collider, it fires at the first one to come through, then aims for the second one as soon as it comes through.

This is the script I’ve created from the turret:

#pragma strict

var myProjectile : GameObject;
var reloadTime : float = 1f;
var turnSpeed : float = 5f;
var firePauseTime : float = .25f;
var muzzleEffect : GameObject;
var myTarget : Transform;
var muzzlePosition : Transform[];
var turretBall : Transform;

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


function Start () {

}

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 == myTarget)
	{
		myTarget = null;
	}
	
	if(other == myProjectile)
	{
		
		Destroy(myProjectile);
	
	}
	
}

function CalculateAimPosition(targetPos : Vector3)
{
	
	desiredRotation = Quaternion.LookRotation(myTarget.position - turretBall.position);
}

function FireProjectile()
{
	nextFireTime = Time.time+reloadTime;
	nextMoveTime = Time.time+firePauseTime;
	
	for(theMuzzlePos in muzzlePosition)
	{
		var newBall = Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
		newBall.GetComponent(CannonBallScript).myTarget = myTarget;
		Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
	}
}

You are setting myTarget each time an Enemy enters your trigger, that’s why. I suggest you to use a (System.Collections.Generic.)List of transform that you increase in OnTriggerEnter instead of a single myTarget, so in your Update myTarget should be the first item of the list.

How would I go about implementing that?

Here is a sample implementation in C#:

List<Transform> myTargets = new List<Transform>();
// .....
void OnTriggerEnter (Collider other) {
    if (other.gameObject.tag == "Enemy") {
        // ...
        myTargets.Add(other.transform);
    }
}
// .....
void OnTriggerExit (Collider other) {
    if (myTargets.Contains(other.transform)) {
        myTargets.Remove(other.transform);
    }
    // ...
}
// .....
void Update () {
	if (myTargets.Count > 0) {
		Transform myTarget = myTargets[0];
		if (myTarget != null) {
			 // ... do what you need with myTarget (eventually passing it to your functions)
		}
	}
}

I think I’ve got it up to a point.

function Update () {
	
	if (myTarget.Count > 0){
	
	//Transform myTarget = myTargets[0];

	if(myTarget != null)
	{
		if(Time.time >= nextMoveTime)
		{
			CalculateAimPosition(myTarget.position);
			turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed);
		}
		
		if(Time.time >= nextFireTime)
		{
			FireProjectile();
		}
	}
	
}

The line commented out returns me with an error -

Assets/Project Work/Scripts/CannonScript.js(29,18): UCE0001: ‘;’ expected. Insert a semicolon at the end.

Looks like you missed close bracket

Where, though? I’ve scanned and scanned and still can’t find it. I must be blind.

You aren’t closing the
if (myTarget.Count > 0) {

I see now! I am blind. Thank you!

Okay, I added another curly brace and the error still exists. Frustration at it’s highest.