# AI logic to rotate enemy towards a glove on screen

Here is my AI code

``````void AiRotateTowardsGlove ()
{
_enemyPos = Camera.main.WorldToScreenPoint (this.transform.position);

if (GameManager.Instance.gloveSpawned == true)
{
if (_destReached)
{
//Find glove and rotate towards that point
_destReached = false;//until it reaches the PU
_glovePos = Camera.main.WorldToScreenPoint (GameManager.Instance.glove.transform.position);
_relativePos = _glovePos - _enemyPos;
_angle = Mathf.Atan2 (_relativePos.y, _relativePos.x) * Mathf.Rad2Deg;
this.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, (_angle - 90)));
}
``````

This code is working fine but its rotating my enemy by snapping, but I want a smooth rotation instead.
I think I need to update its position in Update function till it reaches its derived roatation and then stop its rotation.
This is slightly tricky also because the enemy is constantly moving forward.
My questions

1. How to do a smooth rotation in z axis alone and
2. How do I make the enemy stop rotating once it knows that it is facing towards the glove so that it can continue to move forward and pick up the glove.

Cool so thanks for not helping, this helped me slog and find an answer for myself. Here is the answer so that it might help someone!

``````void AIGoToGlove ()
{
if (rotate)
{
_enemyPos = Camera.main.WorldToScreenPoint (this.transform.position);
_glovePos = Camera.main.WorldToScreenPoint (GameManager.Instance ().glove.transform.position);
_relativePos = _glovePos - _enemyPos;
_angle = Mathf.Atan2 (_relativePos.y, _relativePos.x) * Mathf.Rad2Deg;
Vector3 rotationTmp = transform.localEulerAngles;
_angle -= 90 + rotationTmp.z;
if (_angle < -180)
{
_angle = 360 + _angle;
}
if (_angle > 0)
{
if (_angle > 5)
{
rotationTmp.z += 5;
transform.eulerAngles = rotationTmp;
}
else
{
rotationTmp.z += _angle;
transform.eulerAngles = rotationTmp;
rotate = false;
}
}
else if (_angle < 0)
{
if (_angle < -5)
{
rotationTmp.z -= 5;
transform.eulerAngles = rotationTmp;
}
else
{
rotationTmp.z -= _angle;
transform.eulerAngles = rotationTmp;
rotate = false;
}
}
}
}
``````