# How to slowly rotate object forward and back?

Hello, I’m making NPC attack motions and faced problem. I can’t find way how to slowly rotate gameObject forward and then back. My code:

transform.rotation = Quaternion.Lerp (this.transform.rotation, Quaternion.Euler (0f, 0f, 80f), 41f * Time.deltaTime);

transform.rotation = Quaternion.Lerp (this.transform.rotation, Quaternion.Euler (0f, 0f, 0f), 41f * Time.deltaTime);

But it just do nothing (as I said it should attack, ant get back to the start position). I think it gets back so fast that I can’t see. Maybe someone faced similar problem and can help? Thank you.

You want to use Quaternion.Euler (0f, 80f, 0f) instead? The one you used will actually rotate the NPC in the z axis.

No, no. I need rotation in the z axis, but it shoud be slow and visible. It is NPC weapon (sword) attacking motion. I need to get sword rotated forwards and then back.

You should separate the two codes above.

If both lines occur within the same frame, you cannot notice the change, although the rotation actually happens. You are actually rotating it back to the same spot every frame.

Is this rotation possible without separation because I don’t undestand how I could seperate it?
Thank you.

You can try this:

``````if(transform.rotation.z == 0f)
{
transform.rotation = Quaternion.Lerp (this.transform.rotation, Quaternion.Euler (0f, 0f, 80f), 41f * Time.deltaTime);
}

if(transform.rotation.z == 80f)
{
transform.rotation = Quaternion.Lerp (this.transform.rotation, Quaternion.Euler (0f, 0f, 0f), 41f * Time.deltaTime);
}
``````

It won’t work, because if you want to get angles (convert transform.rotation to angles) you need to use Quaternion.Euler. And it won’t work too because rotation will never reach 80 degree.
So I need a better solution…

Oh sorry was in a daydream while typing…

Replace it with “transform.localEulerAngles.z == 80”

Thank you!