Problem With lookAt and Turret Controller Script

I’m trying to create a script that automatically rotates a turret to lookAt an incoming enemy fighter, but lookAt requires a position which I don’t know how to acquire, as the fighter is a moving target. How do I make the turret rotate to look at the incoming enemy fighter?

Unless I’m missing something here, doesn’t the turret know what entity it is targeting and therefore can get the position of that entity regardless whether it is moving or not?

No. I dunno how to make it to where the turret does know. I tried a couple o’ things a while ago before the Scripts 2.0 but nothing worked, even then. I tried using the lookAt function and putting the entity name in the parameters but the parameters are supposed to be coordinates. I also tried getPosition or something to that effect but I could never get the script working.

I’m guessing that you looking more for how to make the turret aware of it’s surroundings so it can target something when it gets into range?

Generally this is done by the game having a system in place that allows queries about what is in the world. Usually the system will keep track of all the objects of interest in the world in some manner to allow for easy querying.

In this case, this system could keep track of all the fighters in the world and the turret can query the system for the closest fighter to it’s position within a certain range. eg:

var closestFighter = World.getClosestFighter(this.entity.getPosition(), maxRange);
if (closestFighter) {
    this.entity.lookAt(closestFighter.getPosition());
}