From the course: Revit to Unity for Architecture, Visualization, and VR

Fly-through camera

From the course: Revit to Unity for Architecture, Visualization, and VR

Start my 1-month free trial

Fly-through camera

- [Instructor] Now that our scene is set up, we're ready to start actually interacting with it. Right now, we have a camera placed in the scene, it's the same camera we've had the entire time, this main camera, but we just moved it into the building. If we push play, we go into a preview mode where now this game window is essentially playing the game or walkthrough or visualization that we've been making this entire time. So the camera's looking at the space, but right now we can't do anything with it, we can't move the camera, we can't interact with the space at all. To fix that, let's make a new script and dive into some basic interaction scripting. We go to scripts, right click and create C# script. We'll call it fly through camera. Here in our visual studio solution, you can go to the assets folder and under scripts, find the script that we just created. For this course, you don't need to already know programming but I'm also not going to be teaching you a lot of programming. I just want to help you have a basic understanding of the structure of a Unity script, what's involved, and how to get started with them. If you want to know more about scripting in C# or in Unity, there are great classes in our library. If you're familiar with object oriented programming, a Unity script is generally a single class. Our class is fly through camera. Let's start by defining some variables in our class. First let's make a rotation speed variable. Public float rotation speed. The word public means that this variable will be available from the Unity editor and can be available from other classes. Float is essentially a number with decimal points. Rotation speed is the name of the variable and it starts with a value of one. Let's go back to Unity. And we can already see the effect of what we just wrote. I'm gonna take our fly through camera script and drag it onto the main camera object. And now you see that it's been added as a component to this game object. The variable that we added is here as rotation speed and it comes default as one. We can do the same thing for translation speed. Now let's take a look at these two methods that already exist within the script by default. The first is the start method. When your game or walkthrough starts running, the first thing that happens is Unity goes through all of the start methods and executes any of the code that is in those methods. It executes that just once. The start method is where you usually put code that needs to happen at the beginning to initialize any variables or set things up for the first time. By contrast, the update script runs every single frame, once per frame. For this script, we don't need anything to happen in the start method, so we can go ahead and delete it. In the update, we're gonna have this script listen for input from the keyboard and then if that input is there, move the camera. Let's start by adding a comment to describe what we're doing and we can use an if statement to see if a key is being pressed. We say if input dot get key key code dot W and we need a second closed parentheses. So what this statement does is it checks every single frame whether the W key is pressed. If it is, then it runs all of the code between these brackets. If not, then it just skips that. So in order to move the camera forward, I'm going to start with transform which refers to that transform component on the game object that this script sits on. Transform component, if you recall, tells the object where it is in space, how it's rotated, and how it's scaled. So we refer to that transform and say dot translate which is moving through the space. Now we need to tell it how far to translate and in what direction. So we're going to use a vector three, which is a vector in three dimensional space, vector three dot forward, which is a shortcut for vector three zero zero one. Meaning zero in the X direction, zero in the Y direction, and one in the Z direction. Vector three dot forward, over 50, just because one meter is way too much to move in a single frame. And then we're gonna multiply that times our translation speed. An overload to this function lets us decide whether we are moving forward according to the world's coordinates or according to the camera's coordinates. This becomes extremely important as our camera begins rotating. So for the coordinate space, we will choose space dot self. We add our semicolon to the end. Save the code and let's test that first button. When we press play, Unity compiles that script and if we click on the game window, we're able to start interacting with it. Press W and we move forward through the space. Now we can copy this and do it again for the other directions. Using A for left, S for backwards, and D for right. Let's test that now. W for forward. A for left. S for back. And D for right. Great. Now let's use a similar strategy to get the camera rotating. We'll use arrow key rotation. We'll do the same kind of if statement. If input dot get key and this time, we'll say, key code dot right arrow. For turning right. Make sure you add that second parentheses. Then with similar command in the brackets, we say transform, dot rotate around, and this let's us choose the axis that we rotate around. Vector three dot up and then we define how far we want it to rotate in degrees. Let's do our rotation speed, divided by 50. And let's test it. Looking good. If you want to be able to see your test better, drag your game window the left and you can see a button that says maximize on play. Let's try that again. Now it fills the window and we can see the game a lot better. Let's go back and add the other turn directions. In order to rotate left, we're gonna have the same rotation about an axis, but we're going to minus the rotation speed. In order to rotate up, we're gonna change our axis of rotation to be right. Down, we change the axis of rotation to be right and we add a minus onto the rotation speed. Let's try that. Rotate right. Rotate left. It looks like we picked the wrong axis of rotation. Let's try this again. If you recall, in the translation portion, I talked about the importance of specifying whether you're using the world's coordinate system or the object's coordinate system. Let's take a look at this transform.rotatearound in the Unity documentation. It specifies here in the documentation that it is point in world coordinates. The better strategy for us will actually just be transform dot rotate for this up and down motion. However, for left and right turning, the world coordinates are actually great. So let's trade this out for transform.rotate in both cases. And then based on the different way that it calculates, we won't need this over 50. We'll save that file and give it another shot. It's looking pretty good. The current set up is using an inverted Y axis so when you point up, you look down, as is common in a lot of video games or in aircraft controls. For this application, let's swap that around. By trading out the minus signs. Now if we jump back into Unity and push play again, it will recompile and we can see the finished product. Now that we've finished writing that script, because we made these variables public, we can change the speed as we like. I might want to fly through a little bit faster. So we can modify those variables without going into the script at all. The final script we wrote here can be found in the exercise files.

Contents