Time for action – follow the y position of the mouse
Modify your code to add a few familiar-looking lines:
function Update () { var halfW:float = Screen.width/2; transform.position.x = (Input.mousePosition.x - halfW)/halfW; var halfH:float = Screen.height/2; transform.position.z = (Input.mousePosition.y - halfH)/halfH; }
The two new lines of code are almost identical to the first two lines. We've created a new variable and called it halfH
(half height) instead of halfW
. We're changing the z
property of Input.mousePosition
instead of x
. When you save the Script and test your game, you'll have a fully movable paddle to bounce your ball on.
Tip
Math effect
I actually put a little cheat into my code. I wanted the paddle to travel deeper and farther along the Z-axis with less mouse movement, so I changed my halfH
variable declaration to this:
var halfH:float = Screen.height/3;
That's a third of the screen, not a half. Really, I should change the name of my variable to something...