So, before I tried to use lists, I had my turret working correctly. It would aim at targets and deal ‘damage’. However, I discovered that it would instantly switch to the next object which entered into its ‘range’, meaning only a set amount of damage could be dealt to the first one, before moving onto the next one.
A member suggested I used a list to fix this.
I tried.
I broke it, and I need assistance.
import System.Collections.Generic;
var myProjectile : GameObject;
var reloadTime : float = 1f;
var turnSpeed : float = 5f;
var firePauseTime : float = .25f;
var muzzleEffect : GameObject;
var myTargets : List.<Transform> = new List.<Transform>();
var muzzlePosition : Transform[];
var turretBall : Transform;
var myTarget : Transform;
var counter : int = 0;
private var nextFireTime : float;
private var nextMoveTime : float;
private var desiredRotation : Quaternion;
function Start () {
}
function Update ()
{
if (counter > 0)
{
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();
}
}
}
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "Enemy")
{
nextFireTime = Time.time+(reloadTime*.5);
myTargets.Add(other.transform);
counter++;
}
}
function OnTriggerExit(other : Collider)
{
if(myTargets.Contains(other.transform))
{
myTargets.Remove(other.transform);
counter--;
}
}
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 = myTargets[0];
Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
}
}