Auto Target - Turn and Rotate

I would like for my player to be able to target the closest enemy to them using the Tab key. Then once selected, I would like to have the player auto rotate around so that it is facing the enemy and tracks the enemy they have targeted as it moves, kind of like an auto targeting sentry turret. Right now I have figured out how to get all the enemy targets in the world but I am not sure how to make my player lock on and rotate towards the closest enemy. Here is the code that I have for the Tab key.

 if(Input.GetKeyDown(KeyCode.Tab))
 {  
	gos = GameObject.FindGameObjectsWithTag("enemy"); 
	print("gos" + gos);
	
	var closestTarget: GameObject;
	var distance = Mathf.Infinity; 
	var position = transform.position; 
	 
	  // Iterate through them and find the closest one 
	 for (var go : GameObject in gos) 
	 { 
	    var diff = (go.transform.position - position); 
	    var curDistance = Vector3.Distance( transform.position, go.transform.position ); 
	    if (curDistance < distance ) 
	    { 
	      closestEnemy = go; 
	      print(go);
	      distance = curDistance; 
	   	  lookAtTarget = true;
	     } 
	     targetObject = closestTarget;
	    // targetObject.renderer.material.color = Color.blue;
	     
	  }
	
	} // end key down
	
	if( lookAtTarget )
	{
	   var player = GameObject.FindWithTag("Player");
	   var rotate = Quaternion.LookRotation( closestEnemy.transform.position - transform.position );
	   transform.rotation = Quaternion.Slerp( transform.rotation, rotate, Time.deltaTime * turnSpeed );
	}

I realize that in the last function, lookAtTarget, I am not using the player var that I set up because I was unsure what I was doing wrong with it. Each time I tried to do something with it, the code would not work.

My idea behind it was this, I was thinking that that I could use the transform of the player to then rotate the player to line up with the AI which is targeted. The player would still be able to move around but their front would always face the target, making it easier to shot them.

If you have any questions or need me to explain something in better detail please let me know and I will do what I can. Thanks for any and all the help!

Modify your lookAtTarget as follows:

if( lookAtTarget )
{
	var player = GameObject.FindWithTag("Player");
	player.transform.LookAt(closestEnemy.transform);
}

BTW, if the script is attached to the player object (the object you want to rotate), then you don’t need to find the object, you can simply do:

if (lookAtTarget)
{
    transform.LookAt(closestEnemy.transform);
}