3D Under the Hood / Throwing Objects
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Now that we know everything there is to know about how 3D works, how scripting works, what X, Y and Z's are and what Local versus Global is, let's put this knowledge into practice and add something to our character that he can throw inside of the world. Now in the last couple parts we've been working on a crate prefab and it should be in your Prefabs folder and this is the crate right here. Now make sure it's called crate with a small C and all small capital letters and we'll start using that in our script. Now in our Scripts folder right-click and create a new JavaScript and call this one Shoot. OK, we'll use this one for shooting. Now open it up and object function is already there but before we start messing around in the Update function let's first add some variables. The first variable that we need is called speed and let's set this variable to a float of 3.0. Now remember an integer variable is a whole number, a float has a. so it has decimals. Now you do note that speed is turning a different color so it's probably a predefined word, but in this case it's not going to bug us much, so it's fine like this. We need a second variable and this variable we'll call cratePrefab and let's make this of the type Transform. As you can see I'm not using an equal sign here, I'm just using a column and then Transform to make sure that you need notice that this is a Transform variable, so in that way it will point back to a game object. That is all that we need from here. Now in the Update function we can start writing our code. Now first, of course, all we need to do is: find out if a fire button is pressed. Of course, you don't want the script to start throwing crates around at will, you only want the crate to be thrown when there's actually a button pressed, so in this case we'll type: if Input.GetButtonDown and this will be a string Fire1 and everything inside this code is true. Now make sure you copy everything exactly - input with a capital I, transform with a capital T, GetButtonDown with capitals and then Fire1, OK? Now, it's time for us to create the prefab in 3D and we do this using script and after that we need to add force to that prefab and this way the crate will move around in 3D space. Now to make a new prefab in 3D space we will make a new variable and call it var crate and this crate we need to instantiate so type Instantiate and if you type it correctly it will turn a different color and the Instantiate function, which is predefined by Unity takes 3 variables: 1, 2 and 3. You have to divide these by commas so that the script knows that you're sending 3 variables to this function. OK, the first is what prefab are we going to make, so that is our cratePrefab as we have defined this variable here and we simply set that function in here. Next, the variable that the Instantiate function needs is the position of where in the world we will create it. Now we are going to attach this one to the Main Camera so we just need to type transform.position and this will set the position of our Main Camera. And then last but not least the third variable is the rotation so and this is done in Unity using Quaternians. Now I'll explain this later so for now just type Quaternian.identity, which is basically the rotation of our current camera so it shoots in 3D space. OK so this will create our crate, now we need to add some force to our crate. So our crate has a rigid body attached to it in our Prefab so we can use the Rigid Body functions. Type crate.rigidbody.AddForce and then the force will come from a transform.forward, which is basically shorthand for where the camera is looking toward, so basically is a local Z, it's depth multiplied by a speed of 2000. Alright let's save this. Let's close our script and then attach our script to our Main Camera. Now in our Main Camera you can see that speed is defined as a variable and our crate Prefab is defined as a variable but our crate Prefab right now is empty, it's an empty Transform variable. Now simply to solve that we just go to Prefab and then drag our crate right on top of this variable and not it's set. Of course, we could do that in script also but why not use the power of the Editor to make things happen pretty quickly. Now when I run our game and I use the left control, which is our Fire Button, you can see I'm actually shooting out crates into world. That's pretty cool, huh? Now if you want, of course, you can add some more floors or something to make sure that things bounce off of each other, but that's pretty cool so far.
Tutorial Information
| Course: | Introduction to Game Development Using Unity 3D |
| Author: | Efraim Meulenberg |
| SKU: | 34092 |
| ISBN: | 1-936334-03-8 |
| Release Date: | 2010-02-23 |
| Duration: | 5.5 hrs / 75 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 