# Turning the Turret towards Player Object

I  am trying to turn the enemy turret towards player object using the Armory 3D game engine with Blender. The script is written using Haxe. I am using the following code with both the player object and enemy object. Both have their Z values set to be the same (in this case zero):

public function turnTowardsPlayer()
{
var A:Transform = enemyObj.transform;
var B:Transform = player.transform;

var A1:Vec2 = new Vec2(A.loc.x,A.loc.y);
var B1:Vec2 = new Vec2(B.loc.x,B.loc.y);

// Finds the Direction Vector.
var res:Vec2 = B1.sub(A1);
// Calculates the angle to turn the turret.
var rot_Z = PI + atan2(res.x,res.y);
rot_Z = -rot_Z;
trace("  --------  ");
trace( "Rot.Z in degrees " + (rot_Z * 180/PI));
trace("  --------  ");
enemyObj.transform.setRotation(0,0,rot_Z);
}


The above code works well if there is no change in Z value. Made following changes to take in to account of change in Z value.

var A2:Vec2 = new Vec2(A.loc.y,A.loc.z);
var B2:Vec2 = new Vec2(B.loc.y,B.loc.z);

var res_1:Vec2 = B2.sub(A2);
trace("res_1.Z   " + res_1.y);
var rot_X =  atan(res_1.y/res_1.x);
//rot_X = rot_X;
trace( "Rot.X in degrees " + (rot_X * 180/PI));
enemyObj.transform.reset();
enemyObj.transform.setRotation(rot_X,0,rot_Z);


The above addition works only for a certain degree of accuracy. When the player moves either far right or far left from the initial position, the enemy bullet misses the player object. In the initial position the player is directly opposite to the enemy and the bullets from enemy hit the player correctly, with Z = 2m (in Blender units).