Time for action - follow the Y position of the mouse

  1. 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 ...

Get Unity 3D Game Development by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.