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.
Author
Released
7/19/2012- 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
- Building projectiles with JavaScript
- 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
Duration
Views
Related Courses
-
Game Character Creation in Maya
with Chris Reilly2h 58m Intermediate
-
Introduction
-
Welcome55s
-
Unity basics1m 8s
-
Using the exercise files1m 29s
-
-
1. Exploring Unity 3D
-
Exploring the interface4m 28s
-
Organizing your project8m 12s
-
Exploring Scene view3m 14s
-
Navigating Scene view2m 52s
-
Creating objects7m 38s
-
Transforming objects8m 16s
-
-
2. Exploring the Terrain Editor
-
Painting the topography3m 56s
-
Painting trees and forests4m 56s
-
Painting detail meshes5m 4s
-
Adjusting terrain settings3m 23s
-
3. Creating the Environment
-
Publishing project settings3m 37s
-
4. Introducing Unity Scripting
-
Exploring Unity scripting3m 34s
-
Using variables11m 46s
-
Discovering functions4m 10s
-
Looking at function syntax3m 44s
-
Printing to the console8m 28s
-
Scripting basic functions7m 12s
-
Combining transforms5m 27s
-
Setting transforms8m 34s
-
-
5. In-Game Scripting
-
Raycasting6m 56s
-
Building timers7m 8s
-
Creating a simple projectile13m 16s
-
Refining the projectile5m 32s
-
Sending messages3m 38s
-
Reacting to messages4m 50s
-
Juggling Play and Edit modes7m 33s
-
Suppressing input8m 2s
-
6. Working with GameObjects and Components
-
Creating GameObjects6m 14s
-
Understanding components5m 3s
-
Using colliders for barriers7m 15s
-
Using colliders for triggers4m 28s
-
Exploring physics7m 15s
-
Making cloth9m 56s
-
Working with wind zones7m 50s
-
Using an audio source6m 14s
-
Creating a sound zone5m 34s
-
Adding audio effects6m 31s
-
-
7. Exploring Prefabs
-
Creating and reusing prefabs8m 11s
-
Using prefabs with arrays5m 28s
-
Creating particle systems6m 58s
-
Refining particle systems5m 30s
-
Combining particle systems7m 10s
-
Upgrading weapons6m 15s
-
Exploring water effects7m 53s
-
-
8. Using Imported Assets
-
Importing static objects12m 36s
-
Manipulating textures5m 42s
-
Handling multiple materials5m 42s
-
Animating UVs8m 27s
-
Tracking objects with LookAt5m 45s
-
-
9. Understanding Lighting
-
Investigating ambient light2m 21s
-
Looking at light types3m 13s
-
Exploring shadows5m 23s
-
Baking lighting with Beast8m 16s
-
Experimenting with Beast7m 42s
-
Baking the game scene4m 20s
-
Creating lighting effects6m 24s
-
Adding a flashlight9m 20s
-
-
10. Keyframing Animation
-
Using the Animation view9m 53s
-
Animating transparency7m 12s
-
Keyframing events6m 28s
-
Avoiding keyframing pitfalls5m 27s
-
11. Animating Skinned Meshes and Controlling Characters
-
Importing skinned meshes9m 42s
-
Dynamic parenting6m 44s
-
-
12. Working with Cameras and Layers
-
Introducing cameras4m 53s
-
Managing multiple levels7m 34s
-
-
13. Creating Game GUIs
-
Working with the Unity GUI10m 31s
-
Using GUI skins6m 30s
-
Introducing fonts and styles5m 56s
-
Scripting in-game menus3m 44s
-
Hooking up an in-game menu5m 20s
-
Creating custom cursors8m 42s
-
-
14. Extra Techniques and Features
-
Creating fade transitions5m 34s
-
Setting AI pathfinding9m 56s
-
-
Conclusion
-
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?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake 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.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Scripting basic functions