Hi Everyone,
I have a player that starts out on an empty terrain. He can place turrets one by one that should shoot a laser at the closest turret once in place. This is to create a laser fence. The turrets used to shoot their lasers, but when I didn’t have the following script, they all shot at the second turret created. Thus I implemented this script, but now, the turrets won’t shoot anything at each other!
Here is my script for the finding of the closest turret:
//..........Finding Closest Turret
var nearestDistanceSqr = Mathf.Infinity;
var taggedGameObjects: GameObject[] = GameObject.FindGameObjectsWithTag("FenceLaserPoint1");
var nearestTrt : Transform = null;
// loop through each tagged object, remembering nearest one found
for (var obj : GameObject in taggedGameObjects) {
var objectPos = obj.transform.position;
var distanceSqr = (objectPos - transform.position).sqrMagnitude;
if (distanceSqr < nearestDistanceSqr) {
nearestTrt = obj.transform;
nearestDistanceSqr = distanceSqr;
}
}
//................................
And here is my laser shooting code:
Laser=Instantiate(laserPrefab) as LineRenderer;
if (Laser!=null)
{
Laser.SetPosition(0,thisLaserPoint.position);
Laser.SetPosition(1,nearestTrt.transform.position);
Destroy(Laser, laserEnergy);
}
I know for sure that the laser shooting code is working well. It’s the scanning code to find the closest turret that isn’t working. Except, there are no errors so I cannot pinpoint any specific issues. I would greatly appreciate if you help me solve this problem.
Thank you,
Hyperion