LONDON LOWMANSTONE: So, hi everybody.
Welcome to the CS50 seminar, Create Your Own Virtual Reality.
I'm London Lowmanstone and I'll be teaching the seminar.
So the first thing I want to talk about is what you're
going to learn during the seminar.
We're going to figure out how to create a the virtual reality app
in a certain environment called Unity.
And we're also going to be able to upload this app to an iPhone
by the time we're done.
I don't have any experience with Android, unfortunately.
So if you have an Android phone--
personally, I learned all of this stuff by doing Google searches last year.
And so, if you have an Android phone, I'm
pretty sure it's fairly easy to go online, find searches, and figure out
how to upload it to Android.
I have an iPhone, so that's the process that I'm going to show here today.
So, for a set up, you're probably going to want to remember
this URL so you can access the page.
All of these instructions, plus a couple more hints,
are available at bit.ly/vrseminar And so, if you follow that link,
you'll get access to a page that has all this information plus more.
So the first thing you're going to want to do is install Unity Personal.
Personal is the free version of Unity.
And as long as you're not using Unity to make more than $10,000,
you should be able to download Unity Personal
and use it for free, which is pretty awesome.
I think Unity will install Monodevelop.
If not, then you should also install Monodevelop.
It's the programming for Unity.
So whenever we need to code something, it will be in Monodevelop.
And if you're on a Windows computer, Microsoft Visual Studio
will suffice, as well.
There is the download for Monodevelop.
And then, in terms of actually using Google Cardboard-- so
Google Cardboard is this thing right here.
It's a virtual reality device.
It unfolds and it becomes like a little box that you can put your phone into.
You're going to need to download the SDK for Google Cardboard.
And that will be a source code zip folder.
And you can download it at this URL here or this one here.
And then if you have an iPhone, and you want
to be able to upload it to your iPhone, you
should download the iPhone programming language, or IDE for programming,
called Xcode.
Get a developer account.
And then you should also register your phone
as a Developer's, like, device that you can then upload things to.
So it might take a while, but it's only needed
for the last step of actually uploading things to your phone.
And then, for the final step, you'll need
to actually buy a Google Cardboard.
It should be less than $10.
This is the only step that should cost any money, whatsoever.
The rest of everything else should be free,
which is something that I really like about Google Cardboard.
So total cost should be less than $10.
So what is virtual reality?
I like to think of virtual reality as separate from these other two types
of virtual-like realities, which are AR and mixed realities.
So AR is augmented reality.
It's when you have a space like this, and you look around,
and it will basically display data on the information around you.
That's augmented reality.
You can also be in the real world and put on, like, glasses or whatnot
and interact with virtual objects.
And that's called mixed reality.
Virtual reality is when everything around you is virtual.
So you have no interactions with the real world.
It's all a complete virtual reality.
So this has many applications.
You can imagine, easy ones for entertainment,
where 3D movies are pretty darned cool.
Job training, where you can have people learn things
about their job or interact with virtual simulator
to learn how to do the job for the first time.
It can also be super useful for remote controls.
So, if you imagine you're, like, flying an airplane,
but you're in a virtual reality version of the actual airplane
that you're flying.
And, in my opinion, it's kind of starting to take off.
There are lots of new devices ranging from all sorts of different ways
to put on virtual reality.
There's Microsoft HoloLens and Oculus Rift and a whole bunch of other things.
HoloLens might be considered more AR or mixed reality.
But anyhow, all this type of stuff is getting extremely popular.
And I think it's really cool that you can make it for under $10.
There's also many new audiences that are being introduced to it.
One of the coolest things I saw was Lockheed Martin,
a company created a bus where they had virtual reality displayed
on the windows, so that kids took a bus field trip on the surface of Mars.
So as they took turns down street, they actually
mapped the streets onto the surface of Mars
and were able to get people to feel like they are on Mars and experience that.
So I thought that that was a super cool thing
that you could do with virtual reality.
So now let's get started.
As I mentioned before, this is all stuff that I learned from Googling last year
because I was interested in virtual reality and [INAUDIBLE] my class
and did it.
So I have no formal training in this whatsoever,
and this is just what I've learned from searching online.
So you can find almost all of this information, if not all of it, online.
And of course, in the seminar.
So Unity is an engine for creating your 3-D world.
We'll be using some other things to actually do the programming
and uploading it to your phone.
But for the actual world, that's all going to be done in Unity.
It makes it really easy to build virtual reality apps.
And it's completely free, as long as you're not making more than $10,000,
which probably you're not-- given that you haven't made an app yet.
So let's set up our world for virtual reality.
I'll make note here that I have a whole lot of slides,
but really, most of the stuff we're going to be doing is on Unity.
So I'll kind of discard the slides at this point.
We might come back to it just to get a hang on some other stuff.
But most of the stuff we'll be doing will actually be on Unity.
So first we're going to create a new project, import the SDK
that we downloaded for Google Cardboard.
We'll choose the layout for our project.
I happen to prefer some other layouts, but we'll stick with the default one.
And I'll just show you guys how that works.
We're going to add in this cardboard main pre-fab
and then delete the main camera.
And we'll have virtual reality.
It won't have any objects in it.
And that's what we'll do after this.
So if you open up Unity--
Unity should look like this if you downloaded it.
You can say file.
And there should be something that says New Project right here.
For you, it will be bold and allow you to open up a new project.
Or you can click this button here.
And then let's name this--
so CS50 Seminar VR World--
if I can spell world--
OK.
And then, I don't really need Unity analytics.
It gives you some data about your projects.
And then you can also choose what location you want to store this on.
So I'm going to go to hard drive and store it in CS50 Seminar.
So that's the folder that's going to contain this entire project.
So now I can say, Create Project.
You also want to make sure that it's 3-D, not a 2-D project.
Unity can be used to build both versions of apps.
And so you want to make sure that it's the 3-D version because we're going
to make a 3-D virtual reality world.
So this is our virtual reality world.
We'll stick with something close to the default.
If you want to change how your layout works,
you can actually drag all these little tabs around.
So this is, like, going to be the game tab where we actually
see what's going on.
This is the scene tab where we can build up the scene.
And the game is where we actually run and play stuff.
This is our Project tab.
It's going to hold all of the folders and things
that we import into our project and all the, like, objects and stuff
that we use to create the scenes.
And then over here is going to be our hierarchy.
And it's going to have all of the objects that we have into our games.
So if we, like, create a ball or create terrain,
these are all going to show up here.
So the first thing we're going to do is import the SDK from Google Cardboard.
So in order to do this, you can go to Assets--
Import Package-- and this is going to be a custom package.
I believe I still have mine--
oh, look.
Comes up right here.
So it depends on where you have your package.
Mine-- I still have in my Downloads folder.
And so you open up that folder--
GBR Unity SDK 0.6 and that should give you the Unity package.
Do make sure that when you download the SDK, you're downloading 0.6--
I haven't gotten the newer versions to work with Unity
and uploading to my iPhone yet.
So make sure you are installing that old version of 0.6.
It's the best one that works with Google Cardboard right now.
So Cardboard SDK for Unity--
Unity Package-- is the one that you're going to want to install.
It will upload everything.
You can oftentimes select what types of things you want.
For this, I'm just going to import it all.
It shouldn't take any longer than usual and should be just fine like that.
So this is because we're using the old version of the SDK.
And since this is a new project, normally
if you had a project that had been built before,
you'd really want to make a backup of it.
Here, we don't exactly have a backup of it,
but we don't care because it's a completely brand new project.
So it will go ahead and basically run a script on all of the Google Cardboard
scripts and update them to the new APIs and whatnot that Unity now uses.
So you'll see that Cardboard and plug-ins show up here.
And what we can do is go into Cardboard and Prefabs.
Prefabs are like pre-made objects that can be downloaded.
And so, for the pre-fab that we want--
it's called Cardboard Main.
You'll see there's another Cardboard M here-- that's Manager.
You don't want that.
You want Cardboard Main.
And this will make our app 3D.
So you'll notice that since this part came down,
you can now see that objects that we have in our world.
We have some light source that's kind of showing up on the horizon here.
And then we also have a main camera.
And now we just dragged in Cardboard Main that just showed up.
So if we run our app, it will switch over to the game tab and show
something like this.
You'll notice it's got weird stuff behind it.
That's because I haven't deleted the main camera yet.
So we can delete the main camera.
And Cardboard Main will actually act like our camera
and will be able to look around a virtual reality.
So you can see that this is something like how the game will display
once it's actually on your phone.
Each side of those--
each side of the screen there--
what Google Cardboard does is it has two magnifying lenses that kind of help
cross your eyes.
And it uses the fact that when you cross your eyes with two things that
are showing different perspectives, it makes it look really 3-D and real.
So that's what this is displaying here.
So that's how we know that Cardboard Main is working.
If you already have some experience with Unity
and you just have a world that you want to make into a 3-D virtual reality,
you can literally drag Cardboard Main.
And then whatever scripts you had on your camera,
you pretty much just put them onto the Cardboard Main object and there you go.
Now you've turned this other 3-D world into a 3-D virtual reality,
which I think is pretty awesome and really easy to do.
So now let's start actually building a world.
We're going to add some terrain and make some mountains in here.
So there are a couple of different ways you can do this.
You can right click in the hierarchy and select 3-D object--
or maybe it's not 3-D object.
I don't know.
You can create stuff both in the hierarchy--
but the way that I normally do it, obviously, is to go through here.
Looks like it is a 3-D object and it is on the terrain.
So now we've got this terrain.
You can see how big it is in the scene here.
Let me back up.
So now we've got a terrain.
As big as it is, I like making mine a little bit bigger.
So we're going to update this.
You can see I clicked on the Settings panel for the terrain.
We've got the terrain selected over here.
And now I'm the inspector for the terrain.
We've got settings for it.
And I'm going to make it twice as big.
So I'm going to adjust the width to be 1,000 and the height to be 1,000.
And now you can see I've got twice as big.
Awesome.
And then you have to be careful if you're doing this on your own
not to make it too big because it does a whole lot of calculations
for the terrain.
And if you make it too big, it can't do all those calculations at once
and it will make your app run a lot slower
or actually cause your app to completely break.
So I find that 1,000 is pretty good for whatever you want to be doing.
If you want to make bigger terrains, see whether or not your app still
works with that gigantic terrain.
Now we can actually draw some mountains on this.
So there is a little raise and lower terrain button.
I'm going to just view here so I'm seeing everything from the top.
And then when you want to move around in your scene,
these are your options up here in the corner.
You can zoom in on these so you can see what's going on better.
So these options up here in the corner are
what you can use to actually move around in the world.
So I'm going to do this and then zoom in.
And since this is a programming course, of course we have to do--
hello, world.
And so I will draw some mountains that spell out hello--
oh, that's a of funky o--
and you'll note that I'm using my trackpad.
A lot of this stuff can be easier with a mouse.
But I happen to use a trackpad and it works just fine.
So you can do either.
So there you go.
It's just that easy.
If you right click and move around, then you can change the orientation.
And we can see that-- let's see, I'm going to move over here a little bit.
And now we have some mountains that spell out, hello world.
So you can kind of move back and forth in the mountains.
So I think it's pretty easy how--
pretty cool how easy that is.
So now we've created the terrain.
The next thing that we're going to want to do
is make a player that can move around the terrain.
So here we are going to create a 3-D object.
And this time it's going to be a sphere.
What we could do at this point is make the camera, itself,
what's moving around the world.
But I find it much easier if we actually create a new object, the sphere,
and put the camera inside of the sphere and have it move around that way.
It makes a lot of the physical interactions easier
and it's a lot easier to separate the programming that way.
So I'm going to call this the player.
And what this is going to do is we're going
to make this into an actual physical object in this world.
So right now it's just going to kind of hover there.
Let's see-- wow, that's very tiny.
And it's kind of stuck in oblivion.
That's extremely tiny.
Anyhow, so in order to make this an actual physical object that
can interact with the mountains, we need to add this component called
the rigid body to it.
So this is rigid body.
And you literally just click on it and it adds a rigid body to it.
You can see that this starts to look more like a physical object.
It's got stuff like mass and drag and other things like that.
So we can uncheck, Use Gravity, because we
want this to be flying around the world and not just rolling.
So that's what that does-- turns off gravity.
And then, now it's time to actually make it fly.
So this is the first programming part of this.
I want to make a new script.
So you'll see that that's an option down at the bottom here--
new script.
And we're going to call it, Fly.
And I happen to use the language C-sharp.
You can also use JavaScript.
But it looks quite a bit different.
I happen to like C-sharp because it's a lot more like Java,
which I know better.
So if you double click on Fly, it should open up in some editor.
For me, that's going to be the Monoscript editor.
For other people, that can be something else, like Visual Studio
or Monodevelop, not Monoscript.
Where did it go?
Hop up here?
Yeah.
OK.
So it might open up in another window or you have to slide over.
Time to make this into a new thing.
OK, so you see that it starts us off with this Fly code here.
And this is how every single script that you make is going to start off.
Using-- you'll notice in C-sharp--
is a lot like Import and Python, where it just uses a particular library.
Our script name is here, it's called Fly.
And it actually subclasses another class called mono behavior.
That's going to be the case for all of your different scripts.
They're all going to be subclasses of mono behavior.
Every single script can have a start and an update.
This runs once at the start of when you're running your project.
And then update this called once every single frame.
And let's start to make this thing fly.
So it seems obvious that we'll probably want a type of speed for the ball.
So we'll have a float speed.
And if we want things to be editable inside of the Unity editor,
we can label them public.
And then we can edit them as we go and adjust them.
So almost all of the--
almost, if not all, of the variables we use
will be public variables so that we can edit them in the editor.
And then, as well as speed, we're also going
to need the pointer that we want the object to go.
So if we want to go this way, we need to have something
for it to point to tell it what direction to go.
So this is actually going to be a game object that we'll give it.
And game objects are really any type of object that we see in that hierarchy.
So the ball itself is a game object, as well as Cardboard Main.
And, as we'll see, a particular part of Cardboard Main called,
Head, is also a game object.
And that's what we're really using as the pointer here.
So we have the speed, we have the pointer,
and now instead of actually using start an update,
I'm going to be using this one called--
what is it called--
is that the right one?
Yes.
Fixed update.
So fixed update is what Unity likes to use
whenever you're adjusting the physical properties or, like, how something
is moving.
So in this case, we want to be adjusting the velocity of the object.
And so you want to do that inside fixed update, not update.
So any physics type stuff you're generally
going to want to do in fixed update.
So now we actually want to get the rigid body component of the ball.
So we'll do that with rigid body kit component.
And then we want rigid body.
So this literally just accesses the rigid body component of ball
that we've made and assigns it to the variable RB, short for rigid body.
And now we can use this rigid body to actually move the object.
So we can say RB.velocity is equal to speed times pointer.
So you'll notice that we just accessed the pointer here.
And then it-- every single game object has
a component of it called the transform.
And I'm actually going to switch over here
so you can see what this transform looks like.
So here you'll notice our player has a transform right here.
The transfer arm holds things like position, rotation, and scale.
But it also holds the different direction.
So it just so happens that there's a forward vector that we can reference.
And so that's what we're going to do back in the code.
So here, transform.forward and that will get the blue axis of the transform
in the world space, which is the forward axis.
So this will set the velocity of the ball to be the speed.
And then whatever object is pointing in a certain direction,
it will point in the same direction as that object
and move forward in that direction.
That's exactly what we want.
So we can save this.
And then as we switch back into Unity, we notice that this will update.
And now it has options for us to put in a speed and a pointer.
So speed works off well as 10-- just to start off-- to see how things go.
And then for the pointer, we actually want
this to be the head underneath Cardboard Main.
So a note about the differences between Cardboard Main and Head.
Head is going to be the rotation of the user when they're moving around.
So when they look through Google Cardboard and they move their head,
that's going to be what's rotating.
Cardboard Main is going to be the entire camera moving around in the space.
So if you have any movement scripts that you want to attach,
those are going to be attached to Cardboard Main.
If you ever want to see what direction a person is looking in,
that's going to be the forward vector on the head.
So since we want the player to move in the direction that they're looking,
we'll just drag the head object in to there
and that should allow the ball to move.
I'm going to make sure that the ball starts off at 0, 0, 0
so we can actually see it.
And when we play, you can see the ball flying off.
And then a little intro into how to actually use the emulator on here.
So I'll play it again.
If you hold down Option, and you move your mouse around,
it allows you to look around in different directions.
And we can see that the ball is moving around in the different directions
that we look-- up and down and around.
And then if you hold down Control, you can tilt.
This won't do anything because it's not actually affecting the forward vector.
But it allows you to move around in the world.
So you'll notice that this is still showing like how
it would display on your phone.
Cardboard Main actually has a couple options to help deal with that.
So VR mode enabled here actually allows it
so that you can just see your world without having that VR stuff.
You're going to make sure that you recheck that though before you actually
export or build your app because otherwise
it will display on your phone like this.
If you don't have Google Cardboard yet, it
might actually be a good idea to export it with this unchecked.
So that way it displays on your phone like this
and you can just look around and see stuff.
If you have Google Cardboard, you're definitely
going to want to have that check so it actually makes a virtual reality.
All right, so that's the first test.
We now have a ball that can fly around the world.
Now we just have to attach the Cardboard Main to the ball
and actually be inside of it so that you feel like you're the one flying
as you go through the app.
So the way that we're going to do that is a pretty simple script--
we're just going to call it Follow, and make a new script.
And this is literally going to follow the player object.
So we'll give it a target.
And then here, again, we're not going to be using Start or Update.
We're going to be using another one called Late Update.
So the reason for this, is that all of the physical actions
take place around this update time and around this, like, fixed update.
And what I want to have happen is I want the ball to move
and then I want the camera to move inside of it.
If we have a situation where the camera moves
to where the ball is, and then the ball jumps forward,
it could be that our camera actually is outside
of the ball by the time the frame is finished.
And then we'll like see the ball hovering in front of us
and it will be kind of weird.
A note as still like how this all works--
Unity only renders the outsides of objects.
So if we put the camera inside of the sphere,
we'll actually be able to look out from the sphere
and won't just see like the outside edge or feel
like we're trapped inside this bubble.
And we'll see a better example of this later, but that's how this works.
So if you put something inside something else,
you can always see outside of the object.
You just can't see into it.
So inside of this Late Update we're going to reference the current object
that the script is on.
So we're going to want to put this onto Cardboard Main.
Game Object it's kind of like Self in Python.
It just references what other current object that the script is on.
So Game Object we want to access this one as transformed-- so it's position.
And so its position equal to whatever the target transference position is.
Target dot transform-- and then position.
Ta-da, not too hard.
So as we switch back over to Unity--
eventually this should update.
I oftentimes like to hit Pause just to force it to update to whatever script
we made.
And now you can see that that shows up.
And inside that target, we're going to want to put the player.
So you can actually drag it.
You notice each time that we drag, there's
also a little button over here--
I'll zoom in so that you can see it better.
So there's this little button here.
And if you click that, then another window
opens up where you can select from all of the different possible objects.
So if you don't like dragging, that's another option
where you can just select it.
So now it's inside of our player, and we should
be able to move around and actually do our first test of actually
being inside of the sphere.
So it turns out that the way that we are moving the player before
was a little bit too slow, so we're going to switch this up to speed of 50.
And then we're going to play it.
So now, for the first time, you can see we are actually
flying around this virtual world.
And we can kind of dip and dive in between mountains and fly around.
And I love doing this because its so much fun to fly around mountains.
So in virtual reality, it will look very similar to this,
except it will actually look 3-D. And if you put it on your phone,
and you don't have the virtual reality in it enabled--
so you like uncheck that box--
then it will show up like this and you'll just move your phone around
and it will display differently based on which direction your phone is
pointed in.
So both are pretty cool effects.
So now that we've actually got a working prototype for how
we want this app to work, it's a good time
to organize this project workspace down here.
So I like to organize everything by its particular type.
So I'm going to make a scripts folder for all of the scripts
and put those in there.
And Google Cardboard looks like it also has a plug-ins folder,
as well, that is imported.
So we can put all of Google Cardboard into one folder.
Plug-ins and cardboard into Google Cardboard.
Rearranging stuff in here won't affect how your project runs at all.
It will find all the same assets in the same spot.
I also notice that I haven't really named the terrain.
So I can call this mountains.
And make sure you hit Enter.
If you don't, it might not update and then
you'll have to re-type in the name.
And I'm also going to call this mountain terrain and make a new [INAUDIBLE]
folder.
So the main reason for organizing stuff is so that it's easy to find later.
And if you make a lot of projects, maybe you
want to copy this script over onto a different project.
And it's really easy if you know where your scripts folder is.
I know this is a lot unlike my room, because my room is very, very messy.
But I like to happen to like to keep my work spaces clean.
So that's that.
Now we're going to make a whole bunch of improvements on this world.
I think it's really fun to paint the mountains and change their textures,
and so we'll look into that.
We'll also be able to add some objects into this world
that you can find online and we'll do some more programming.
I want to show a bug that's in the program right now.
As we sort of fly, I didn't actually try to fly through any mountains,
but you'll notice that if we try to, sometimes
we'll actually bump into the mountain.
A lot of time, we'll kind of like jerk through it and it will act very weird.
So we're going to spend some programming time going through that
and figuring out different methods of dealing with that.
So Unity isn't completely perfect all the time.
And oftentimes, you have to find ways to get around ways that it's not
absolutely perfect.
So the first thing-- textures.
Textures are what allow objects to look differently.
And so, as we go through, we'll be able to actually-- instead of just having
this plain, vanilla-like white background,
we can change how the mountains look and actually
get them to look a heck of a lot more like mountains.
These textures can be any PNG image.
So if you have, like, a photo of your friend
that you want to use as how you want your mountains to look,
you can actually copy paste the photo of your friend onto here.
That will look pretty flat.
One thing that all of the PNGs are is they're static,
so you can't have, like, moving images on there.
I tried to look up a little bit yesterday
at how you could do moving images.
It looks like it's fairly complicated and that Unity isn't especially suited
to it.
But you might be able to if you wanted to.
Most of textures though, are going to be completely static.
You can make them look a lot more 3-D though with a special normal PNG.
And so it'll look kind of funky, but it will allow you to have a flat area
and then show a 3-D texture on top of it.
So I'll explain that more when we get there.
The coolest thing about textures is that you can actually
download them from online.
So we're going to show you guys the asset store here.
And it takes a second to load.
It's pretty cool because it has a lot of free stuff that you can get.
And it's right here inside of Unity.
Looks like it didn't quite load correctly, so I'll reload it.
Hopefully it will come up right-- yay.
OK, so in this case, we're looking for textures.
So I'm going to search for a texture pack.
And I only want free texture packs because I
don't feel like spending money.
And this one looks pretty good.
So for you guys, this will probably say download.
It just so happens that I've already downloaded it so I can just import it.
And this will show up similar to when we imported our SDK as a package.
It will show, like, a little list of objects.
For these ones, I highly recommend selecting everything
that you don't need.
Here-- there is a lot of stuff that we really don't need in it.
And so the things that I want are actually
just these particular cave rock wall images.
You can see that that's what that one's going to look like.
This is a different color.
And this is what a normal looks like.
So it's kind of got like this funky purple, like, blue sheen to it.
And that's what's going to make errors actually look 3-D.
And then I also like having this darker one, as well.
So we can import these.
And this will show up in our project plane.
Just so happens that it already puts them
in a textures folder, which I really like,
so I don't have to organize anything.
And now we can start to paint our mountains.
So we'll go into the scene here, click on our mountains.
And you'll notice that there is a little paint brush here.
And this is what allows us to paint our mountains different colors.
So we can edit the textures.
Add a particular texture.
And your texture options--
if we want to select a particular one, we
can select it here, or of course drag.
It's interesting.
Normally this image would also be displayed here.
So something's going a little bit wrong, but hopefully this will still work.
Yep, it still displays there just fine.
So, you know, Unity can be a little bit finicky.
But as long as things seem to be working, they'll generally work.
So here, now we can add that to here.
And you can see that our mountains just got looking a lot more like mountains.
So if we fly around the world now, you can
see that they look like this when you get up close and it looks pretty cool.
So I'm going to back up.
And I don't want it all to be gray.
So I'm going to add a new texture.
And we'll make this one a little bit lighter.
And then we can actually, literally, just paint in our world.
And I want to use a slightly bigger one so we can cover all of the mountains.
And then I want to switch the white one and paint world.
And then, as we fly around, we will see that some of the mountains
are going to be lighter.
And then some of the other ones will be darker.
So we can hit play.
They'll fly around our world and see that there are some
that look like snow has fallen on them.
So there's a whole lot of cool stuff you can do with textures.
And one of the things that I really want to experiment with,
and I hope you guys will, is if you have more metallic textures,
and you can mix them with, like, natural textures,
and make a whole bunch of, like, really funky scenes that kind
of border in between natural and metallic and man-made.
And so I think that would be a super cool thing to do.
I'm running through the mountains.
OK.
So we can stop that.
So now we've done our painting of the mountains
and gotten things to look a little bit differently.
Let's see what happens if we want to add an object.
So it just so happens that Unity is able to display these fbx or .fbx or film
box objects.
And there are a lot of these online because they're
used, oftentimes, for 3-D modeling.
And I asked my roommate so like, what do you
think would be a cool thing to add to this world?
And he said, oh, that looks pretty cool, but I'd like to add a fortress.
So I literally went to Google and--
see if I can get out of this--
and typed in fortressfbx--
and hey, what do you know, they have a three fortress fbx model
we can click on and download.
Looks like this.
And you can download it right there and it should give you
an option to select it as an fbx.
It just so happens that I've already downloaded it.
So I'm going to now install it in my projects.
Just going to switch a couple slides here as I get back to my main screen.
All right.
So here's my desktop.
I'm going to open up Finder.
And I just happened to-- when I downloaded that--
I happened to not save it My Downloads folder, but instead
into this Resources folder.
And so the castle is right here.
Remember when at the start of--
when I made my project, I named it CS50 Seminar VR World.
So we actually can just copy this castle folder
and paste it into the Assets folder of our project.
Oh, did I say new folder instead of paste.
Pasted on.
It will show up there.
And then as we go back to Unity, you'll see that Unity starts to update.
And if we click on Assets after it's done updating,
we'll see that there is a castle folder inside of there that
will actually hold this castle object.
So here is the castle fbx right here.
We can just drag this onto the scene.
Looks like it's super tiny right now.
So let's make it a little bit bigger.
How about 500 times bigger?
There we go.
And then let's rotate it so that fits on the ground better.
And I happen to know that I want it facing this way
direction because I know that the person's going
to be starting off in this corner.
So I actually want them to face the castle as they go into the world.
That's why I did negative 90 instead of 90,
because 90 would have flipped it the other way.
So we now have this castle in our world.
And if we press play, and we turn around--
hey, what do you know?
We have a castle.
One potential issue with this castle, though,
is that it doesn't have any of these things called colliders on it.
So the mountains happen to have built-in colliders,
as do the sphere that we have the camera on right now.
And it makes it so that sometimes you bump into stuff.
I mentioned that there are issues with this
that we'll fix with programming soon.
But the castle has no colliders, so we can literally
fly through walls and doors and things like that without any issue whatsoever.
And it looks really weird like that.
So let's back up and fix that.
I literally discovered this yesterday while I
was making this presentation because--
and it would have saved me a heck of a lot of time
while I was making a project last year.
It just so happens that under the castle fbx--
object down in here.
So you have to actually click on it inside of the Project tab.
There's an option called Generate Colliders.
And this creates colliders that will then make it so that our ball does not
run through the different walls.
So for now if we turn and try to run through a wall,
sometimes this works better than other times,
because again, Unity is not working great.
But we can-- maybe it will stop us.
Maybe-- yeah, this time it actually stopped us and we now
are running into the wall.
So you can see that Unity still isn't rendering it perfectly,
but hey, at least it got a little bit better.
And we can see the collider is there.
If we hadn't done that, then the ball would never have run into the wall.
So it's time to talk a little bit more about these colliders
and how they work better.
But first let's adjust a little bit how this whole colliding
thing is messing up in Unity.
So the easy fix for why the ball is running through these mountains
and through the castle is that the ball is just too small.
And when Unity is given more points to work with, and you have a bigger ball,
things will look better because when you crash into stuff,
your camera won't be right up against the wall.
It will have a little bit of space in between it
so you feel like you're actually, like, bumping
into things that are farther away.
And you can see what's going on when you bump into things.
And the other reason is that it gives Unity more points
to deal with so when it's trying to compute collisions,
it actually has a better chance of saying,
oh, there's actually a collision going on here.
So I'm actually going to make our ball 50 times bigger.
And just to give an idea of what this sort of looks like--
as we're playing our game, I'm going to drag the scene down here and orient it
so we can kind of see what's going on.
There we go.
And then, as we play the game, we can see our ball rolling around down here.
And then as we move--
so this is what's actually going on in the game-- is
that we've got this ball with a camera inside of it rolling around
and now it will actually bump into stuff.
And if we notice, as we try to run into mountains and things,
it stops on, like, flying through them weirdly.
So now you can kind of have a sort of physical interaction with the objects.
And that, for the most part, fixes most of the issues.
Another potential issue that we're dealing with
is that Unity really doesn't like what we did in the code
there where we literally just set the velocity to be a particular value.
Much like most of physics, it doesn't like instantaneously setting velocities
and deals much better with setting forces.
And so if we switch over our code to using forces,
it will work a heck of a lot better.
So let's do that.
I'm going to click on Fly and it's now time to become a programming Jedi
and learn how to use the force.
So we're going to make a new public acceleration
and it's going to be a float.
And instead of having a speed, we're going to call this max speed now.
We will still limit the speed so that it doesn't accelerate infinitely fast.
And then we're still going to want to follow particular pointer.
So here again, we still want to get the rigid body.
And then this time, though, we're going to add a force to it.
Not [INAUDIBLE], just add force.
Thank you.
All right, then this force has to be a three dimensional vector, which is just
going to be our acceleration times the direction that the head is pointing in.
And then we've got this one weird last term, which is time.deltatime.
So I'm honestly not exactly sure how time.deltatime works.
It's more of an intuition for me, from what I've seen online,
where any single time you have an action that you want to take place,
like adding a force, and it takes place in between frames
and you want it to add just the right amount of force
for that particular frame, you want to multiply
whatever vector you are doing by time.deltatime
and it will allow you to actually get the object
to move forward at the correct speed.
So if you're just-- if you're confused kind of like I am,
you can do searches online.
And what I generally ask is, like, does ad force use time.deltatime
and you'll usually find some answers online regarding how that works.
So we can out ad a force.
And then, again, we're going to want to actually limit it to max speed.
So our rb.velocity is equal to max speed.
And here we actually want to limit velocity.
So we're going to do vector 3, which is just
the three dimensional-- it's the class for three dimensional vectors.
And we want to clamp the magnitude to be max speed.
And the vector we want to clamp is its own velocity.
And max speed.
So this will take the velocity and set it to make sure that it's always
at max speed or less than it.
Again, like I said before, Unity still doesn't quite like this.
And this isn't the perfect way of doing things.
Ideally, what we would do, is if you were, like, moving forward
at the max speed, it would then start to cut out
whatever component of that particular direction you're moving in.
So if you started to turn, it would still turn
but it would kind of limit how much more forwards you can move.
And always make sure that that speed's the same.
I didn't want to do any of that complicated math here.
This will still work great for what we want to do here.
So we'll do that.
If you want to program that on your own, it shouldn't be that difficult.
But I thought it would be easier to do-- show a simple example here
of how that works.
So switching back to Unity, we should find here that this will update.
Again, I like forcing it to kind of play through stuff
and then update based on that.
There we go-- acceleration--
so acceleration is how quickly it's going to adapt when we turn our heads.
So if we set it to be something, like--
we can actually adjust these values as we go.
Let's set our speed back to 50 like we had it before.
And I'll adjust these values as we play.
So we'll notice that if we set it to 10, it's just barely moving right now.
So maybe we'll bump it up to be, like, 10 times higher.
And now we can see that we kind of start moving,
but still it's very hard to control.
And so even at 1,000 really--
now we actually start getting moving.
But if I'm, like, aiming towards this, and then I
start to turn my head like I want to go that way,
it takes a little bit for it actually move in the direction
that I'm tilting my head.
So if you like that sort of effect, where
if I were to turn directly around, I'd still
be moving backwards a little bit-- and then more like a slingshot or rubber
band-- it kind of like, start moving forwards again.
Then you can keep it around 1,000 or 2,000 or so.
I don't like that effect, and so I just set it to be 10,000.
And then whatever direction I move, I look in--
I pretty much move in that direction.
So I happen to like it at that speed.
And now we have something that's got better physics.
So I keep on talking about this physics, but all we've really seen
is bumping into things.
So it's time for me to talk a little bit more about colliders and how they work.
So you'll notice that here we've got a sphere collider.
And this has a whole bunch of different options.
One of these being a physic material.
So what physic materials do is they determine how objects interact
when they bump into each other.
So let's create our first physics material
just to show what particular options are available.
I'm going to go back to my assets, create a new folder,
material, and then another folder for physic materials.
And then inside of this folder we can create a new physic material.
So we have options here for dynamic friction, static friction, bounciness,
and then how they combine them.
So let's say one of my objects--
like, the mountains are super duper bouncy--
and my player is not bouncy at all.
How are you going to figure out when they collide what's going to happen?
So there are a couple of options.
We can either average them.
We can figure out the minimum of them.
We can multiply them together since they're both
going to be values between 0 and 1.
Or we can find the maximum of them.
Here, I'm going to make this super duper bouncy no matter what bounces into it.
So we're going to set bounciness to the maximum value of 1.
And no matter what your object is like, whenever you bounce into this,
you're going to bounce off of the bounciness of one.
And I'm going to call this, bouncy, because that's what it does.
So now we have this bouncy physic material.
And I happen to find it funny that mountains are generally
not bouncy at all.
But this is virtual reality, so our mountains can be bouncy.
So you'll notice that your mountains already come with a terrain collider
here.
And this is what controls what happens when things bump into the mountains.
And so we can move our bouncy over here to the material.
And now our mountains will become bouncy and we can actually
run through and see what happens when we run into mountains.
Oh, another thing to mention-- so you'll see here we're moving slow again.
If you change these values while you're playing the game,
they reset to the values that they originally were when you started.
So you'll notice that when I first started that other thing,
we started at 10, when I actually wanted it to be 10,000.
So I actually have to quit out of the playing, edit it to 10,000,
and then come back in.
And now it will always default to 10,000 when we come back in.
So there we are moving around.
And it's time to see what happens if we run into one of these mountains.
Will we bounce off?
Boing!
So you can see how now we bounce in between mountains.
And if you get inside one of these pockets,
you can kind of, like, bounce back and forth and all around.
But if we try to run into the walls of the castle here,
you'll notice we don't bounce at all.
And that's because the physic collider on the castle doesn't have any bounce.
You'll also notice, and I said earlier, that you'd
see a better example of, like, displaying things from one side better.
So I talked about how this sphere can only be displayed from one side
overall.
Here you'll notice that we can only see one side of the castle wall.
So if we're looking at it from the outside, we see the castle.
And if we're looking at it from the inside, we don't see the castle at all.
That's mostly because the person who did this didn't use good practices when
creating their model.
So the fbx for this one only used these things called planes on the outside
instead of using cubes.
So it only displays planes from those one side.
Cubes on the other hand, if you're looking at them from the outside,
it'll be visible from all sides of the outside.
So that's mainly an issue with this fbx object.
And I thought about maybe choosing a different one.
But I also thought, hey, this is a great example
of how to deal with issues that you might encounter when creating VR.
So another potential issue is because they only display from one side,
it also means their collider only happens on one side, too.
So you may be wondering how it is that I'm actually
running into something here when there's nothing in front of me.
Well, it turns out the value that I chose for a ball of radius of 50
makes it so that it's actually running into those things at the top there.
It's not running into any wall here.
And if I adjust our player to be, like, a size of 30 or so--
you'll notice that, one, we start moving faster because the ball's smaller.
But also, if we try to run through the wall on this side,
we can actually fit through it.
So it is actually running through those things on the top.
And if we run through--
try to run through it on this side, it does run through the wall.
So you can do different things like that.
So changing the sphere to 50 was a way to make it so, to the user,
it feels like that's still a wall, even though the wall isn't there
just because the person did manage to put some things up on the top
where the ball could be big enough to run into.
So it's little tips and tricks like that that
will make your world feel a lot better to your user,
even though things might not actually be working within your program.
So another thing that colliders can do that I didn't mention
is that they can act as triggers.
So in one of the apps that I made, I wanted the ball to--
I wanted to have certain checkpoints as you went through it.
And so colliders can focus on how objects interact with each other
and how they bounce off and whatnot.
But they can also act as, like, sensors and be completely,
non-physically interactive.
You can run right through them.
But as soon as you touch them, they'll trigger in your code.
And so you can look up more on how to use colliders as triggers
and then you can use them as sensors.
So, like, when my object ran into a checkpoint,
the checkpoint was literally just a cube, an invisible cube that
had a collider that was a trigger on it, and when
the ball touched that particular trigger,
it set off something that made it so that that was the new checkpoint.
So that trigger button does that and makes
it so that things won't bounce off of it or physically interact with it.
But you can then access the trigger within your code.
So now that we've actually done through this--
gone through all of this--
our world is actually complete.
This is the final version that we'll be using for this talk.
And so you can bounce off the mountains.
And feel free to adjust this however you want.
Use different terrains, figure out other cool new stuff
to add, use better fbx materials, and add in cooler castles or other objects
that you find interesting, and color things super duper cool,
and make better mountains.
So you can do a heck of a lot of stuff with all of this.
And I think hopefully you'll enjoy it and putting it on your phone.
So that's exactly what we're going to try to do next,
is put this onto your phone.
So in Unity, there is an option called Build Settings.
And this is how we go through and put things onto your phone.
If you have Android, you're going to want to use the Android platform here.
When you download Unity, there should have been something like,
do you want to install iOS?
Do you want to install Android?
If you clicked on that bit.ly link to VR Seminar
and mentioned that you have to select which version of a phone you have.
So I did download the iOS one when I downloaded Unity.
And so I can select Unity here and say switch platform.
This takes a while and so I'm not going to actually do that right now.
I'm going to go through and open up with a different project that has already
been switched to iOS.
So I'm going to--
oh, a mention on saving, which I should have been doing a lot through this--
but I've read online that saving your scene also saves your projects.
I had a time when I was working on this and I saved my scenes
and then accidentally did a folder move and some of the objects or things
that I had created weren't there.
So I always saved the scene and saved the project.
Saving the project does not save your scene at all,
because there are some things that you may want to change your project that
don't affect the scene.
And so saving your scene should save your project,
but I've had issues with that.
So I always do save scene and save project.
Where I always just save my scenes as Main,
and then they show up here as a scene, and then
sticking with what I've done before--
calling this scenes-- and then putting Main as a scene in there.
So that also means that if you make a new Unity Project,
and you want to use the same scene in that project,
you can just drag the scene inside of your folder file system
for your computer, drag the scene into the new assets
folder for that new project, and boom, the scene will be in there.
All right, so let's open up that project now and save this one.
So Save Project and Save Scenes and we can open up this new one.
Open project.
All right, so I'll open up this.
And inside, see it-- it says 50 Seminar.
All right, which one do I want?
I want the CS50.
Ah, it's under Practice [INAUDIBLE].
So this is the second test.
And this is the world that I had built before this.
You'll see that Unity is restarting and building up this new world.
And this is one that I've already uploaded onto my phone.
So you'll see it looks very similar to the one
that we just built except for this one.
You'll notice that everything is dark right now.
Unity takes a little bit to calculate the light
and how the light should reflect off of objects.
So that's what just happened there-- is it finally figured out how to calculate
how the light bounces off of objects.
And that's one of the other big things that you
might run into if you make your terrain too big,
is that it just can't calculate the light.
So if we go into to Build Settings--
here you'll see that it's already on iOS.
So now we can look at player settings.
Here, if you're an iOS developer, you're going to want to put your company name.
I'm not-- I just have, like, the free version of iOS developing,
so you can really choose whatever you want to be your company
name-- your project name.
And then you're also going to want to go to Other Settings
and copy whatever you have there in to here.
It really doesn't matter whether or not they match all that much.
You can choose pretty much whatever you want.
And I think it works better if you copy one to the other,
and it might be necessary, but really, any value should work.
On these, you can change your version and whatnot if you want that.
This virtual reality supported button--
do not check it.
This is for the new versions of virtual reality.
And you'll see that you can add Cardboard.
You don't want to do that.
Leave it unchecked.
For this version of virtual reality, all of these settings
are inside Cardboard Main and it will make it a virtual reality for you.
So leave that button unchecked for sure.
In your resolution and presentation, you're
going to want to make sure it's listed as Landscape Left.
And then all of the defaults for everything else
should work pretty much as expected.
Another thing to mention is there are particular icons that you
can add here and override them.
Or if you just want to use the general icon, you can put in any PNG file right
here as the icon for your app.
I'm not going to make one here.
It's totally fine.
It will make like a little symbol for you that looks like the Unity symbol.
It's kind of cool.
Anything else I need to do here?
Nope, so this is all you need to do in Unity
and then you can click the Build button.
Again, this takes a fairly long time, and so I'm
going to pull up the one that I've already built for this.
Now it's time to start using Xcode.
So Unity, when you build stuff, it will build it for that particular platform.
So you'll notice that we chose iOS as the platform.
So iOS is openable in Xcode.
So you're going to go and open and find it.
So you'll notice that I had it in the CS50 Seminar.
When you choose to build your project, it will open up and say, like, here's
a new folder.
Where do you want to put it?
Just name that folder and then save it wherever it tells you to.
Or if you want to put it in a different spot, you can.
I like putting mine underneath the objects underneath the folder.
So see here-- practice for CS50 Seminar--
if I go into the folder for that, I have all of these different versions
that I've actually built.
Second test 3 is the latest version that I've built
and so I'm going to open the folder.
If you look inside of this folder, there is an Xcode project inside of it.
You don't want to open that project.
If you do this on the newer versions of the SDK,
it will give you some really weird errors
that are super hard to figure out what's going on.
The trick is to open up the folder and not the project inside of it.
And so open up this second test three folder-- or for you it will be, like,
CS50 Project.
And inside of that, there should be another one
that's the name that you put in when you built the project.
So here, for you guys, it will probably show up just with the Unity iPhone
here.
You can click on it and it should pop up with this General tab.
It will show a little error here because you'll have nobody
selected for your development team.
And then you can just choose yourself.
And it will manage with all these profiles.
Another thing you have to fix in here is that they
didn't add in the security framework.
And so you have to scroll down to Linked Frameworks and Libraries,
add in the security framework.
And then fix that.
You'll see that I had already added it in, so it's just added in again.
And again, another odd thing is in Build Settings,
you should scroll down until you find something called Enable Bit Code.
You want to make sure that that bit code is disabled.
Unfortunately, this makes it so that any virtual reality
app you make can't be uploaded online to the App Store.
I'm not exactly sure of any workarounds for this currently.
And so if you find any through your searches,
please let me know and let other people who are developers know.
Because I've done a lot of research on this
and haven't figured out any way to get around the errors that--
having bit code enabled creates errors, and the only way to get around them
so far has been to disable bit code.
But when you do that, you can't upload apps to the App Store.
So if you find a way around that, that would be great.
Please let me know.
But for now, we're just going to disable that.
And then everything should be up and ready to go.
And so you plug-in your phone and make sure
it's unlocked so it can actually run on it.
And then hit Run and it will upload to your phone.
So this will actually take a lot longer for you
guys because it will have to run through and actually create the entire project
and compile it and run on your phone.
But then once you're done, you can see that it started
running on my phone a little bit.
I'm not exactly sure how easily the cameras picked this up.
But you have your phone here.
You can unpack whatever Google Cardboard you've gotten.
This one happens to come in a box like this.
It has a silly face on front.
I actually used this for my Harvard interview and it was great.
And it made me a lot more comfortable to see my supposedly scary Harvard
interviewer put this on and look like this and is looking around.
So thank you, interviewer, for helping me feel a little bit more
comfortable during the interview.
So you put this on and you put your phone inside of it like this.
And then if you look around, you should be
able to see around inside of your world.
And that will be your virtual reality application.
Do make sure that when you're exporting--
I know that for displaying, we mentioned that this--
where was it--
VR Mode Enabled should be unchecked so that you can see things easily
inside this display.
You do want to have that checked when you export it, or else, what you'll get
is a version that doesn't use Google Cardboard where you should just be
looking like this around in your world.
So that brings us to the end of my presentation.
There are a couple extra things that you can fool around with.
Google gives you the option--
there's like an alignment marker, a settings button,
a Back button that show up on the app here that you can toggle around with.
I hope you guys explore more with Unity and are
able to find out how to add in things like trees and wind
and fool around with all the cool options that Unity has to offer.
You can also fool around with, like, sounds and getting things,
like, the running water on those fountains
there-- and getting it to make it so that as you get closer,
it sounds more and more like the fountain's actually running and just
doing a whole bunch of cool worlds and making them, and encouraging
your friends to make them, as well.
So thank you so much.
And this is the CS50 Seminar.