 Alright, so now that we've actually visualized what the problems are and we understand what the problem is, let's take a look at actually creating a more advanced version of the follow camera that resolves these issues and maintains the distance behind the character. Okay, so let's get over into Unity and get started. All right, here we are back in Unity and we are going to create a more advanced follow camera. You know, advance is all relative when we start talking about these things, but what we want to try to solve is that issue of the distance between the character and the camera not remaining constant as the character is rotating around. So what we're going to do is we're actually going to create a new script first. So let's do that. Let's go and create a new C sharp script and I'm going to call it IP advanced. Next, follow camera. Okay, and again, I'm going to sign a little icon to it just because I like to do that with my scripts and it's a good practice to get into. Again, like I said before, in the previous video, it helps you identify which scripts are yours. If you're building a framework and it just separates it from other scripts that you would pull in from other asset packs. So let's go and actually launch this script in mono develop. Okay. Awesome. And let's get a few things set up. So first thing is getting the namespace in place. All right. Any pixel dot cameras. All right. And again, that just keeps the code secure from clashing with other classes that might have the same name from other assets that you import from asset packs like from the asset store. And let's put in our variables. And actually, we don't need these guys remember because we are going to base this from the base camera script so that by basing your scripts like this or inheriting from base classes, right, you can save yourself a lot of time and give yourself the ability to create a lot of code really quickly. It does take a little more upfront time to design how you want all your components to work and how your inheritance works, but it's highly recommended. So in this case, what all I'm going to do is actually just copy all these because it's basically the same thing. We're just going to handle the way the math works in a bit different way. Okay. And this allows us this will this technique will allow you to maintain that distance between the camera and the character and still have a smoothing property to the position of the camera. So and that's what we're looking for. Okay. So I don't need this debug distance anymore. What we want to do is actually take we want to actually figure out the rotation angle in the y axis of the character. Okay, so we need a couple more variables to be able to store these particular values. Okay, so the first one I want is a vector three. So vector three, and this is going to be called the wanted back. And I'll explain these as we go along. I just want to get these stubbed in. Then the next one that we want is a private float wanted y angle. Okay. And that basically is going to store the y rotation. Okay, because we're going to capture the current y rotation of the character and use that value to drive the position of where the camera should be. Okay. All right. So let's actually see this in action. Okay. So the first thing that we want to do is we want to say that the wanted y angle. So actually what I'm going to do first is just comment these guys out so we can go through this step by step here. Okay. So we're going to do wanted y angle is equal to math f dot lerp. Okay. All right. And remember lerp basically is just a linear interpolation. Okay. So it's the cheapest version really that we can use to smooth things out. So what we want to do is take the current y rotation. Okay. And make the target or make this y rotation, try to get to the target dot Euler angles dot y. So we want to try to get to that y rotation. And we're going to do time dot delta time times that smooth rotation speed or smooth speed. Okay. All right. So then the next step we need to store the world back direction. This is really easy. So we're just going to do back is equal to vector three dot back. Okay. So previously you saw that we captured the forward vector from the character. All right. This time we're actually just going to use the world position. So to make that more clear, the world position is this little gizmo up here. Okay. That's the best way I can visually show you. So it's always the pointing in the world position. And because we're using vector three dot back, we're getting this guy. Okay. So we're pointing backwards because it's different from the character because the characters forward and back is now local. Basically, it's local to this character. All right. So what we're going to do is we're going to start with the world position. Okay. And what we want to do is we actually want to rotate this world position to the current rotation of the character. I know this might sound confusing at first, but in order to do that in order to take a vector and rotate it, we have to multiply it by a quaternion. Okay. And you need to give us these really awesome quaternion methods that are pre that are built in. Okay. And the one that we are going to use is the angle axis. So what I want to do is put in the wanted y angle and then the vector three. Yeah. So basically what's happening here, okay, is it's building a quaternion rotation for us based off of the angle that we captured from the target from the character. All right. And we're smoothing out just that float value. Okay. So we're going back and forth between that float value and we're putting that into this angle axis. And then we're going to multiply that by the back vector. Okay. And that rotates the back vector. And to prove that what we're going to do is we're going to do a debug draw array. Okay. And we are going to draw that ray from the target's position. Okay. To the back. And we're going to give that a color of green just so we can see it. All right. Perfect. So let's actually take a look at that. Let's take a look and see what we're getting now. All right. So let me turn off the ground here and let's just hit play. All right. And we get nothing because I did not assign the script to this particular camera. So I'm going to assign that and turn off our basic follow that we did previously. Now let's hit play. And it looks like we don't have something assigned. Right. Which means we need to give the target variable an assignment. There we go. All right. So now if I were to rotate the character, you can see that we are rotating. But what's going on here? So when we're capturing that angle, you'll notice that it's working when we just slightly move. It's, oh, we got, there we go. There it is. Whenever we change from positive to negative, we're getting this flipping. All right. And this is just because the nature of rotations and the fact that we go from zero to 360. And what's happening when you'll notice up here when we rotate, we're actually going from zero to negative. And so it's wrapping all the way around to try to catch up to that value. And that's not what we want. We need to wrap the rotations. And in earlier versions of Unity, that was actually kind of difficult to do. But now Unity has provided us an awesome version of the Lerp called Lerp Angle. Boom. And you'll notice that if you hover over this, it says, same as Lerp, but make sure the values interpolate correctly when they wrap around the 360 degrees. And that's exactly what I want to do. And that's why we were getting that weird jumping. Okay. So let's test this out again now. All righty. Hit play. And there we go. Boom. There it is. And you'll notice that it is maintaining its distance all the time. So now we're not getting that weird camera motion where it's just trying to catch up to the latest position in a linear fashion. We're actually rotating properly around it. Okay. So the last step to get the camera to move is quite simple. Let's uncomment this, these actual pieces of code here. Okay. And what we want to do now is we want to say that the wanted position is equal to the back vector, right? Because the back vector is the vector that we're rotating times the distance variable that we've given it, plus our vector three dot up. So we get the height for our camera position. So m height. All right. And that's exactly all that we really need to do. The last step, we don't need this slurp anymore, right? We can actually just use that wanted position now. Okay. Because we're already smoothing it out right here. So we're already going to get a nice smoothing. Okay. So let's take a look now and see what we get. All right. So now I can turn the ground back on. Cool. There's our camera. Let's go and to snap over there. Beautiful. And let's actually make it a little closer. So let's do something like five, maybe three for the height. And that's good. Let's, let's try that out now. So now if I were to move around, you can see that we're not necessarily following, but we are rotating. And you'll notice that we're not getting a weird rotation where the cameras is trying to find the new position in the linear fashion. We're actually maintaining our distance behind the camera or the character. Perfect. So now when we move forward, you notice that we're still watching the character. So all we need to do is add on. So we're going to say plus the target position. You can add that to any one of these steps. You could add that actually to, well, let's just try that first. That should work just fine. But I'll switch back once we compile here. So now we're following the character and you could smooth that out as well if you wanted to. Right. But you could just put this up over here. That's what I was going to say to keep your code clean. So we know that we're doing all the math right here basically. And then we're just adding the position on like so. All right. And that is really all I wanted to show. In the next video, we're just going to wrap this all up and add to our camera menu. So we actually have two new cameras that we can produce. Okay. So let's do that in the next video. Thanks so much.