- Home /
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);
}
}
$$anonymous$$aybe use transform.rotation = Quaternion.Slerp(transform.rotation, hit.transform.rotation, speed);
to rotate to the enemy.
Answer by Digital-Phantom · Mar 19, 2015 at 12:58 PM
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.
:)
Your answer
![](https://koobas.hobune.stream/wayback/20220613190553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Smooth Mouse Orbit c# 2 Answers
smooth fps movements using joystick 1 Answer
Execute function with momentary input 1 Answer