Hello, I’m making a Tower Defense game, and I have used a script from a free package for my turret. The only problem is that the turret attacks one enemy then ignores the rest. And I know this may seem easy to fix but the code is written in Boolean, so if anyone could help I would appreciate it. The turret attacks anything with the tag target.
Here is the turret script
import UnityEngine
class Turret (MonoBehaviour):
public TargetObject as Transform
public RotationSpeed as single
public ElevationSpeed as single // 0 means fixed barrel angle, such as with a missile launcher.
public Barrels as Transform
public ProjectileForce as single
public Projectile as GameObject
public RateOfFire as single
public NumberOfProjectiles as int
public Deviation as single
public Indirect as bool
_barrels as (Barrel)
_origin as Vector3
_init = false
def OnDrawGizmosSelected():
Gizmos.color = Color.cyan
if not _init:
return
for barrel in _barrels:
Gizmos.DrawSphere(barrel.GetProjectileOrigin(), 0.1)
targetPosition = GetTargetPoint()
Gizmos.DrawSphere(targetPosition, 0.5)
Gizmos.DrawSphere(_origin, 0.1)
def Start():
_barrels = GetComponents of Barrel()
for barrel in _barrels:
barrel.Init(self)
_init = true
def FixedUpdate():
if not TargetObject or not _init:
fire = false
for Barrel in _barrels:
Barrel.CeaseFiring()
return
if (targetPoint = GetTargetPoint()) == null:
fire = false
for Barrel in _barrels:
Barrel.CeaseFiring()
return
// Vector that looks at targetPosition. Converted into local space to account for parent transform rotations.
firingVector = transform.parent.InverseTransformPoint(targetPoint) - transform.parent.InverseTransformPoint(_origin)
fire = true
// Rotate on the Y axis to align with firingVector
yRotation = Quaternion.LookRotation(firingVector, transform.parent.up)
yRotation = Quaternion.Euler(0, yRotation.eulerAngles.y, 0)
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, yRotation, RotationSpeed)
if Quaternion.Angle(transform.localRotation, yRotation) > Deviation:
fire = false
if not ElevationSpeed == 0:
// Rotate the barrels on their X axis to align with firingVector
xRotation = Quaternion.LookRotation(firingVector, Vector3.right)
xRotation = Quaternion.Euler(xRotation.eulerAngles.x, 0, 0)
Barrels.localRotation = Quaternion.RotateTowards(Barrels.localRotation, xRotation, ElevationSpeed)
if Quaternion.Angle(Barrels.localRotation, xRotation) > Deviation:
fire = false
if fire:
for Barrel in _barrels:
Barrel.BeginFiring()
else:
for Barrel in _barrels:
Barrel.CeaseFiring()
def GetTargetPoint():
targetPosition = TargetObject.position
// Projectile origin. Is between the (assumed) two barrels.
originDiff = _barrels[0].GetProjectileOrigin() - _barrels[1].GetProjectileOrigin()
_origin = _barrels[0].GetProjectileOrigin() - originDiff * 0.5
// Angle needed to hit the target with the given force.
targetAngle = Ballistics.GetVerticalAim(_origin, targetPosition, ProjectileForce, Indirect)
return null if targetAngle == 0
// solutionTime = CalculateFlightTime(solutionAngle * Mathf.Rad2Deg);
// Vector that points from _origin to targetPosition
targetVector = targetPosition - _origin
targetVector.y = 0
// Find the "right" of the targetVector to rotate around
targetVectorRight = Vector3.Cross(targetVector, Vector3.down)
// Rotate the targetVector around its own "right" to get the target point
targetVector = Quaternion.AngleAxis(targetAngle, targetVectorRight) * targetVector
// The final target point. A shot fired at this point in world space will hit targetPosition
return _origin + targetVector
And the script that comes with the two barrels that are attached to the turret
class Barrel (MonoBehaviour):
public Delay as single
public BarrelObject as Transform
_turret as Turret
_firing = false
//_count2 = 0
def Start ():
pass
def Update ():
pass
def Init(turret as Turret):
_turret = turret
def BeginFiring():
if not _firing:
_firing = true
//Debug.Log("Begin " + Time.time + " Delay " + Delay + " Rate of Fire " + _turret.RateOfFire)
InvokeRepeating("Fire", Delay, _turret.RateOfFire)
def CeaseFiring():
if _firing:
_firing = false
CancelInvoke()
def Fire():
//Debug.Log("Fire " + Delay + " Count2 " + _count2 + " Time " + Time.time)
//_count2 += 1
for i in range(_turret.NumberOfProjectiles):
projectile = Instantiate(_turret.Projectile, BarrelObject.position, BarrelObject.localRotation);
velocity = BarrelObject.TransformDirection(Vector3(Random.Range(-_turret.Deviation, _turret.Deviation), Random.Range(-_turret.Deviation, _turret.Deviation), _turret.ProjectileForce));
projectile.rigidbody.velocity = velocity;
def GetProjectileOrigin() as Vector3:
return BarrelObject.position
Any Help will be greeted with many thanks x3