Rotation won't lerp (2D)

Hey all, I’m making a quick 2D spaceshooter game, and I’m having problems using quaternion.lerp in my code.

Vector2 dir = look.normalized;
        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        angle-=90;
        transform.rotation = Quaternion.Lerp(Quaternion.AngleAxis(angle, Vector3.forward),transform.rotation,Time.deltaTime);

This is my code, and the effect is either an instant rotation if the t variable (Time.deltaTime) is set to anything above zero, or no rotation if it’s zero.

I’m using this instead of transform.lookat because transform.lookat doesn’t work in 2d correctly, by the way.

1 Like

The t variable should be advanced from 0.0 to 1.0 over time. It shouldn’t actually be Time.deltaTime. There are some big issues with Unity's Lerp tutorial. Who can I talk to about it? - Unity Engine - Unity Discussions (Read the whole thread.) LookAt in 2D: LookAt 2D Equivalent? - Questions & Answers - Unity Discussions

–Eric

The rotation is usually being updated per-frame, that’s why I’m using Time.deltaTime. I want the smooth change in rotation. It’s worked before with Vector3.lerp like this.

Read the thread I linked to about why using Time.deltaTime as the third parameter is mathematically incorrect and does not work. It may seem like it works, but in fact it runs differently on different machines and therefore should not be used. To quote from a post there, “You should never pass a linear multiple of delta time as the lerp argument, it doesn’t make sense, it is simple but mathematically always wrong. Assuming you want the behaviour of moving swiftly towards a point but slowing down on the approach, and you want correct compensation for delta time, you ought to use 1 - exp(-k * dt) as the lerp argument, and tweak k to control the rate.”

Anyway you can bypass the issue by using a 2D lookat method; see the other link I posted.

–Eric

I found the problem.

Changed transform.rotation = Quaternion.Lerp(Quaternion.AngleAxis(angle, Vector3.forward),transform.rotation,Time.deltaTime);
to

transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion.AngleAxis(angle, Vector3.forward),0.1f);

I just had it organized wrong.

1 Like

okay this one doesn’t rotate anything regardless of any of these floats

Note that this was a post from 7 years ago. That user hasn’t posted for 6 years.