Join Sue Blackman for an in-depth discussion in this video Scripting basic functions, part of Unity 3D 3.5 Essential Training.
Let's see if we can get the cube moving the same way we got it rotating. First though let's deactivate the Sphere over in the Inspector. So we're going to select this Sphere. We can't see him in the viewport, but he is still there, and in the Inspector we're going to uncheck the Activate button. Now let's go ahead and open the TransformTest script again. And we're double-clicking on it as usual. To start, we're going to comment out the rotation line with our forward slashes, and then I'm going to go out and grab a new bit of code from the exercise files: Ch04/04-08/Code_Snippets.
And we're going to start with this one. Right-click, Copy, and then I'm going to tab back to the Scripting Editor. And I want to add this line of code just below the first one. I'll go ahead and Tab over first, paste, Ctrl+V. And we've got transform.Translate(speed * Time. deltaTime, and then the usual zeros.
So it's pretty much like our first line. We've got Translate instead of Rotate, and we've also got a different variable. So let's pop over and get that one as well. Here is our speed variable, right-click, Copy, and we'll tab back over to the Script Editor, and we'll add this guy up at the top, or near the top, and paste. We can go ahead and click Save now, minimize, and when we click Play, the cube will probably scoot out of the view pretty quickly. And we'll try that. And there it goes.
So we'll exit Play mode and in the Inspector we'll reduce the speed, and we have to select the cube to see the TransformTest script in here, in the Inspector. And so we are going to reduce the speed. I'm going to reduce it to about 5. And then we also should use the middle- mouse roller to zoom back in the viewport so we'll have a better chance of seeing it when it starts moving. Now we can click Play again and it moves slowly off.
Now in case you are wondering what would happen with both lines of code active, we better check that out. So let's back into the Script Editor and uncomment or remove our forward slashes. We can save again. Back in the Unity editor if you click on the cube, over in the Inspector we don't see the Rotation Speed. If you remember, we left it as an internal variable. It looks like we'll have to go back out and fix that.
I'm going to tab over to the Script Editor and remove internal. Delete and remember to save. Now we can go back to the Unity Editor and as soon as the focus is changed, our Rotation Speed variable turns up again. And I want to set that to about 50. Now let's click play and see what happens. We should have both the rotation and the transform happening.
And sure enough, our cube is rotating and moving at the same time. Now if you're a little bit unsure of what's happening here, there's two things you need to do. First make sure the cube is selected, which it is. We can see our Transform gizmo, but here is the trick. Instead of Global we want to see Local. With Local turned on, now you can see wherever the X axis is pointed, that's where our cube is moving.
Now we can see that the Translate function moves an object along the object's local axis. Wherever the cube is rotated to face, the cube moves forward in that direction. Let's go ahead and exit Play mode. So far so good. We can animate an object to move or rotate using transform.Rotate and transform.Translate. And in case something about those looks familiar, yes, they are functions and they belong to transform.
But what if we just wanted to put an object in a different location, not animate it? For that we will need to change the object's transform parameters. Take a look at the cube's Transform values in the Inspector as it animates. So we'll turn it on for a second again. We can see the object's Transform is moving in the Inspector. Let's exit Play mode again. All the Transform values are variables: Position, Rotation, and Scale.
The parameter names are capitalized in the Inspector, just like our own variable names were, but since they're really just variables, they won't be capitalized when we assign our own values to them. Let's have our own script give us the position of the cube this time. So let's open up the Script Editor again. This time we'll comment out both of the transform lines. And here is something that's a little new: if we right-click, in the right- click menu we can Toggle Line Comments.
That way we get them both at once. Now in the Update function, let's add another bit of code that we'll go out and grab. This time I'm going to copy, tab back into my Script Editor, and paste, and this time we're going to have it print (transform.Position). So now we can go ahead and save, get back into the Editor, and now when we click play, if we move the cube around, we should be able to see its position being reported in the status line. And sure enough, there it is.
The position is reported as a three-part variable, a Vector3 type. We'll go about setting position in the next video. So to recap, transform.Translate moves an object using its local coordinate system. Vector3 type contains three values-- x, y,z--and transform.Position will report the location of an object.
- Understanding game and level design theory
- Organizing your project in Unity
- Creating and transforming objects
- Setting up the geometry
- Painting in terrain, textures, and trees
- Adjusting the render settings
- Importing terrains
- Creating a first-person controller
- Creating materials and shaders
- Lighting the game
- Working with cameras and multiple views
- Animating characters and assets
- Creating fire with particle systems
- Managing the GUI (graphical user interface)
Skill Level Beginner
Creating Urban Game Environments in 3ds Maxwith Adam Crespi5h 54m Intermediate
1. Exploring Unity 3D
2. Exploring the Terrain Editor
3. Creating the Environment
Publishing project settings3m 37s
4. Introducing Unity Scripting
5. In-Game Scripting
6. Working with GameObjects and Components
7. Exploring Prefabs
8. Using Imported Assets
9. Understanding Lighting
10. Keyframing Animation
11. Animating Skinned Meshes and Controlling Characters
12. Working with Cameras and Layers
13. Creating Game GUIs
14. Extra Techniques and Features
What's next1m 13s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.