Why not use Lerp here as well

In the PlayerFreeLookState the Quaternion.Lerp function was already introduced.

At this point in the course, why don’t you use it at well for the targeting? Is makes for a much smoother turn, which is visually more appealing, and which would help to consolidate the knowledge about the Quaterion.Lerp function.

My best guess would be - these were earlier lessons and they didn’t want to update the code/videos. They might refactor this before/after the official release.

I updated the code as well to a Slerp for a smoother rotation

    protected void FaceTarget()
    {
        Quaternion currentRotation = stateMachine.transform.rotation;
        if (stateMachine.Targeter.CurrentTarget==null) {return;}

        Vector3 lookPos = stateMachine.Targeter.CurrentTarget.transform.position - stateMachine.transform.position;
        lookPos.y = 0f;
        stateMachine.transform.rotation = Quaternion.Slerp(currentRotation,Quaternion.LookRotation(lookPos),10f * Time.deltaTime );
    }
3 Likes

I strongly recommend doing exactly that. In fact, I did it with my own code.

I tried your code. It worked but I noticed that as I circled the target the Player got further and further away from the target whereas using just the Quaternion.LookRotation() did not. The Unity docs on Quaternion.Slerp [Unity - Scripting API: Quaternion] didn’t shed a lot of light on the subject.

Hey @radhat , thanks for bringing this up. Yes, you are correct. I’ve used “10f * Time.deltaTime” for the interpolation ratio. if you increase that number to anything above 100 it will mitigate some of the rotation drift. Don’t know how big of an issue this would be in an actual game, as your target will move with you changing the Slerp values

I’ve also tested with just Quaternion.LookRotation() - and it also had some rotation drift when you are really close to the target

What was unclear from the unity Description of Slerp?

Spherically interpolates between quaternions a and b by ratio t . The parameter t is clamped to the range [0, 1].

Use this to create a rotation which smoothly interpolates between the first quaternion a to the second quaternion b , based on the value of the parameter t . If the value of the parameter is close to 0, the output will be close to a , if it is close to 1, the output will be close to b .

protected void FaceTarget()
    {
        Quaternion currentRotation = stateMachine.transform.rotation;
        if (stateMachine.Targeter.CurrentTarget==null) {return;}

        Vector3 lookPos = stateMachine.Targeter.CurrentTarget.transform.position - stateMachine.transform.position;
        lookPos.y = 0f;
        stateMachine.transform.rotation = Quaternion.Slerp(currentRotation,Quaternion.LookRotation(lookPos),200f * Time.deltaTime );
    }
1 Like

Thanks for the reply @mihaivladan . I thought the interpolation might be the culprit. I guess I didn’t go far enough in my testing. The description was OK, but just didn’t quite do it for me. I went there looking to see if it could explain the drift. Maybe I was asking for more than it was designed to give.

Oh, by the way, I saw your post on JetBrain’s Rider IDE and gave it a shot. It is so much easier even for an intermediate programmer like me. Out of the box it gives me more than VS Code (with a ton of extensions) was giving.

Thanks again
-R

1 Like

I’ve been a happy JetBrains Rider customer for several years now. I have VS Code and Community installed on my computer, but that’s mainly so I can attempt to help people who are having trouble making those programs work, unlike Rider that just works.

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms