Time for action – create a new MouseFollow Script

  1. In the Project panel, right-click/alternate-click on an empty chunk of space and navigate to Create | JavaScript. Alternatively, you can navigate to Assets | Create | JavaScript in the menu at the top of the screen, or use the Create button at the top of the Project panel.
  2. A new Script is added to the Project panel. Name it MouseFollow.
  3. Drag-and-drop your new MouseFollow Script onto your Paddle GameObject.
  4. Double-click to open the Script in MonoDevelop. Just as before, we're going to add a single, simple line of code inside the curly braces (sandwich buns) of the Update function (sandwich):
        function Update () {
     transform.position.x = 2;
        }
  5. Add a Rigidbody component to Paddle by navigating to Component | Physics | Rigidbody.
  6. In the Inspector panel, find the Rigidbody component and check box next to Is Kinematic. The Unity manual warns against moving colliders around through code without adding a Rigidbody component because it could add a lot of overhead (unnecessary calculation processing), so we've added Rigidbody. The Is Kinematic option excludes the Paddle GameObject from Unity's physics simulation.
  7. Save the Script and press Play to test your game. Like pulling the chair out from beneath someone when he tries to sit down, your Paddle should act like a total jerk, and pop out of the way to let your Ball plummet to its doom. Not cool, Paddle. Not cool.

What just happened?

Just as we saw with the Mesh Renderer component, Transform is also a component of your GameObject. It's the first attached component on the list in the Inspector panel when you select your Paddle. As we learned in Chapter 3, Game #1 – Ticker Taker, the Transform component decides how the GameObject is positioned, rotated, and scaled (made larger or smaller).

What just happened?

In the Unity environment, the Transform component of our Paddle was set to position 0 in the X-axis. But, we changed this with our line of code, setting the x property of the paddle's transform to 2. The result is that the first time the Update function is called, the paddle appears to jump out of the way to two units along the X-axis.

The thought may have already struck you: if you can control the x position of the Paddle, you can probably also control its y and z positions just as easily. And if position is available to you, rotation and scale can't be far behind! But, which keywords should you use to change these properties? rotation and scale are two good guesses, but we'd rather be sure.

To satisfy your curiosity, let's hit the Unity Script Reference again; this time, we'll take a shortcut. Highlight the word transform in your Script and press Ctrl + ' on your keyboard, or command + ' on a Mac (that's the apostrophe character). You'll zip directly to the Script Reference with a list of hits as if you'd searched for transform in the search field. What wonders await you within?

What just happened?