Important Notice:

As of September 15, 2016, we no longer publish new courses on this website. We encourage you to use your Digital-Tutors credentials to sign in on Pluralsight where you'll find all new creative courses, skill tests and paths, 1:1 mentoring and more.
Project: Finishing the jump function
See Details
Joshua Kinney


Joshua Kinney
In this lesson, we're going to learn how to constrain our rotations of a rigid body object. All right, so we're just about finished with our basic movement script here. So we have our movement using the WASD keys. And that works really well. And we have our jump script. And it only jumps whenever it's grounded. But whenever we move and jump, our box begins to tumble. Now, this is OK for a physics-based type game. But what happens is sometimes the box will flip over on its side, and then those axes get confused and we lose all control. And whenever we start to press S to go backward, it actually moves forward. And we don't necessarily want that. So what we can do is, we're going to learn how to constrain that axis to keep it from flipping over. All right, so to get started with this, go ahead and go into the Movements Scripts folder. And in between lessons, I went ahead and created this. So that way, we can see our project files a little bit easier. Now, inside of that, I've created the Lesson 15 movement. Go ahead and open that inside of MonoDevelop, and paste in all of the script from lesson 14. And let's go ahead and try that one more time. Let's double-click on lesson 15, and that will open up there. So take everything from lesson 14, paste that in. And to get started with this, we need to start thinking about rotation. Now, we've talking about in terms of translation, where the object will move along a specific axis. But now we want to start dealing with its actual rotation. So to get an idea of where to start, we could start out with this transform.translate. Let's start looking at something like transform.rotate or something like that. Let's just investigate it. So going to our Unity script reference, we're going to type in "transform." And let's look through this. So I'm using transform, and looking through this I see rotation. So this is going to control the rotation of the transform and world space stored as a quaternion. Now, "quaternion" is a term that is very odd. And it can be a bit confusing. So let's go ahead and see what that means. So quaternions are used to represent rotations. It says they are compact, and they don't suffer from gimbal lock. So what is gimbal lock exactly? Well, without getting too technical, gimbal lock is basically whenever rotations begin to rotate in all different directions. They have what's called a rotation order. So the rotation order is x first, and then y, and then z. So x will rotate independently on its own axis. Now, whenever y is rotated, it will rotate the y-axis, and then also pull along with it the x-axis. And then the z, whenever it rotates, it pulls along the y- and the x-axis. And what happens is, whenever those rotations become parallel, it can give you some unexpected results. So to break that gimbal lock, we'll use what's called quaternion. And it will rotate independently of one another. So we're going to go ahead and we're gonna use this quaternion to help us out. So let's go into MonoDevelop here. And we're going to go ahead and we're going to type in a simple transform.rotation. And we're gonna do that underneath our transform.translate here. So we're gonna type in transform.rotation. And we're going to set this equal to a quaternion. So we want it to rotate along that. We want it to rotate using this method or this class. And then we're gonna type in dot. And we could take a look through some of these different functions that we have available to us. And I want it to look in a specific direction all the time. So I'm gonna set its look rotation to the vector 3 forward direction, and then also the up direction. So let's go ahead and type in the vector 3 forward. So Vector3.forward. And then I also want it to look in the up direction. So I don't want it to rotate along the actual z-direction. I only want it to lock or look in the x and in the y. So we're just going to type in Vector3.up. So this simple line of code here is going to use the quaternion rotation, so that way we don't get that gimbal lock. And it's going to set our look rotation to be forward, and then also up. So that way, it will lock in those directions there. So now that we've done that, lets his Save. And let's go into Unity. Let's apply this lesson 15 to our cube. So we're gonna remove this component, apply lesson 15. And let's go ahead and take our jump speed up to 200. And let's go to Play. And whenever we jump, you'll notice that our object is no longer rotating. We can take our speed up a little bit more. Let's go to 10. And let's move around, and let's jump there. So what we've done is we've just created some player movement that kind of looks like maybe something from a Super Mario Brothers game or something like that, because what it does is it allows us to move and jump at the same time. But then we could also control our jump. Now, you could take this movement script even further if you want and create the functionality that you would like. But what we're going to do now is we're gonna move onto something a little more advanced, and we're going to start using that character controller in Unity to begin creating a movement script there. So I'll see you in the next lesson.
In this series of Unity tutorials we'll discuss the major foundations of scripting with C# in Unity and apply what we've learned in two mini projects.

To start out, we'll look at several of the terms and techniques that are used when scripting in Unity such as creating and manipulating variables, understanding the different types of operators, and how we can create instructions for our game objects using functions. We'll also jump into creating logic with conditional statements, and loops. We'll even learn how to use basic arrays. Finally, we'll take what we've learned and apply it to creating a movement and animation script.