Smoothly stop rotating

Right now I am casting out a ray to hit a enemy, while I have the enemy targeted I want my UI image to rotate, but when the ray moves off the enemy I want the rotation to return smoothly back to it’s original position.
I have tried to get Lerp to work in this case but I haven’t had any success.
So how can I take the rotation from after I have target the enemy and then smoothly rotate back to the starting position?

	void Update() 
	{
		Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
		
		Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow);
		
		RaycastHit hit;
		if (Physics.Raycast(ray, out hit))
		{
			if (hit.collider.tag == "Enemy")
				{
				reticle.color = Color.Lerp (reticle.color, targetColor, transitionTime * Time.deltaTime);
				transform.Rotate(0, 0, speed * Time.deltaTime, Space.Self);
				}
		}
		else
			{
			reticle.color = Color.Lerp (reticle.color, Color.white, transitionTime * Time.deltaTime);
			}
	}

give your rotation a variable for speed (rotSpeed). Create a loop and each time decrease ‘rotSpeed’ by a certain amount, 0.1 or such like.
Then once rotSpeed is at zero (or whatever speed you think looks right) just have the rest of your script run.

:slight_smile: